if 條件判斷
if (()); then
echo "OK";
elif (()); then
echo "OK";
else
echo "Not OK";
fi
-f 判斷文件是否存在 if [ -f 文件名 ]
-d 判斷目錄是否存在 if [ -d 目錄名 ]
-eq 整型 ==
-ne 整型 !=
-lt 整型 <
-gt 整型 >
-le 整型 <=
-ge 整型 >=
-z 空字符串判斷
格式化輸出
echo -e "\033[32m this is xxx \033[0m"
常量
$0 當前腳本名
$1 參數(shù)1
$n 輸入的第n個參數(shù)
循環(huán)
for 變量 in 字符串
for ((i=0;i<=100;i++))
do
循環(huán)體
done
--------------------------------
for i in ` find /var/log/ -name "*.log"`
do
循環(huán)體
done
while
while [[ 條件 ]]; do
循環(huán)體
done
read 交互命令
read -p "please input a number" input
echo $input
< 導入命令
while read line
do
echo "$line"
done </etc/hosts
循環(huán)內(nèi)的計數(shù),注意加號前后要有空格
index=1
index=`expr ${index} + 1`
Until 循環(huán)(直到條件滿足才推出)
until 條件
do
action
done;
case 語句
case $arg in
pattern1)
...
;;
pattern2)
...
;;
*)
...
;;
esac
select 語句
select i in 變量
do
action
done
變量
局部變量
全局變量
變量引用:$變量
$0 當前程序的名稱
$n 當前程序的第n個參數(shù),n=1,2,3,4,5..
$# 當前程序的參數(shù)個數(shù)(不包括程序本身)
$? 命名或程序執(zhí)行完以后的狀態(tài)简十,一般返回0表示執(zhí)行成功
$UID 當前用戶的id
$PWD 當前所在的目錄
數(shù)組
數(shù)組的定義鄙早,一般以括號的方式來定義,A=(1 2 3)
數(shù)組的引用:echo ${A[0]},從0開始
{A[@]} 全部
數(shù)組個數(shù):{#A[@]}
函數(shù) function name() {}
awk枫甲、sed命令
sed 's/old/new/g' 文件名源武;全文件替換
sed 's/^/& /g' 文件名;文件行首添加空格
sed '/old/a 追加內(nèi)容' 文件名言秸; 在指定位置后添加一行
sed -n '1p' 取首行
sed -n '$p' 取尾行
find 查找命令
find path -name "關(guān)鍵詞"
find . -name "a.txt" -exec cp {} /temp/ \; 查找并拷貝
find . -name "a.txt" -exec rm -rf {} \; 查找并刪除
字符串函數(shù)
zch=abcd
${#zch} 獲取字符串長度
${zch:position:n} 從指定position位置(從0 開始)软能,截取n長度的