Linux基礎(day69)

20.1shell腳本介紹

shell腳本介紹

  • shell是一種腳本語言 和傳統的開發語言比較,會比較簡單
    • shell有本身的語法;可使用邏輯判斷、循環等語法
  • 能夠自定義函數,目的就是爲了減小重複的代碼
  • shell是系統命令的集合
  • shell腳本能夠實現自動化運維,能大大增長咱們的運維效率

20.2 shell腳本結構和執行

shell腳本結構和執行

  • 開頭須要加#!/bin/bash
  • 以#開頭的行做爲解釋說明
  • 腳本的名字以.sh結尾,用於區分這是一個shell腳本
  • 執行方法有兩種
  • chmod +x 1.sh; ./1.sh
  • bash 1.sh
  • 查看腳本執行過程 bash -x 1.sh
  • 查看腳本是否語法錯誤 bash -n 1.sh

shell腳本結構和執行

  1. 建立一個shell文件夾,並切入到文件夾中
[root@hanfeng ~]# mkdir shell
[root@hanfeng ~]# cd shell/
[root@hanfeng shell]#
  1. 寫shell腳本
  • #! /bin/bash //第一行必須這麼寫,固定格式,做用就是腳本如果在當臺機器上去執行,能夠不加這一行也不要緊,由於它知道下面若干條的命令能在這臺機器上去執行,去解析
[root@hanfeng shell]# vi 01.sh

#! /bin/bash   	//固定格式
echo "123"
w
ls
保存退出
  1. 執行腳本——>sh 01.sh
[root@hanfeng shell]# sh 01.sh
123
 22:45:12 up 14 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     22:32    0.00s  0.05s  0.03s w
01.sh
[root@hanfeng shell]#
  1. 在當前終端裏,把01.sh中的#! /bin/bash 去掉後在執行腳本,會看到獲得的結果相同,不會出現任何的問題,這就說明這臺機器是能識別裏面一條一條的命令的,去運行這裏面的命令;但如果換一臺機器,就不必定能執行了
  2. 在第一行,文件頭指定 #!/bin/bash ,接下來要運行的命令是經過哪個解釋器來操做的,一般都是 /bin/bash 解釋器來執行的
  3. 給01.sh文件一個執行權限
[root@hanfeng shell]# chmod a+x 01.sh
[root@hanfeng shell]#

7執行shell腳本 ./01.sh 能這樣執行,說明這些命令被解析了,被/bin/bash認識了linux

[root@hanfeng shell]# ./01.sh
123
 23:11:21 up 41 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     22:32    1.00s  0.05s  0.00s /bin/bash ./01.sh
01.sh
[root@hanfeng shell]#
  1. /bin/bash也是一條命令, /bin/bash 和 /bin/sh 是同一個文件
[root@hanfeng shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960368 6月  10 2014 /bin/bash
[root@hanfeng shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 10月 21 00:47 /bin/sh -> bash
[root@hanfeng shell]#
  1. 01.sh文件內容就是被/bin/bash所解析的
  2. 若沒有 /bin/bash ,可使用 /bin/bash 01.sh去執行
[root@hanfeng shell]# /bin/bash 01.sh
123
 23:21:43 up 51 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     22:32    7.00s  0.03s  0.00s w
01.sh
[root@hanfeng shell]#
  1. 如果在shell腳本中在寫入第二遍 #號開頭的行, 就表示解釋說明的做用
  2. 腳本的通常都是以.sh結尾,是爲了區分這是一個shell腳本,不然須要打開這個文件,才知道是shell腳本
  3. 運行shell腳本有兩種方法
  • 一種是sh 01.sh運行shell腳本
  • 另外一種方法
    • 先 chmod a+x 1.sh 給文件加一個執行權限
    • 再 ./1.sh 去執行
      • 這裏的 ./ 就至關於一個相對路徑,相對當前一個路徑
      • 也可使用絕對路徑去執行腳本 /root/shell/01.sh ,其實就是找到這個文件去執行
[root@hanfeng shell]# /root/shell/01.sh
123
 23:45:02 up  1:14,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     22:32    6.00s  0.03s  0.00s w
01.sh
[root@hanfeng shell]#
  1. 查看腳本執行過程 sh -x 01.sh 或者bash -x 01.sh
  • -x,就是顯示腳本執行的過程
  • 每個加號,表示一個操做步驟
[root@hanfeng shell]# sh -x 01.sh
+ echo 123
123
+ w
 23:47:35 up  1:17,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     22:32    7.00s  0.03s  0.00s sh -x 01.sh
+ ls
01.sh
[root@hanfeng shell]#
  1. 查看腳本是否有錯誤 sh -n 01.sh
  • 如果沒有任何的輸出,那麼腳本則沒有錯誤
  • sh -n 01.sh命令是檢測是否存在語法錯誤
[root@hanfeng shell]# sh -n 01.sh
[root@hanfeng shell]#

20.3 date命令用法

date命令用法

  • date +%Y-%m-%d, date +%y-%m-%d 年月日
  • date +%H:%M:%S = date +%T 時間
  • date +%s 時間戳
  • date -d @1504620492
  • date -d "+1day" 一天後
  • date -d "-1 day" 一天前
  • date -d "-1 month" 一月前
  • date -d "-1 min" 一分鐘前
  • date +%w, date +%W 星期

date命令用法

  1. date命令,會顯示當前系統時間日期
[root@hf-01 ~]# date
2018年 01月 14日 星期日 06:13:14 CST
[root@hf-01 ~]#
  1. date命令,在shell中用處很是大;對文件後綴增長一個時間,以便後期管理
  2. date +%Y-%m-%d, date +%y-%m-%d 年月日
[root@hf-01 ~]# LANG=en	切換爲英文顯示
[root@hf-01 ~]# date
Sun Jan 14 06:19:49 CST 2018
[root@hf-01 ~]# date +%Y	
2018	四位的年
[root@hf-01 ~]# date +%y
18		兩位的年
[root@hf-01 ~]# date +%m
01		月份
[root@hf-01 ~]# date +%M
20		分鐘
[root@hf-01 ~]# date +%d
14		日期
[root@hf-01 ~]# date +%D
01/14/18	直接標記年月日,不過格式比較特殊
[root@hf-01 ~]# date +%Y%m%d
20180114	年月日
[root@hf-01 ~]# date +%F
2018-01-14	年月日,這種帶橫槓的
[root@hf-01 ~]#
  1. 常見時間單位
[root@hf-01 ~]# date +%w
0		表示周幾
[root@hf-01 ~]# date +%W
02		今年的第幾周,今年的第二週
[root@hf-01 ~]# date +%h
Jan		英文的月份
[root@hf-01 ~]# date +%H
06			小時
[root@hf-01 ~]# date +%S
04			秒
[root@hf-01 ~]# date +%s
1515882702	這是一個時間戳,距離1970總共過去多少秒
  1. 時間其餘標記方法
  • date +%H:%M:%S = date +%T 時間
[root@hf-01 ~]# date +%T
06:24:36
[root@hf-01 ~]# date +%H:%M:%S
06:24:36
[root@hf-01 ~]#
  1. 顯示日曆 cal命令,查看到日期
[root@hf-01 ~]# cal
    January 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

[root@hf-01 ~]#
  1. 標記以前的日期
  • 好比:在作nginx日誌切割的時候,到了凌晨切割日誌,到了零點零分切割的日誌是前一天的日誌。因此把日誌加一個時間標記的話,應標記爲昨天的日期
  1. 學會用date標記以前的日期
  • day、month、year、hour、min後面能夠加 s 能夠不加 s
  • 減號- 表示以前的日期,加號 + 表示從今日後的日期
  • date -d "-1 day" +%F 顯示前一天的日期
  • date -d "-1 month" +%F 顯示上個月的日期
  • date -d "-1 years" +%F 顯示上一年的日期
  • date -d "+1 hour" +%T 顯示下一小時
  • date -d "+1 min" +%T 顯示下一分鐘
[root@hf-01 ~]# date -d "-1 day"
Sat Jan 13 06:47:30 CST 2018
[root@hf-01 ~]# date -d "-1 day" +%F
2018-01-13
[root@hf-01 ~]# date -d "+1 month" +%F
2018-02-14
[root@hf-01 ~]# date -d "+1 year" +%F
2019-01-14
[root@hf-01 ~]# date -d "+1 hour" +%T
08:09:05
[root@hf-01 ~]# date -d "+1 min" +%T
07:11:21
  1. 時間戳
  • date +%s
  • 另外一種表現方法,表示時間戳
    • date -d @1504620492 就是@後跟時間戳
[root@hf-01 ~]# date +%s
1515885248
[root@hf-01 ~]# date -d @1515885248
Sun Jan 14 07:14:08 CST 2018
[root@hf-01 ~]#
  1. 若想在linux系統中,把具體的日期換算成時間戳的時候,可使用date +%s -d "2018-01-13 07:14:08"
[root@hf-01 ~]# date +%s -d "2018-01-13 07:14:08"
1515798848
[root@hf-01 ~]# date -d @1515798848
Sat Jan 13 07:14:08 CST 2018
[root@hf-01 ~]#

20.4 shell腳本中的變量

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]
相關文章
相關標籤/搜索