【Shell】菜鳥教程bash學習記錄

1 . 通用

  • Bourne Again Shell稱爲bash
  • #!申明解釋器,#!/bin/bash
  • POSIX 可移植操作系統接口Portable Operating System Interface of Unix
  • chmod +x file 添加文件可執行權限
  • 使用變量$
  • 雙引號字符串"value is ${num}"
  • #計算字符串長度
  • $string:n:m提取子串
  • 數組用空格分開Array=(1 2 3 4)
  • $Array[@]表示所有數組元素
  • 獲取數組長度${#Array[@]}
  • #表註釋
  • 腳本傳參$n
  • expr表達式計算
  • expr a + b運算符與數字之間要有空格
  • 乘法\*
  • \n換行
  • \c不換行
  • echo "sss" > file 輸出到文件
  • printf
    • %-10s長度爲10的字符,-左對齊
    • %d %f
  • test [$a == $b]

2. 運算符

  • 關係運算符(只用於數字)
    • -eq equal
    • -ne not equal
    • -lt less than
    • -gt great than
    • -le less or equal
    • -ge great or equal
  • 布爾運算符
    • ! not
    • -o or
    • -a and
  • 邏輯運算符
    • && 邏輯與
    • || 邏輯或
  • 字符串運算符
    • =
    • !=
    • -z 長度是否爲零
    • $ 是否爲空
  • 文件測試運算符
    • -d
    • -f
    • -e
    • -r -w -x

3. 流程控制語句

  • if ... then ... else ... fi
# 判斷語句
if [ $a == $b ]
    then
        echo 'true'
fi
  • for ... in ... do... done
# 循環語句
for a in ${array[@]}
do
    echo $a
done
  • while ... do ... done
  • until ... do ... done
  • case...in...)...;;...esac
  • function(){...}
  • 函數返回值,調用函數後用$?獲取

4. 輸入輸出重定向

  • command > file 終端不再有輸出,重定向的含義就是本來輸出到終端的被重新定向輸出到文件
  • command >> file 追加到文件末尾
  • command < file
  • 每個linux命令運行時都會打開三個文件:stdin stdout stderr
  • >/dev/null禁止輸出

5. 文件包含

  • . filename 引入文件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章