十六週二次課

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 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章