shell腳本學習總結(不斷更新中)

前言:自從大學畢業參加工做以來,接觸的開發工做都是在服務端完成,因而接觸了比較多的Linux當作開發機使用,或多或少有一些重複性的工做,因而開始琢磨學習一些shell腳本的知識,以便處理這些繁瑣的事情。html


 

shell種類有不少bash、sh、csh、ksh等,不一樣的環境下使用的也不一樣,不過bash應該是使用最普遍了~也是Linux發行版默認的;shell

主要就是掌握常見的命令以及語法格式吧~而後就能夠上手幹活了:數組

命令以下:bash

  1. echo:輸出命令到終端
    • -e:表示對轉義字符進行替換
    • -E:禁止轉義
  2. read:從終端中讀入命令
  3. readonly:將命令標記爲只讀命令
  4. unset:刪除變量,可是不能刪除readonly標記的變量
  5. printf:usage: printf format-string [arguments...]  eg:printf "%s %s %s\n" a b c d e f g h i j

轉義字符:ide

  1. \\:反斜槓
  2. \a:警報
  3. \b:退格
  4. \f:換頁
  5. \n:換行
  6. \r:回車
  7. \t:tab鍵
  8. \v:垂直製表符

特殊變量:函數

  1. $0:當前腳本的文件名
  2. $n:表示傳遞給文件或者函數的參數,n是幾就表示第幾個參數
  3. $#:表示參數的個數
  4. $*:表示全部的參數
  5. $?:上個命令的退出狀態或則函數的返回值
  6. $$:當前shell的進程ID
  7. $@:也是表示全部參數,和$*的區別就是:"$@"會顯示全部的參數,每一個一行,"$*"將全部的參數顯示在一行

命令替換:`command`學習

DATE=`date`
echo "Date is $DATE"USERS=`who | wc -l`
echo "Logged in user are $USERS"UP=`date ; uptime`
echo "Uptime is $UP"


變量替換:測試

  1. ${var} 變量原本的值
  2. ${var:-word} 若是變量var爲空或則Unset,那麼返回word,可是不改變var的值
  3. ${var:=word}若是變量var爲空或則unset,那麼返回word,並將var的值設置爲word
  4. ${var:?messgage}若是變量var爲空或則unset,那麼messgae將送到標準錯誤輸出,腳本中止運行
  5. ${var:+word}若是變量var被定義,那麼返回word,但不改變var的值

運算符:url

  1. 算術運算符(expr):
    1. +  加法  `expr $a + $b`
    2. -   減法  `expr $a - $b`
    3. *      乘法      `expr  $a \* $b`
    4. /       除法      `expr $b / $a`
    5. %     取餘       `expr $b % $a`
    6. =      賦值       a=$b 把變量b的值賦給a
    7. ==   相等,用於比較兩個數字,相同則返回true    [$a == $b]
    8. !=    不相等。用於比較兩個數字,不相同則返回true  [$a != $b]
    9. eg:
      #!/bin/sh
      a=10
      b=20
      val=`expr $b % $a`
      18.echo "b % a : $val"

       

  2. 關係運算符
    1. -eq    檢測兩個數是否相等,相等則返回true
    2. -ne    檢測兩個數是否不相等,不相等則返回true
    3. -gt    檢測左邊的數是否大於右邊的數,若是是,則返回true
    4. -lt     檢測左邊的數是否小於右邊的數,若是是,則返回ture
    5. -ge   檢測左邊的數是否大於等於右邊的數,若是是,則返回true
    6. -le    檢測右邊的數是否小於等於右邊的數,若是是,則返回true   
    7. eg:
      #!/bin/sh
      
      a=10
      b=20
      if [ $a -eq $b ]
      then
         echo "$a -eq $b : a is equal to b"
      else
         echo "$a -eq $b: a is not equal to b"
      fi

       

  3. 布爾運算符:
    1. !    非運算    [!false] 返回true
    2. -o    或運算    [$a -lt 20 -o $b -gt 100]
    3. -a    與運算    [$a -lt 20 -a $b -gt 100]  
  4. 字符串運算符:中括號兩邊要留個空隙
    1. =    檢測兩個字符串是否相等,相等則返回true     [ $a = $b ]返回false
    2. !=   檢測兩個字符串是否相等,不相等返回true     [ $a != $b ]返回true
    3. -z    檢測字符串長度是否爲0, 爲0返回ture         [ -z $a ]
    4. -n    檢測字符串長度是否不爲0,不爲0返回true    [ -n $a ]
    5. str   檢測字符串是否爲空,不爲空返回true           [ $a ] 返回true  
  5. 文件測試運算符:
    1. -b file    檢測是不是塊設備文件    [-b $file]
    2. -c file    檢測是否是字符設備文件   [-c $file]
    3. -d file   檢測是否是目錄     [-d $file]
    4. -f file    檢測是否是普通文件(既不是目錄,也不是設備文件) [-f $file]
    5. -g file   檢測是否設置了SGID位
    6. -k file   檢測是否設置了(sticky bit)
    7. -p file   檢測是否具備管道
    8. -u file   檢測是否設置了SUID位
    9. -r file    檢測是否可讀
    10. -w file   檢測是否可寫
    11. -x file   檢測是否能夠執行
    12. -s file   檢測是否爲空文件(大小爲0)
    13. -e file   檢測文件是否存在

字符串操做:spa

  1. ${#string}    $string的長度
  2. ${string:position}  在$string中,從位置$position開始提取子串
  3. ${string:position:length}   在$string中,從位置$position開始提取長度爲$length的子串
  4. ${string#substring}   從變量$string的開頭,刪除最短匹配$substring的子串
  5. ${string##substring}  從變量$string的開頭,刪除最長匹配$substring的子串
  6. ${string%substring}  從變量$string的結尾,刪除最短匹配$substring的子串
  7. ${string%%substring}  從變量$string的結尾,刪除最長匹配$substring的子串
  8. ${string/substring/replacement}   在$string中用$replacement取代第一個匹配的$substring
  9. ${string//substring/replacement}  在$string中用$replacement取代所有匹配的$substrng
  10. ${string/#substring/replacement} 若是$string的前綴匹配$substring,那麼用$replacement取代$substring
  11. ${string/%substring/replacement} 若是$string的後綴匹配$substring,那麼用$replacement取代$substring

數組的操做:  

  定義數組:array_name=(value0 value1 value2 value3)數組的操做:
  讀取數組:${array_name[2]}
  讀取數組的元素的個數:${#array_name[@]}/${#array_name[*]}
  返回數組的全部的下標:${!array_name[@]}/${!array_name[*]}
  返回數組的全部的元素:${array_name[@]}/${!array_name[*]}

注意的事項:

  1. 變量的初始化中間不能有空格,eg: name="Spider"
  2. 變量的使用時候,最好以${name}這種形式,確認變量的邊界;eg: echo 'I am ${name}-spiders'
  3. 註釋只能用#號
  4.  if..else使用:
    • if then... fi 語句;
    • if then... else ... fi 語句;
    • if then... elif then... else ... fi 語句。
  5. case..switch
    1.   
      echo 'Input a number between 1 to 4'
      echo 'Your number is:\c'
      read aNum
      case $aNum in
          1)  echo 'You select 1'
          ;;
          2)  echo 'You select 2'
          ;;
          3)  echo 'You select 3'
          ;;
          4)  echo 'You select 4'
          ;;
          *)  echo 'You do not select a number between 1 to 4'
          ;;
      esac


       

  6. for循環:
    1.   
      for 變量 in 列表
      do
          command1
          command2
          ...
          commandN
      done

       

  7. while循環:
    1.   
      while command
      do
         Statement(s) to be executed if command is true
      done

       

  8. until循環:
    1.   
      until command
      do
         Statement(s) to be executed until command is false
      done

       

  9. break and continue ,和其它語言不一樣的是,shell中的能夠後加數字n,表示第幾層循環
  10. 函數:
    1. 注意,$10 不能獲取第十個參數,獲取第十個參數須要${10}。當n>=10時,須要使用${n}來獲取參數  
      function_name () {
          list of commands
          [ return value ]
      }

       command > file   輸出重定向到file

    2. command < file   輸入重定向到file
    3. command >> file 輸出以追加的方式重定向到file
    4. n > file 將文件描述符重定向到file
    5. n >> file 將文件描述符以追加的方式重定向到file
    6. n >& m 將輸出文件m和n合併
    7. n <& m 將輸入文件m和n合併
    8. << tag 將開始標記tag和結束標記tag之間的內容做爲輸入
  11. 重定向,若是但願將 stdout 和 stderr 合併後重定向到 file,能夠這樣寫:
    1. $command > file 2>&1
  12. 重定向到/dev/null 文件,屏蔽輸出結果
  13. 調試shell 腳本:在#! /bin/bash -x 就能夠看到腳本執行的順序
  14. 通配符

    shell常見通配符:

    字符 含義 實例
    * 匹配 0 或多個字符 a*b  a與b之間能夠有任意長度的任意字符, 也能夠一個也沒有, 如aabcb, axyzb, a012b, ab。
    ? 匹配任意一個字符 a?b  a與b之間必須也只能有一個字符, 能夠是任意字符, 如aab, abb, acb, a0b。
    [list]  匹配 list 中的任意單一字符 a[xyz]b   a與b之間必須也只能有一個字符, 但只能是 x 或 y 或 z, 如: axb, ayb, azb。
    [!list]  匹配 除list 中的任意單一字符 a[!0-9]b  a與b之間必須也只能有一個字符, 但不能是阿拉伯數字, 如axb, aab, a-b。
    [c1-c2] 匹配 c1-c2 中的任意單一字符 如:[0-9] [a-z] a[0-9]b  0與9之間必須也只能有一個字符 如a0b, a1b... a9b。
    {string1,string2,...} 匹配 sring1 或 string2 (或更多)其一字符串 a{abc,xyz,123}b    a與b之間只能是abc或xyz或123這三個字符串之一。 
相關文章
相關標籤/搜索