what is shell and why I should learn shell script
(what)shell腳本即用shell命令執(zhí)行一些強(qiáng)大的功能号胚。
(why)shell可以在win和linux上執(zhí)行员萍,熟練掌握腳本使我們操作計(jì)算機(jī)更靈活快速
應(yīng)用場景
- 執(zhí)行接口自動(dòng)化框架
- 自動(dòng)實(shí)現(xiàn)更新工程中的sdk庫
- 自動(dòng)打包承绸、編譯江醇、發(fā)布
- 清理磁盤
- 總之可以使復(fù)雜命令簡單化都可以嘗試用shell腳本實(shí)現(xiàn)
shell工作原理
和Python一樣屬于腳本語言,不需要編譯娘纷,因此效率稍微差一點(diǎn)
一個(gè)hello的簡單腳本
#!/bin/bash
echo "hello shell"
將shell命令保存為test1.sh嫁审,進(jìn)入的到test1.sh所在目錄跋炕,輸入test1.sh執(zhí)行腳本如下圖所示:
好了赖晶,一個(gè)最easy的shell腳本誕生了。了解更多的吧辐烂。
定義變量
myfirstshell="hello shell"
Num=1
這里有個(gè)坑遏插,定義變量的等于號=前后不能有空格,如果在python語法習(xí)慣留空格的話這里要注意纠修!
訪問變量
myfirstshell="hello shell"
num=1
echo $myfirstshell # 帶$
echo $num
echo myfirstshell # 不帶$
echo num
這里有個(gè)注意點(diǎn)胳嘲,訪問變量必須前綴使用$符號,否則輸出的會是變量名而不是變量值扣草!如下圖所示:
加減乘除運(yùn)算
運(yùn)算名稱 | shell對應(yīng)符號 |
---|---|
加號 | + |
減號 | - |
乘號 | \* |
除號 | / |
注意幾點(diǎn):
乘號中的\是轉(zhuǎn)義符了牛,必須加上。
運(yùn)算符前后必須有空格這點(diǎn)和定義變量相反辰妙!
shell中可執(zhí)行的代碼中需要加上``符號鹰祸,這個(gè)符號在Tab鍵上方。
expr用于加在表達(dá)式前執(zhí)行表達(dá)式密浑,expr和執(zhí)行內(nèi)容中間必須有空格
案例如下所示:
a=1
b=2
c=3
res=`expr $a + $b + $c`
echo "加法結(jié)果為:$res"
res=`expr $a - $b - $c`
echo "減法結(jié)果為:$res"
res=`expr $a \* $b \* $c`
echo "乘法結(jié)果為:$res"
res=`expr $c / $a / $b`
echo "除法結(jié)果為:$res"
http://note.youdao.com/favicon.ico)
與或非蛙婴、求余、相等運(yùn)算
運(yùn)算名稱 | shell符號 |
---|---|
與 | -a |
或 | -o |
非 | ! |
求余 | % |
不相等 | != |
賦值 | = |
相等 | == |
示例:
a=1
b=2
res=`expr $a % $b` #取余運(yùn)算
echo "取余結(jié)果為$res"
b=3 # 重新賦值
echo "變量b重新賦值為了3-->$b"
if [ $a == $b ] # 相等運(yùn)算
then
echo "變量a與b相等"
elif [ $a != $b ] # 不相等運(yùn)算
then
echo "變量a與b不相等"
fi
http://note.youdao.com/favicon.ico)
關(guān)系運(yùn)算
運(yùn)算名稱 | shell符號 |
---|---|
-eq | 兩個(gè)數(shù)相等返回true |
-ne | 兩個(gè)數(shù)不相等返回true |
-gt | 左側(cè)數(shù)小于右側(cè)返回true |
-lt | 左側(cè)數(shù)大于右側(cè)數(shù)返回true |
-ge | 左側(cè)大于等于右側(cè)返回true |
-lt | 左側(cè)小于等于右側(cè)返回true |
示例
a=1
b=1
if [ $a -eq $b ] # 兩個(gè)數(shù)相等返回true
then
echo "-eq兩個(gè)數(shù)相等返回true"
fi
b=2 #b賦值為2
if [ $a -ne $b ] # 兩個(gè)數(shù)不相等返回true
then
echo "-ne兩個(gè)數(shù)不相等返回true"
fi
a=3 # a賦值為3
if [ $a -gt $b ]
then
echo "-gt左側(cè)數(shù)大于右側(cè)數(shù)返回true"
fi
b=4
if [ $a -lt $b ]
then
echo "-lt左側(cè)數(shù)小于右側(cè)數(shù)返回true"
fi
a=5
if [ $a -ge $b ]
then
echo "-ge左側(cè)數(shù)大于等于右側(cè)數(shù)返回true"
fi
b=5
if [ $a -le $b ]
then
echo "-le左側(cè)數(shù)小于等于右側(cè)數(shù)返回true"
fi
字符串運(yùn)算
shell符號 | 運(yùn)算符含義 |
---|---|
= | 兩個(gè)字符串相等返回true |
-z | 字符串長度為0返回true |
-n | 字符串長度不為0true |
!= | 兩個(gè)字符串不相等返回true |
str1="hello shell"
str2="hello shell"
if [ "$str1=$str2" ]
then
echo "字符串相等返回true"
fi
str2="hello Python" # 這里把str2變量重新賦值
if [ "str1 != str2" ]
then
echo "字符串不相等返回true"
fi
str1=""
if [ "$str1 -z" ]
then
echo "字符串長度為0返回true"
fi
st1="hello java"
if [ "str1 -n" ]
then
echo "字符串長度不為0返回true"
fi
這里有個(gè)注意點(diǎn):if [ "str1 -n" ]中 的字符串運(yùn)算表達(dá)式必須加上雙引號尔破,否則會提示operator expected的錯(cuò)誤
文件運(yùn)算
shell符號 | 運(yùn)算含義 |
---|---|
-d file | 檢測文件是否是目錄街图,是浇衬,返回true |
-r file | 檢測文件是否可讀,是餐济,返回true |
-w file | 檢測文件是否可寫耘擂,是,返回true |
-x file | 檢測文件是否可執(zhí)行絮姆,是梳星,返回true |
-s file | 檢測文件是否為空,文件大小是否大于0滚朵,不為空返回true |
-e file | 檢測文件(或目錄)是否存在冤灾,是,返回true |
示例:
filename="C:\Users\Administrator\Desktop\yangs"
if [ -d "$filename" ]
then
echo "-d file有目錄辕近,返回true"
fi
if [ -r "$filename" ]
then
echo "-f file文件可讀,返回true"
fi
if [ -w "$filename" ]
then
echo "-w file文件可寫韵吨,返回true"
fi
if [ -x "$filename" ]
then
echo "-x file文件可執(zhí)行,返回true"
fi
filename="C:\Users\Administrator\Desktop\yangsc"
if [ -s "$filename" ]
then
echo "-f file文件不為空移宅,返回true"
else:
echo "file 為空"
fi
if [ -e "$filename" ]
then
echo "-e file存在归粉,返回true"
else
echo "file文件不存在"
fi
這里需要注意的是:
文件路徑作為字符串變量名,if [ -e "$filename" ]必須在if判斷中加上雙引號
另外一個(gè)技巧是shell的報(bào)錯(cuò)行數(shù)一般是前后2行漏峰,而不一定是報(bào)錯(cuò)的那會那行
字符串拼接及長度
方法及說明 | shell語法 |
---|---|
拼接1 | str1= |
拼接2 | str2=" |
拼接3引號間可加內(nèi)容 | str3= |
拼接4 | str4=" |
長度 | ${#Val} |
切片 | ${Val:1:2} |
這里拼接只示例第3種方式:
str1="I"
str2="shell"
str3=$str1"am"$str2 # 拼接
echo $str3
echo ${#str3} # 獲取長度
echo ${str3:1:2} # 獲取切片字符
數(shù)組
方法名稱 | shell代碼 |
---|---|
定義數(shù)組 | array=(6 7 8) |
獲取下標(biāo) | ${array[2]} |
變量引用下標(biāo)值 | Var=${array[2]} |
獲取數(shù)組長度 | length=${#array[*]} |
array1=(6 7 8)
echo "這是數(shù)組"
index=${array1[2]}
echo "這是數(shù)組的第二個(gè)下標(biāo)的值--->$index"
length=${#array1[*]}
echo "這是數(shù)組的長度--->$length"
這里的注意點(diǎn)是獲取長度和獲取下標(biāo){}大括號別忘了
輸出程序
方法名稱 | shell代碼 |
---|---|
換行輸出 | echo "str1 \nstr2" |
重定向至指定文件 | echo "hello shell" > xx.txt |
注意點(diǎn):
- \n后面不能有空格糠悼,否則輸出會有空格
示例:
echo -e "hello \nshell" # 換行輸出
echo "hello shell" > shell2.txt # 重定向
echo `date` # 輸出系統(tǒng)實(shí)時(shí)時(shí)間
printf函數(shù)
打印功能,C語言的printf()類似
使用printf的腳本比echo更好浅乔,不會換行倔喂,所以需要手動(dòng)添加\n
示例:
printf "%-10s %-8s %-4s\n" 姓名 性別 體重kg
printf "%-10s %-8s %-4.2f\n" 阿米 男 67.123
printf "%-10s %-8s %-4.2f\n" 啊超 男 78.6543
printf "%-10s %-8s %-4.2f\n" 阿福 男 48.9876
判斷語句
- if
- if-else
- if-elif
- case
a=1
b=2
c=1
if [ $a == $b ]
then
echo "a和b相等"
elif [ $a != $c ]
then
echo "a和c不相等"
else
echo "類型不同"
fi
注意點(diǎn):
- if=elif-else中只要有一個(gè)條件成立其他的都不會執(zhí)行
- []方括號前后需要空格,否則格式報(bào)錯(cuò)
- if和elif后面都需要加then如果寫在一行還需要加標(biāo)點(diǎn)
- else后沒有引號
- 語句結(jié)束必須加fi
- case語句和If語句使用類似靖苇,只能判斷一種條件席噩,有興趣可以搜索相關(guān)資料,這里不作介紹
for循環(huán)
格式:
for i in data/str/array/file
do
echo $i
done
示例
data="h e l lo shell"
for str in $data # data為字符串時(shí)贤壁,輸出結(jié)果以空格界定單個(gè)元素
do
echo $str
done
#data=(1 2 3) # 錯(cuò)誤寫法
#for i in 1 2 3
for i in 1 2 3 # 數(shù)組循環(huán)時(shí)不能在數(shù)組外加括號悼枢,并且不能用變量的形式表示數(shù)組
do
echo $i
done
break跳出循環(huán)
名稱 | shell代碼 |
---|---|
跳出所有循環(huán) | break |
跳出第N層f循環(huán) | break n |
跳出當(dāng)前循環(huán) | continue |
for i in 1 2 3
do
if [ $i == 3 ]
then
break # 當(dāng)循環(huán)到3時(shí),跳出循環(huán)脾拆,3不會被輸出
fi
echo $i
done
函數(shù)
無參數(shù)函數(shù)
test(){ # 無參數(shù)無返回值函數(shù)
echo "無參數(shù)函數(shù)hello shell"
}
test
test1(){ #無參數(shù)有返回值
a=1
return $a
}
test1
res=$?
echo $res
test2(){ # 有參數(shù)有返回值函數(shù)
echo $1
echo $2
}
test2 2 3
重定向
#!/bin/bash
$echo res > file #將結(jié)果寫入文件馒索,結(jié)果不會在控制臺展示,而是在文件中名船,覆蓋寫
$echo res >> file #將結(jié)果寫入文件绰上,結(jié)果不會在控制臺展示,而是在文件中包帚,追加寫
echo input < file #獲取輸入流
IO
名稱 | 代碼符號 | 句柄符號 |
---|---|---|
標(biāo)準(zhǔn)輸入 | stdin | 0 |
標(biāo)準(zhǔn)輸出 | stdout | 1 |
stderr | stderr | 2 |
一個(gè)自動(dòng)執(zhí)行python的腳本
博主編寫了這個(gè)腳本用來跑自己寫的接口自動(dòng)化框架渔期,每執(zhí)行一次框架都會先清空日志,再將本次的執(zhí)行日志全部輸出到bash終端界面
# 每次啟動(dòng)接口自動(dòng)化框架前清空日志文件,并將最新的日志輸出到bash終端
startpy(){
for i in 1
do
cd ~
cd d:
cd untitled
cd untitled
cd shopvinterface
cd Report
true > logs
cd ..
cd Run
python RunCase.py
cd ..
cd Report
cat logs
done
}
startpy
效果是不是很酷疯趟!自從會了shell腳本拘哨,媽媽再也不用擔(dān)心我要加班了。
文章部分有引用此文http://www.reibang.com/p/71cb62f08768
及菜鳥教程