设为首页 收藏本站
查看: 1378|回复: 0

linux下shell脚本编程1

[复制链接]

尚未签到

发表于 2018-8-20 06:46:42 | 显示全部楼层 |阅读模式
  1、 shell脚本是什么
  它是一种脚本语言,并非编程语言。
  可以使用一些逻辑判断、循环等语法。
  可以自定义子函数,是系统命令的集合。
  shell脚本可以实现自动化运维,大大增加我们的工作效率。
  2、shell脚本结构以及执行方法
  开头行指定bash路径: #! /bin/bash
  以#开头的行作为解释说明
  #注释自己的脚本内容,方便自己查阅;utf8字符集,支持中文;
  脚本的名字以.sh结尾,用于区分这是一个shell脚本
  执行脚本方式有两种:
  chmod a+x 1.sh    添加x执行权限;
  ./1.sh 可以直接执行,或者写绝对路径/root/shell/1.sh
  如果没有执行权限可以 bash 1.sh    或 sh 1.sh
  bash -x 1.sh 可以查看脚本执行过程
  实验练习:
[root@localhost shell]# cat 1.sh  
#!/bin/bash
  
#这是我的第一个脚本
  
echo "hello world"
[root@localhost shell]# ls -l  
-rw-r--r-- 1 root root 60 6月  16 19:28 1.sh
  
[root@localhost shell]# chmod a+x 1.sh
  
[root@localhost shell]# ls -l
  
-rwxr-xr-x 1 root root 60 6月  16 19:28 1.sh
  
[root@localhost shell]# ./1.sh
  
hello world
  
[root@localhost shell]# /root/shell/1.sh
  
hello world
  
[root@localhost shell]# /bin/sh 1.sh
  
hello world
  
[root@localhost shell]# bash -x 1.sh
  
+ echo 'hello world'
  
hello world
  执行bash和sh是一样的,sh是bash的软连接文件;
[root@localhost ~]# ls -l /bin/bash  
-rwxr-xr-x. 1 root root 871700 10月 16 2014 /bin/bash
  
[root@localhost ~]# ls -l /bin/sh
  
lrwxrwxrwx. 1 root root 4 3月   4 00:59 /bin/sh -> bash
  3、学会date命令的用法
  date  +%Y-%m-%d     date +%y-%m-%d 年月日
  date  +%Y-%m-%d = date +%F 年月日
  date  +%H:%M:%S = date +%T 时间
  date +%s  时间戳
  date -d @1434248742    根据时间戳算出当前时间
  date -d "+1day" 一天后    date -d "-1day" 一天前
  date -d  "-1month" 一月前
  date -d  “-1min” 一分钟前
  date +%w     date +%W 星期
  实验练习:
[root@localhost shell]# date +%y  
15
  
[root@localhost shell]# date +%Y
  
2015
  
[root@localhost shell]# date +%m
  
06
  
[root@localhost shell]# date +%d
  
16
  
[root@localhost shell]# date +%H
  
14
  
[root@localhost shell]# date +%M
  
01
  
[root@localhost shell]# date +%S
  
54
  从1970年 01月01日0点0分开始算起到现在多少秒;
[root@localhost shell]# date +%s  
1434434874
  
[root@localhost shell]# date -d @1434434874
  
2015年 06月 16日 星期二 14:07:54 CST
  CST是中国时间 +8小时
[root@yonglinux shell]# date -d @0  
1970年 01月 01日 星期四 08:00:00 CST
[root@localhost shell]# date +%F  
2015-06-16
  
[root@localhost shell]# date +%T
  
14:04:17
  
[root@localhost shell]# date +%Y-%m-%d
  
2015-06-16
  
[root@localhost shell]# date +"%Y-%m-%d %H:%M:%S"
  
2015-06-16 14:05:13
  
[root@localhost shell]# date +"%F %T"
  
2015-06-16 14:05:38
  周二
[root@localhost shell]# date +%w  
2
  今年的第多少周,24周
[root@localhost shell]# date +%W  
24
  全年有多少周,52周;
[root@localhost shell]# echo "365/7" | bc  
52
[root@localhost shell]# date -d "-1 day"  
2015年 06月 15日 星期一 14:16:31 CST
  
[root@localhost shell]# date -d "-1 day" +"%F %T"
  
2015-06-15 14:19:13
  
[root@localhost shell]# date -d "+1 day" +"%F %T"
  
2015-06-17 14:19:22
  
[root@localhost shell]# date -d "+1 month" +"%F %T"
  
2015-07-16 14:19:31
  
[root@localhost shell]# date -d "+1 year" +"%F %T"
  
2016-06-16 14:19:39
  
[root@localhost shell]# date -d "+1 week" +"%F %T"
  
2015-06-23 14:19:45
  
[root@localhost shell]# date -d "-10 hour" +"%F %T"
  
2015-06-16 04:19:59
  
[root@localhost shell]# date -d "-10 min" +"%F %T"
  
2015-06-16 14:10:15
  
[root@localhost shell]# date -d "-10 sec" +"%F %T"
  
2015-06-16 14:20:14
  4、shell脚本中的变量
  当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替。
  使用条件语句时,常常使用变量    if [ $a -gt 1 ]; then ... ; fi
  引用某个命令的结果时,用变量替代   n=`wc -l 1.txt`
  写和用户交互的脚本时,变量也是必不可少的  read -p "Input a number: " n; echo $n
  如果没写这个n,可以直接使用$REPLY
  内置变量 $0, $1, $2,$#    $0表示脚本本身,$1 第一个参数,$2 第二个参数,$#表示参数的个数;
  数学运算a=1;b=2; c=$(($a+$b))  或者 c=$[$a+$b]
  实验练习:
  引用某个命令的结果,使用变量代替
[root@localhost shell]# file=`which yum`  
[root@localhost shell]# echo $file
  
/usr/bin/yum
  
[root@localhost shell]# rpm -qf $file
  
yum-3.2.29-60.el6.centos.noarch
  变量只在当前shell下生效,子shell不会生效;
  要想子shell也生效,使用export file 声明变量;
  用户交互的变量:
[root@localhost shell]# cat 2.sh  
#!/bin/bash
  
#与用户交互的变量
  
read -p "请输入一个数字:" num
  
echo $num
  
[root@localhost shell]# sh 2.sh
  
请输入一个数字:333
  
333
  参数的变量:
[root@localhost shell]# cat 3.sh  
#!/bin/bash
  
#关于参数的变量
  
echo "\$1=$1"
  
echo "\$2=$2"
  
echo "\$3=$3"
  
echo "\$#=$#"
  
echo "\$0=$0"
  
[root@localhost shell]# sh 3.sh ABC linux world
  
$1=ABC
  
$2=linux
  
$3=world
  
$#=3
  
$0=3.sh
  数值变量:
[root@localhost shell]# a=1;b=2  
[root@localhost shell]# c=$a+$b
  
[root@localhost shell]# echo $c
  
1+2
  
[root@localhost shell]# c=$[$a+$b]
  
[root@localhost shell]# echo $c
  
3
  
[root@localhost shell]# c=$(($a+$b))
  
[root@localhost shell]# echo $c
  
3
  
  5、shell中的逻辑判断
  格式1:if 条件 ; then 语句; fi
  格式2:if 条件; then 语句; else 语句; fi
  格式3:if …; then … ;elif …; then …; else …; fi
  逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等;注意到处都是空格。
  可以使用 &&并且 || 或者 结合多个条件
  大于>gt    (greater than)
  小于< lt     (less than)
  大于等于 >= ge
  小于等于 /tmp/$d.log 2>&1  表示执行下面的行,输出正确或错误的信息都写到/tmp/目录下 日期.log里面;



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-553992-1-1.html 上篇帖子: shell中的select循环使用 下篇帖子: shell脚本——翻译英文单词
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表