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
test [$a == $b]
2. 運算符
- 關係運算符(只用於數字)
-eq
equal
-ne
not equal
-lt
less than
-gt
great than
-le
less or equal
-ge
great or equal
- 布爾運算符
- 邏輯運算符
- 字符串運算符
- 文件測試運算符
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. 文件包含