1. 程式人生 > >20.1 shell腳本介紹 20.2 shell腳本結構和執行 20.3 date命令用法 20.4 shell腳本中的變量

20.1 shell腳本介紹 20.2 shell腳本結構和執行 20.3 date命令用法 20.4 shell腳本中的變量

20.1 shell腳本介紹 20.2 shell腳本結構和執行 20.3 date命令用法 20.4 shell腳本中的變量

- 20.1 shell腳本介紹
- 20.2 shell腳本結構和執行
- 20.3 date命令用法
- 20.4 shell腳本中的變量
# 20.1 Shell腳本介紹
-  shell是一種腳本語言  關註aming_linux  blog.lishiming.net
-  可以使用邏輯判斷、循環等語法
-  可以自定義函數
-  shell是系統命令的集合
-  shell腳本可以實現自動化運維,能大大增加我們的運維效率








# 20.2 Shell腳本結構和執行
- 開頭需要加#!/bin/bahs  //告訴系統,這個腳本是通過哪一個解釋器來進行操作的
- 以#開頭的行作為解釋說明
- 腳本的名字以.sh結尾,用於區分這是一一個shell腳本
- 先創建一個shell目錄
```
[root@aming-01 ~]# clear
[root@aming-01 ~]# mkdir shell
[root@aming-01 ~]# cd shell/
[root@aming-01 shell]# ls
[root@aming-01 shell]# vi 01.sh

#!/bin/bash
echo "123"
w
ls
~                                                                                         
                                                                                   
~                                                                                         
:wq
[root@aming-01 shell]# vi 01.sh
```

- 執行方法有兩種:
chmod +x 1.sh; ./1.sh
bash 1.sh  , sh 1.sh
```
[root@aming-01 shell]# sh 01.sh
123
 21:07:19 up 5 min,  2 users,  load average: 0.04, 0.33, 0.20
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      21:02    4:39   0.02s  0.02s -bash
root     pts/0    192.168.202.1    21:03    7.00s  0.03s  0.01s w
01.sh
[root@aming-01 shell]# 
```
- 還有一種, 文件開頭的意思就是意味著 接下來的命令是由這個文件來解析的。
```
[root@aming-01 shell]# chmod a+x 01.sh
[root@aming-01 shell]# ./01.sh
123
 21:09:48 up 7 min,  2 users,  load average: 0.14, 0.23, 0.18
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      21:02    7:08   0.02s  0.02s -bash
root     pts/0    192.168.202.1    21:03    4.00s  0.03s  0.00s /bin/bash ./01.sh
01.sh
[root@aming-01 shell]# 
[root@aming-01 shell]# cat 01.sh
#!/bin/bash
echo "123"
w
ls
[root@aming-01 shell]# 

```
- bin/bash 其實就是 bin/sh
```
[root@aming-01 shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960392 8月   3 2016 /bin/bash
[root@aming-01 shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 10月  5 22:18 /bin/sh -> bash
[root@aming-01 shell]# 

```

- 查看腳本執行過程,每個+表示一個步驟
```
[root@aming-01 shell]# sh -x 01.sh
+ echo 123
123
+ w
 21:17:01 up 14 min,  2 users,  load average: 0.02, 0.07, 0.12
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      21:02   14:21   0.02s  0.02s -bash
root     pts/0    192.168.202.1    21:03    5.00s  0.05s  0.00s sh -x 01.sh
+ ls
01.sh
[root@aming-01 shell]# 
```
- 也可以用 sh -n 查看腳本的語法是否正確
- 沒有輸出,表示沒有錯誤

```
[root@aming-01 shell]# sh -n 01.sh
[root@aming-01 shell]# 
```
- 修改一下文件測試一下錯誤
```
[root@aming-01 shell]# sh -n 01.sh
[root@aming-01 shell]# vi 01.sh

#!/bin/bash
echo "123"
w
ls
for i in `seq 1 10`
do 
 echo $i
~                                                                                         
                                                                                      
~                                                                                         
~                                                                                         
:wq

```
- 再執行下sh -n 看下
```
[root@aming-01 shell]# sh -n 01.sh
01.sh:行8: 語法錯誤: 未預期的文件結尾
[root@aming-01 shell]# 
```
- 改回來
```
[root@aming-01 shell]# vi 01.sh

#!/bin/bash
echo "123"
w
ls
~                                                                                         
~                                                                                         
~                                                                                         
                                                                                      
:wq
```
- 再監測就是沒用問題
```
[root@aming-01 shell]# sh -n 01.sh
[root@aming-01 shell]# 
```







# 20.3 date命令用法
- date  在shell中用處非常大;對文件後綴增加一個時間,以便後期管理
- date +%Y-%m-%d, date+%y-%m-d 年月日
```
[root@aming-01 ~]# date
2017年 11月 20日 星期一 22:01:01 CST
[root@aming-01 ~]# 
```
- m 月,M 分鐘 
```
[root@aming-01 ~]# date
2017年 11月 20日 星期一 22:01:01 CST
[root@aming-01 ~]# date +%Y
2017
[root@aming-01 ~]# date +%y
17
[root@aming-01 ~]# date +%m
11
[root@aming-01 ~]# date +%M
27
[root@aming-01 ~]# 
```
- 日期 d  大D 
```
[root@aming-01 ~]# date +%d
20
[root@aming-01 ~]# date +%D
11/20/17
[root@aming-01 ~]# 
```
- 日期,年月日 date +%Y%m%d
```
[root@aming-01 ~]# date +%Y%m%d
20171120
[root@aming-01 ~]# 
```
- 日期 date +%F
```
[root@aming-01 ~]# date +%F
2017-11-20
[root@aming-01 ~]# 
```
- 小時 ,分 ,秒   
- s 表示一個時間戳 表示 距離1970年1月1日0點0分到現在的 總共多少秒
```
[root@aming-01 ~]# date +%H
22
[root@aming-01 ~]# date +%s
1511188397
[root@aming-01 ~]# date +%S
22
[root@aming-01 ~]# date +%S
32
[root@aming-01 ~]# 
```
- 關於時間
- date +%T
- date +%H%M%S
```
[root@aming-01 ~]# date +%T
22:35:38
[root@aming-01 ~]# date +%H%M%S
223608
[root@aming-01 ~]# 
```
- h顯示月份,
```
[root@aming-01 ~]# date +%h
11月
[root@aming-01 ~]# 
```
- 先把語言改成英文
```
[root@aming-01 ~]# LANG=en
[root@aming-01 ~]# date +%h
Nov
[root@aming-01 ~]# 
```
- 加上冒號 date +%H:%M:%S 等同於  date +%T
```
[root@aming-01 ~]# date +%H:%M:%S
22:42:38
[root@aming-01 ~]# date +%T
22:42:42
[root@aming-01 ~]# 
```
- 周 
- w 周幾
- W 今年的第幾周
```
[root@aming-01 ~]# date +%w
1
[root@aming-01 ~]# date +%W
47
[root@aming-01 ~]# 
```
- 顯示日歷cal
```
[root@aming-01 ~]# cal
    November 2017   
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

[root@aming-01 ~]# 
```
- 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 一年前

- 標記昨天的日期 date -d "-1 day"
```
[root@aming-01 ~]# date
Mon Nov 20 23:07:38 CST 2017
[root@aming-01 ~]# date -d "-1 day"
Sun Nov 19 23:07:46 CST 2017
[root@aming-01 ~]# 
[root@aming-01 ~]# date -d "-1 day" +%F
2017-11-19
[root@aming-01 ~]# 

```
- 上個月 date -d "-1 month"
```
[root@aming-01 ~]# date -d "-1 month" +%F
2017-10-20
[root@aming-01 ~]# 
```
- 上一年 date -d "-1 year"
```
[root@aming-01 ~]# date -d "-1 year" +%F
2016-11-20
[root@aming-01 ~]# date -d "-1 year"
Sun Nov 20 23:10:43 CST 2016
[root@aming-01 ~]# 
```
- 上一個小時  date -d "-1 hour"
```
[root@aming-01 ~]# date -d "-1 hour" +%T
22:11:43
[root@aming-01 ~]# 
```
- 時間戳 轉換成具體的日期時間,也可以現在的時間轉換成時間戳
```
[root@aming-01 ~]# date +%s
1511190759
[root@aming-01 ~]# 

[root@aming-01 ~]# date -d @1511190759
Mon Nov 20 23:12:39 CST 2017
[root@aming-01 ~]# 
[root@aming-01 ~]# date +%s -d "2017-11-20 23:12:39"
1511190759
[root@aming-01 ~]# 
```








# 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]


20.1 shell腳本介紹 20.2 shell腳本結構和執行 20.3 date命令用法 20.4 shell腳本中的變量