shell腳本介紹,shell腳本結構和執行,date命令用法,shell腳本中的變量

shell腳本介紹

  • shell是一種腳本語言 aming_linux(公衆號) blog.lishiming.net(博客)
  • 可以使用邏輯判斷、循環等語法
  • 可以自定義函數
  • shell是系統命令的集合
  • shell腳本可以實現自動化運維,能大大增加我們的運維效率

    shell腳本結構和執行

  • 開頭需要加#!/bin/bash,意味着接下來的語句是由這個文件解析的,因爲有了它我們纔可以./1.sh這樣執行,不然只能/bin/bash 1.sh這樣執行
    [root@akuilinux01 shell]# chmod a+x 1.sh 
    [root@akuilinux01 shell]# ./1.sh
    123
    22:37:09 up  4:02,  2 users,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     22:29    5.00s  0.10s  0.00s /bin/bash ./1.sh
    root     pts/1    192.168.21.1     19:00    3:31m  0.08s  0.08s -bash
    1.sh
    [root@akuilinux01 shell]# /bin/bash 1.sh 
    123
    22:37:27 up  4:03,  2 users,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     22:29    7.00s  0.10s  0.00s /bin/bash 1.sh
    root     pts/1    192.168.21.1     19:00    3:31m  0.08s  0.08s -bash
    1.sh
  • 以#開頭的行作爲解釋說明,除了第一行的#!/bin/bash和有些特殊的腳本
  • 腳本的名字以.sh結尾,用於區分這是一個shell腳本
  • 執行方法有兩種
    • chmod +x 1.sh; ./1.sh
    • sh 1.sh
    • sh -x 1.sh 顯示執行過程
      
      [root@akuilinux01 shell]# sh -x 1.sh 
    • echo 123
      123
    • w
      22:43:05 up 4:08, 2 users, load average: 0.00, 0.01, 0.05
      USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
      root pts/0 192.168.21.1 22:29 1.00s 0.11s 0.00s sh -x 1.sh
      root pts/1 192.168.21.1 19:00 3:37m 0.08s 0.08s -bash
    • ls
      1.sh
      帶+號的就是執行的語句
    • sh -n 1.sh 可以檢查腳本的語法問題

      date命令用法

  • 年月日
    [root@akuilinux01 shell]# date +%Y-%m-%d
    2018-07-11
    [root@akuilinux01 shell]# date +%y-%m-%d
    18-07-11
    [root@akuilinux01 shell]# date +%Y%m%d
    20180711
    [root@akuilinux01 shell]# date +%D
    07/11/18
    [root@akuilinux01 shell]# date +%F
    2018-07-11
  • date +%s  時間戳,距離1970年1月1日0點0分過去多少秒,還有時間戳和時間之間轉換
    [root@akuilinux01 shell]# date +%s
    1531320917
    [root@akuilinux01 shell]# date -d @1531321618
    2018年 07月 11日 星期三 23:06:58 CST
    [root@akuilinux01 shell]# date +%s -d "2018-07-10 23:06:58"
    1531235218
  • 時間
    [root@akuilinux01 shell]# date +%H:%M:%S
    22:57:20
    [root@akuilinux01 shell]# date +%T
    22:57:32
  • 星期
    [root@akuilinux01 shell]# date +%w
    3
    [root@akuilinux01 shell]# date +%W
    28
    W是今年的第幾周
  • 日誌標記昨天日期的方法
    [root@akuilinux01 shell]# date -d "-1 day" +%F
    2018-07-10
    [root@akuilinux01 shell]# date -d "-1 month" +%F
    2018-06-11
    [root@akuilinux01 shell]# date -d "-1 year" +%F
    2017-07-11
    [root@akuilinux01 shell]# date -d "-1 hour" +%T
    22:05:55
    [root@akuilinux01 shell]# date -d "+1 day" +%F
    2018-07-12
  • 查看日曆
    [root@akuilinux01 shell]# cal 
      七月 2018     
    日 一 二 三 四 五 六
    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

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