shell是一种脚本语言, shell是系统命令的集合
一、可使用逻辑判断、循环等语法;
跟C语言有点像,但有本质的区别。
二、能够自定义函数;
定义函数是为了减小咱们的重复代码。
三、它能够实现自动化运维,能够批量同时执行,能大大增长咱们的运维效率,;
要想学好写好shell就要不断的去练习。shell
1、在开头须要加bash
#!/bin/bash
指定用bash解释器来执行,以#开头行进行解释说明。脚本名以.sh结尾,用于区分这是一个shell脚本。
2、执行shell脚本方法有两种:运维
[root@riven ~]# chmod a+x 1.sh #给脚本执行权限 [root@riven ~]# ./1.sh #执行权限后用./直接执行 [root@riven ~]# bash 1.sh #直接用bash执行 12233...
[root@riven ~]# bash -n 1.sh # -n 查看脚本是否是语法错误 [root@riven ~]# bash -x 1.sh #-x 查看脚本的执行过程 + echo 12233... 12233... + echo 111 [root@riven ~]#
3、data命令的用法
1、直接打data命令它会显示系统的日期和时间。
2、data命令在shell里面很是有用,它能够用来标记时间。
标记方法:ide
[root@riven ~]# date +%Y #年 2018 [root@riven ~]# date +%m #月 07 [root@riven ~]# date +%d #日 13 [root@riven ~]# date +%M #分钟 58 [root@riven ~]# date +%y #两位数的年 18 [root@riven ~]# date +%D #带/的年月日 07/14/18 [root@riven ~]# date +%Y%m%d 20180714 [root@riven ~]# date +%F #带横线的年月日 2018-07-14 [root@riven ~]# date +%H #小时 00 [root@riven ~]# date +%S #秒 38 [root@riven ~]# date +%s #时间戳,距离1970年0点0分0秒过去多少钞 [root@riven ~]# date +%T #当前时间 00:04:24 [root@riven ~]# date +%H%M%S #时分秒 000554 [root@riven ~]# date +%H:%M:%S 00:06:02 [root@riven ~]# cal #显示最近一个月的日历 七月 2018 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [root@riven ~]# date -d "-1 day" +%F #前一天 2018-07-13 [root@riven ~]# date -d "+1 day" +%F #后一天 2018-07-15 [root@riven ~]# date -d "+1 year" +%F #后一年 2019-07-14 [root@riven ~]# date -d "-1 year" +%F #前年 2017-07-14 [root@riven ~]# date -d "-1 month" +%F #上个月 2018-06-14 [root@riven ~]# date -d "+1 month" +%F #下个月 2018-08-14 [root@riven ~]# date -d "+1 hour" +%T #后一个小时 01:16:58 [root@riven ~]# date -d "-1 hour" +%T #前一个小时 23:17:04 [root@riven ~]# date -d @16032303211 #时间戳转成具体时间 2478年 01月 16日 星期日 09:33:31 CST [root@riven ~]# date +%s -d "2018-09-09 00:00:00"#具体时间转换成时间戳 1536422400 [root@riven ~]#
1、当脚本中使用某个字符串较频繁而且字符串长度很长时就应该使用变量代替。
2、使用条件语句时,常使用的变量:
if [$a -gt 1];then ...;fi
三、引用某个命令的结果时,用变量代替:
n='wc -| 1.txt'
四、写和用户交互的脚本时,变量也是必不可少的
read -p "Input a number: " n; echo $n #若是没写这个n,能够直接使用$REPLY
五、内置变量 $0, $1, $2… $0表示脚本自己,$1 第一个参数,$2 第二个 …. $#表示参数个数
6、数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]函数