十六周二次课

20.1 shell脚本介绍

shell 是一种脚本语言;和传统的开发语言比较,会比较简单

shell有自己的语法;可以使用逻辑判断、循环等语法

可以自定义函数
定义函数的目的,就是为了减少重复代码

shell是系统命令的集合

shell脚本可以实现自动化运维,能打打的增加我们的运维效率

20.2 shell脚本结构和执行

开头需要加#!/bin/bash //告诉系统,这个脚本是通过哪一个解释器来进行操作的

以#开头的行作为解释说明

脚本的名字以.sh结尾,用于区分这是一一个shell脚本

执行方法有两种:

chmod +x 1.sh; ./1.sh
bash 1.sh

查看脚本执行过程

bash -x 1.sh
[root@aminglinux-02 shell]# sh -x 01.sh
+ echo 123        //表示运行的命令
123
+ w
20:34:44 up  3:11,  1 user,  load average: 0.50, 0.56, 0.56
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    172.16.22.220    10:23    1.00s  0.07s  0.01s w
+ ls
01.sh

查看脚本是否有语法错误

bash -n 1.sh

没有输出,表示没有错误

修改一下文件测试一下错误

#!/bin/bahs
echo "123"
w
ls
for i in `seq 1 10`
do
  echo $i
don

检查错误:

# sh -n 01.sh
01.sh:行9: 语法错误: 未预期的文件结尾

20.3 date命令用法

  • date +%Y-%m-%d, date +%y-%m-%d 年月日
[root@test220 ~]# date +%Y-%m-%d

2018-03-29

[root@test220 ~]# date +%y-%m-%d

18-03-29

[root@test220 ~]# date +%F

2018-03-29

[root@test220 ~]# date +%Y%m%d

20180329
  • date +%H:%M:%S = date +%T 时间
[root@test220 ~]# date +%H:%M:%S

11:18:24

[root@test220 ~]# date +%T

11:18:49
  • date +%s 时间戳
[root@test220 ~]# date +%s

1522293644

[root@test220 ~]# date +%s -d '2017-09-18 17:23:25'

1505726605
  • date -d @1504620492 //转换时间戳为时间
[root@test220 ~]# date -d @1504620492

2017年 09月 05日 星期二 22:08:12 CST

[root@test220 ~]# date -d @`date +%s`

2018年 03月 29日 星期四 11:22:33 CST

[root@test220 ~]# date

2018年 03月 29日 星期四 11:22:54 CST
  • date -d "+1day" 一天后
[root@test220 ~]# date -d "+1day"

2018年 03月 30日 星期五 11:24:42 CST

[root@test220 ~]# date -d '1day' +%F

2018-03-30
  • date -d "-1 day" 一天前
[root@test220 ~]# date -d "-1day"

2018年 03月 28日 星期三 11:25:45 CST

[root@test220 ~]# date -d "1day ago"

2018年 03月 28日 星期三 11:28:36 CST

[root@test220 ~]# date -d "1day"

2018年 03月 30日 星期五 11:35:39 CST
  • date -d "-1 month" 一月前
[root@test220 ~]# date -d '-1month'

2018年 03月 01日 星期四 11:38:13 CST

[root@test220 ~]# date -d "-1month"

2018年 03月 01日 星期四 11:38:52 CST
  • date -d "-1 min" 一分钟前
[root@test220 ~]# date -d '-1min'

2018年 03月 29日 星期四 11:38:43 CST
  • date +%w, date +%W 星期
[root@test220 ~]# date +%w

4

[root@test220 ~]# date +%W

13
  • cal 以日历形式显示
[root@test220 ~]# cal

March 2018

Su Mo Tu We Th Fr Sa

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

date +%w星期几;date +%W 本年的第几个星期
date -d "+1 day" +%F 一天后
date -d "-1 day" +%F一天前
date -d "-1 month" +%F一个月前
date -d "-1 min " +%F一分钟前
date -d "-1 year " +%F 一年前

20.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))

或者

$[$a+$b]
  • Shell编程中的注释以#开头
  • 对shell变量进行数字运算 使用expr 命令:expr integer operator integer,其中operator 为+ - / %, 但对的使用要用转义符\
  • Shell编程的参数传递, 可通过命令行参数以及交互式输入变量(read) 例:

restoreall.sh 对backup.sh程序的备份磁带进行恢复:

$ cat > restoreall.sh

cd $WORKDIR

cpio -i < /dev/rmt/0h
$      s hel l 变量名的开始 如$var  
|      管道 将标准输出转到下一个命令的标准输入  
#     注释开始  
&     在后台执行一个进程  
?        匹配一个字符  
*       匹配0 到多个字符(与DOS不同可在文件名中间使用并且含.)  
$-    使用set 及执行时传递给shell的标志位  
$!     最后一个子进程的进程号  
$#    传递给shell script 的参数个数  
$*    传递给shell script 的参数  
$@    所有参数 个别的用双引号括起来  
$?    上一个命令的返回代码  
$0    当前shell的名字  
$n    (n: 1-)  位置参数  
$$    进程标识号(Process Identifier Number, PID)  
>f i l e         输出重定向  
<f i l e         输入重定向  
`command`     命令替换 如    filename=`basename /usr/local/bin/tcsh`
>>fiile       输出重定向 append 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章