一、shell腳本傳參
4.1.shell文件中輸入
echo $1 $2 $3
su test.sh one two three
打印出對應(yīng)輸入的$1 $2 $3
參數(shù)one two three
也可以使用args數(shù)組方式存儲(chǔ)參數(shù)
args=("$@")
echo ${args[0]} ${args[1]} ${args[2]}
還有簡潔的方式
args=("$@")
#echo ${args[0]} ${args[1]} ${args[2]}
echo $@
二补履、if-else語句
在shell中的if控制語句很像python中的語
- if -then
if condition
then
command1
command2
...
commandN
fi
2.if--else語句
if condition
then
command1
command2
...
commandN
else
command
fi
例如:
echo -e "Enter the name of file: \c"
read filename
if [ -e $filename ]#檢查文件是否存在
then
echo "File found"
else
echo "File is not exist or not found"
fi
echo -e "Enter the name of file: \c"
read filename
if [ -f $filename ]#檢查文件是常規(guī)文件
then
echo "File found"
else
echo "File is not exist or not found"
fi
echo -e "Enter the name of file: \c"
read filename
if [ -d $filename ]#-d表示directory,判斷是否是一個(gè)目錄
then
echo "File found"
else
echo "File is not exist or not found"
fi
注意 -e表示是否存在
-f表示file朱躺,判斷是否是常規(guī)的文件
-d表示directory,判斷是否是一個(gè)目錄搁痛。
-e表示empty长搀,默認(rèn)文件是不為空。
3.if-then-elif-then-else
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
例如:
a=10
b=20
if [ $a == $b ]
then
echo "a == b"
elif [ $a -gt $b ]
then
echo "a > b"
elif [ $a -lt $b ]
then
echo "a < b"
else
echo "Ineligible"
fi
4.利用if在文件尾部寫入內(nèi)容
首先要判斷是不是常規(guī)文件落追,如果是盈滴,在判斷是否有寫的權(quán)限,如果有寫得權(quán)限需要輸入轿钠,并不輸入的內(nèi)容保持到文件的維度
上面可以看出這里需要嵌套if巢钓,
文件寫入需要用到cat命令 cat > file 就是覆蓋 cat >> file就是不覆蓋,直接在后面寫入新內(nèi)容
echo -e "echo -e "Enter the name of file: \c"
read filename
if [ -f $filename ]#判斷文件是否存在
then
if [-w $filename ]#判斷寫權(quán)限
then
echo "type some test date. press ctrl+d to quit."
cat >> $filename
else
echo "$filename do not have write permissiong"
fi
else
echo "$filename is not exit"
fi
2.for循環(huán)
循環(huán)一般格式為:
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
dovimne
例如:
for str in This is a string
do
echo $str
done
還有一種
for (( 表達(dá)式1疗垛; 表達(dá)式2症汹;表達(dá)式3))
do
command1
command2
command3
done
例如
t=0
for ((i=1; i<=100; i++ ))
do
t=$(( t +i ))
done
echo $t
使用sh filename.tes這里會(huì)發(fā)現(xiàn)Syntax error: Bad for loop variable
因?yàn)閟h 命令執(zhí)行腳本的時(shí)候?qū)嶋H使用的是 dash,而 dash 不支持這種 C 語言格式的 for 循環(huán)寫法贷腕。
所以背镇,使用 bash 代替 sh 運(yùn)行腳本 bash filename.tes
3.while語句
1.常見的while語句格式
while condition
do
command
done
例如
#!/bin/bash
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
無線循環(huán)
while :
do
command
done
或者
while true
do
command
done
或者前面學(xué)到的for
for (( ; ; ))
4.until循環(huán),until 循環(huán)執(zhí)行一系列命令直至條件為真時(shí)停止
格式
until condition
do
command
done
例如:打印1到9
n=1
until [ $n -ge 10 ]
do
echo "$n"
(( n++ ))
done
5.case
case 語句匹配一個(gè)值與一個(gè)模式,如果匹配成功泽裳,執(zhí)行相匹配的命令瞒斩。case 語句格式如:
case 值 in
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
esac
注意:取值后面必須為單詞 in,每一模式必須以右括號(hào)結(jié)束涮总。取值可以為變量或常數(shù)胸囱。匹配發(fā)現(xiàn)取值符合某一模式后,其間所有命令開始執(zhí)行直至 ;;瀑梗。
取值將檢測匹配的每一個(gè)模式烹笔。一旦模式匹配裳扯,則執(zhí)行完匹配模式相應(yīng)命令后不再繼續(xù)其他模式。如果無一匹配模式谤职,使用星號(hào) * 捕獲該值饰豺,再執(zhí)行后面的命令。
例如:
echo 'Enter a number between 1 and 4:'
echo 'The number you entered is:'
read aNum
case $aNum in
1) echo 'You have chosen 1'
;;
2) echo 'You have chosen 2'
;;
3) echo 'You have chosen 3'
;;
4) echo 'You have chosen 4'
;;
*) echo 'You did not enter a number between 1 and 4'
;;
esac
6.跳槽循環(huán)
break 和 continue
break 命令允許跳出所有循環(huán)(終止執(zhí)行后面的所有循環(huán))
例如
while :
do
echo -n "Enter a number between 1 and 5:"
read aNum
case $aNum in
1|2|3|4|5) echo "The number you entered is $aNum!"
;;
*) echo "The number you entered is not between 1 and 5! game over!"
break
;;
esac
done
continue 命令與 break 命令類似允蜈,只有一點(diǎn)差別冤吨,它不會(huì)跳出所有循環(huán),只跳出當(dāng)前循環(huán)陷寝。
while :
do
echo -n "Enter a number between 1 and 5: "
read aNum
case $aNum in
1|2|3|4|5) echo "The number you entered is $aNum!"
;;
*) echo "The number you entered is not between 1 and 5!"
continue
echo "game over"
;;
esac
done
三锅很、數(shù)組
arr=('Alice' 'Jone' 'Jack' 'Lili')
arr[4]='Alex'#添加元素
echo "${arr[@]}"#遍歷數(shù)組
echo "${arr[1]}"#單個(gè)制定位置的元素
echo "${!arr[@]}"#數(shù)組的索引查詢
echo "${#arr[@]}"#數(shù)組的長度
unset arr[1]# 刪除數(shù)組