1、shell脚本介绍:
shell是一种脚本语言和传统的开发语言相比,会比较简单:
shell有自己语法,可以支持逻辑判断、循环等语法:
可以自定义函数,目的是减少重复的代码:
shell是系统命令的集合:
shell脚本可以实现自动化运维,能大大增加我们的运维效率:
2、shell脚本结构和执行:
开头需要加#!bin/bash
#号开头注释,解释说明:
脚本需要以.sh结尾,用以区分是shell脚本:
执行的方法有两种:
chmod +x 1.sh ; sh 1.sh
bash 1.sh
查看脚本的执行过程: sh -x 1.sh
查看脚本的是否语法错误: sh -n 1.sh
3、shell脚本操作:
创建一个shell目录,用于写shell脚本: mkdir shell ; cd shell
#!bin/bash 第一行必须要这样写,固定的格式,表示在当前机器上用什么命令去执行这个脚本:
[root@localhost_02 shell]# cat 1.sh#!/bin/bashecho "123"wls
执行脚本: sh 1.sh
[root@localhost_02 shell]# sh 1.sh123 14:47:01 up 1:28, 1 user, load average: 0.03, 0.03, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.149.135 13:20 5.00s 0.04s 0.00s w1.sh :q!
注释:在当前机器上,不加"#!/bin/bash"也是可以执行的,得到的结果也相同,说明机器可以识别到每一条命令,但是换一台机器就不一定能可以执行。
通常指定"#!/bin/bash"后,接下来要运行的这些命令是通过哪一个解释器来操作的,通常都是/bin/bash这个解释器来执行的,它的作用都在于此:
3:通过给文件一个执行权限: chmod a+x 1.sh
执行./1.sh ,说明它的这行命令已经解析,被bin/bash认识了:
[root@localhost_02 shell]# chmod a+x 1.sh[root@localhost_02 shell]# ./1.sh123 14:50:13 up 1:31, 1 user, load average: 0.01, 0.02, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.149.135 13:20 5.00s 0.05s 0.00s /bin/bash ./1.sh1.sh :q!
其实/bin/bash和bin/sh是同一个文件: 连接文件:
[root@localhost_02 shell]# ls -l /bin/bash-rwxr-xr-x. 1 root root 960472 8月 3 2017 /bin/bash[root@localhost_02 shell]# ls -l /bin/shlrwxrwxrwx. 1 root root 4 5月 31 22:04 /bin/sh -> bash
若没有/bin/sh这个文件:则可以使用/bin/bash这个命令来执行:
[root@localhost_02 shell]# /bin/bash 1.sh123 14:53:19 up 1:34, 1 user, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.149.135 13:20 7.00s 0.09s 0.03s w1.sh :q!
注释:若以"#"开头的则表示解释说明,在脚本中第二行开始开头的:
脚本一般都是以sh结尾的,用于区分是shell脚本,如若没有,则需要查看这个文件才能知道是shell脚本:
shell有两种执行方式:
1) sh 1.sh 运行shell脚本:
2) chmod a+x 1.sh 先给脚本添加一个执行权限,
然后 ./1.sh 来执行.
这里的 ./ 是一个相对路径的目录,表示当前目录:
也可以使用绝对路径来执行: /root/shell/1.sh #其实就是找到这个文件再执行:
[root@localhost_02 shell]# /root/shell/1.sh 123 15:04:11 up 1:45, 1 user, load average: 0.01, 0.03, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.149.135 13:20 3.00s 0.07s 0.01s w1.sh :q!
查看脚本的执行过程: 注释:每一个加号,则表示一个操作步骤:
sh -x 1.sh 或者 /bin/bash -x 1.sh
[root@localhost_02 shell]# sh -x 1.sh+ echo 123123+ w 15:06:37 up 1:47, 1 user, load average: 0.02, 0.03, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.149.135 13:20 5.00s 0.06s 0.00s sh -x 1.sh+ ls1.sh :q!
查看脚本是否有错误: 注释:若没有错误,则不会有任何的输出:
sh -n 1.sh
[root@localhost_02 shell]# sh -n 1.sh
2、data 命令语法: 一般用法前面需要"+"号:
%y-%m-%d ==== 年 月 日
%H:%M:%S ==== 小时 分钟 秒
小s:表示时间戳: | 大S:表示秒:
date +%Y-%m-%d 表示年月日(大Y表示四位的年):
date +%y-%m-%d 表示年月日(小y表示两位的年):
date +%s 表示时间戳:(距离1970年过去了多少秒):
date -d @1537515069 (数字是时间戳显示出来的数字): 显示出来的也是当前日期:
date -d "+1 day" 表示一天前:
date -d "-1 day" 表示一天后:
date -d "+1 month" 表示一月前:
date -d "-1 month" 表示一月后:
date -d "+1 min" 表示一分钟前:
date -d "-1 min" 表示一分后:
date +%w 表示周几:
date +%W 表示一年的第几周:
date语法实践:
date 命令,会显示当前的系统的日期和时间:
[root@localhost_02 shell]# date2018年 09月 21日 星期五 15:39:22 CST
date命令在shell中的作用非常大,一般用于对文件的后缀名增加一个时间:
1:表示年 月 日 date +%Y-%m-%d date +%y-%m-%d
[root@localhost_02 shell]# LANG=en_US.UTF-8 #设定字符集:[root@localhost_02 shell]# date +%Y-%m-%d #表示年月日,四位的年: 2018-09-21[root@localhost_02 shell]# date +%y-%m-%d #表示年月日,两位的年:18-09-21[root@localhost_02 shell]# date +%Y #表示四位的年:2018[root@localhost_02 shell]# date +%y #表示两位的年:1[root@localhost_02 shell]# date +%m # m 表示月份:09[root@localhost_02 shell]# date +%d # d 表示日期:21[root@localhost_02 shell]# date +%D # D 表示一种特殊格式的年月份:09/21/18[root@localhost_02 shell]# date +%F # 表示年月日的简写:2018-09-21[root@localhost_02 shell]# date +%Y%m%d #表示年月日,不加杆也可以的:20180921
注释:其实x表示年月日的写法: data +%F === date +%Y-%m-%d === date +%D(特殊格式)
注释:在表示年月日的时候:默认是用" - "来区分的(如 date +%F), 不加也是可以的,如上例子:
2:时间单位(小时 分钟 秒 周末) #默认使用冒号分割:
[root@localhost_02 shell]# date +%T # T 表示简写的小时 分钟 秒:15:53:08[root@localhost_02 shell]# date +%H:%M:%S # 表示小时 分钟 秒:15:54:30[root@localhost_02 shell]# date +%H # H 表示小时:15[root@localhost_02 shell]# date +%M # M 表示分钟:55[root@localhost_02 shell]# date +%S # S 表示秒:06[root@localhost_02 shell]# date +%s #表示时间戳(距1970年过去了多少秒):1537516549[root@localhost_02 shell]# date +%w # w 表示周几:5[root@localhost_02 shell]# date +%W # W 表示今年的第几周:38
注释:表示时间的方式: date +%T === date +%H:%M:%S
注释:表示日期的方式: date +%F === date +%Y-%m-%h
3、date还用来标记之前的日期:会用来标记日记(前一天的日期):
year month day hour min second week (后面的s可加可以不加):
用加减标记(" - "减号标记之前的日期) (" + "加号标记之后的日期)
day -1 "-1 year" #显示上一年的日期:
day -1 "-1 month" #显示上一个月的日期:
day +1 "+1 day" #显示明天的日期:
day +1 "+1 hour" #显示下一个小时的日期:
day +1 “+1 min” #显示下一个分钟的日期:
day +1 "+1 sec" #显示下一秒的日期:
[root@localhost_02 shell]# date -d "-1 year" #显示上一年的日期:2017年 09月 22日 星期五 07:49:09 CST[root@localhost_02 shell]# date -d "-1 month" #显示上一个月的日期:2018年 08月 22日 星期三 07:49:16 CST[root@localhost_02 shell]# date -d "-1 day" #显示昨天的日期:2018年 09月 21日 星期五 07:49:20 CST[root@localhost_02 shell]# date -d "+1 hour" #显示一小时后的日期:2018年 09月 22日 星期六 08:49:28 CST[root@localhost_02 shell]# date -d "+1 min" #显示一分钟后的日期:2018年 09月 22日 星期六 07:50:32 CST[root@localhost_02 shell]# date -d "+1 sec" #显示一秒后的日期:2018年 09月 22日 星期六 07:49:37 CST[root@localhost_02 shell]# date -d "+1 week" #显示一周后的日期:2018年 09月 29日 星期六 07:49:40 CST
4、时间戳: date +%s date -d
date +%s 表示显示出时间戳
[root@localhost_02 shell]# date +%s1537574033[root@localhost_02 shell]# date -d @1537574033 @后面跟的就是时间戳:2018年 09月 22日 星期六 07:53:53 CST
注释:若想在linux系统中,把当前日期换算成时间戳,可以使用: date +%s -d"2018-09-22 07:56:53"
[root@localhost_02 shell]# date +%F-%T2018-09-22-07:56:53[root@localhost_02 shell]# date +%s -d"2018-09-22 07:56:53"1537574213
5、在linux下还可以编辑日期: cal
[root@localhost_01 network-scripts]# cal 九月 2018 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930
3、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)) 或者$[$a+$b]