shell腳本學習一(shell基本語法)

 

1、前提

編寫shell腳本的時候,最前面要加上一行:

#!/bin/bash

因爲linux裏面不僅僅只有bash一個解析器,還有其它的,它們之間的語法會有一些不同,所以最好加上這一句話,告訴系統要用這個解析器。

2、shell變量

shell的變量賦值的時候不用帶“$”,而使用或者輸出的時候要帶“$”。加減乘除的時候要加兩層小括號。括號外面要有一個“$”,括號裏面的變量可以不用“$”。需要注意的是,變量賦值,變量使用的時候不能有空格,否則會被解析成命令,報錯無此命令。

列子:

#!/bin/bash
echo "hello shell" #echo 是打印命令
a=5
b="2019.8.20"   #shell腳本中只有int和char兩種數據類型
c=3
sum=$((a+c))
echo $b
echo "a+c="$sum
d=${#b}     #計算字符串b的長度
echo "2019.8.20 length is"$d
echo ${b:5} #截取從第五個後面開始到最後的字符
echo ${b:5:2}   #截取從第五個後面開始的2個字符
echo ${b#*9}    #從開頭刪除9的字符
echo ${b##2*}   #刪除全部
echo ${b%8*0}   #從結尾0刪除到8之間的字符
echo ${b%%*0}   #全部刪除

3、shell測試判斷test或[]

#!/bin/bash
read filename
test -x $filename && echo "the file can executable" || echo "the file can not executable"
test -f $filename && echo "the file is ordinary file" || echo "the file is not ordinary file"
test -d $filename && echo "the file is document folder" || echo "the file is not document folder"
test -r $filename && echo "the file can read" || echo "the file can not read"
test -w $filename && echo "the file can write" || echo "the file can not write"
​
echo "Please input two numbers:"
read num1
read num2
​
echo "num1 = "${num1}
echo "num2 = "${num2}
echo "by test\n"
test $num1 -eq $num2 && echo "num1 == num2" || echo "num1 != num2"
test $num1 -ne $num2 && echo "num1 != num2" || echo "num1 == num2"
test $num1 -gt $num2 && echo "num1 > num2" || echo "num1 <= num2"
test $num1 -lt $num2 && echo "num1 < num2" || echo "num1 >= num2"
test $num1 -ge $num2 && echo "num1 >= num2" || echo "num1 < num2"
test $num1 -le $num2 && echo "num1 <= num2" || echo "num1 > num2"
​
echo "by []\n"
[ $num1 -eq $num2 ] && echo "num1 == num2" || echo "num1 != num2"
[ $num1 -ne $num2 ] && echo "num1 != num2" || echo "num1 == num2"
[ $num1 -gt $num2 ] && echo "num1 > num2" || echo "num1 <= num2"
[ $num1 -lt $num2 ] && echo "num1 < num2" || echo "num1 >= num2"
[ $num1 -ge $num2 ] && echo "num1 >= num2" || echo "num1 < num2"
[ $num1 -le $num2 ] && echo "num1 <= num2" || echo "num1 > num2"

需要注意的是使用[]的時候必須要每個變量之間都要有空格,和左右中括號也要有空格,否則報錯。

四、shell條件分支結構語句

1.單分支判斷語句

格式:if 條件 ; then 結果 fi ,最後面一定要有fi,在shell腳本里面,控制分支結構結束都要和開頭的單詞相反,例如,if <–> fi,case <–> esac。

2.雙分支判斷語句

read num1
read num2
########################################
if [ $num1 -eq $num2 ] ;then
    echo $num1"="$num2
fi
###########################################
if [ $num1 -eq $num2 ] ;then
    echo $num1"="$num2
else
    echo $num1"!="$num2
fi
​
#########################################

3.多分支判斷語句

多分支判斷有兩種,和C語言的一樣 if else if,case。只是形式上有一些不同。

#!/bin/bash
​
echo "Please input your math grades"
read grades
​
​
if [ $grades -ge 90 ] && [ $grades -le 100 ];then
echo "Your grade is excellent."
elif [ $grades -ge 80 ] && [ $grades -le 89 ];then
echo "Your grade is good."
elif [ $grades -ge 70 ] && [ $grades -le 79 ];then
echo "Your grade is middle."
elif [ $grades -ge 60 ] && [ $grades -le 69 ];then
echo "Your grade is passing."
else
echo "Your grade is badly."
fi

 

#!/bin/bash
​
echo "Please input a command"
read cmd
case $cmd in
cpu)    echo "The cpu information is"
        cat  /proc/cpuinfo;;
mem)    echo "The mem information is"
        cat /proc/meminfo;;
device) echo "The device information is"
        cat /proc/scsi/device_info;;
CD-ROM) echo "The CD-ROM information is"
        cat /proc/sys/dev/cdrom/info;;
*)      echo "Your input command is invalid"
esac

五、shell循環語句

1.while語句

while語句是隻要條件爲真就執行下面語句。 格式: while 條件 do 語句 done

需要注意的是,這裏的條件除了 while true 可以這樣寫,其它的條件都要用 test或者 []來判斷

#!/bin/bash
​
i=$1
while [ $i -gt 0 ]
do
echo $i
((i--))
done

$0:表示文件名

$1:表示文件名後的第一個參數

$2:表示第二個,以此類推。

終端輸入:

./hello.sh 6

2.until語句

until語句是隻要條件爲假就執行下列語句 格式: until 條件 do 語句 done

3.for語句

格式: for 變量 in 列表 #這個和Python中的for用法類似 do 語句 done

六、shell函數

格式: [function] funcName() { 語句 [return 返回值] } 返回值是可選的,如果沒有顯示return 則默認返回最後一條語句執行的結果。

Shell 函數返回值只能是整數,一般用來表示函數執行成功與否,0表示成功,其他值表示失敗

#!/bin/bash
​
add()
{
   c=$((a+b))
   echo $c
}
​
a=2
b=7
add a b

七、總結

shell的基本語法就介紹這麼多,光學這些是沒辦法看懂別人寫的腳本的,當然也沒辦法自己寫法能夠實現特定功能的腳本,如給Ubuntu電腦裝企業微信的腳本。因此 ,還需要學習shell的命令,shell命令是可以在腳本中直接生效的,如echo、rm、pwd等。

所以學習路線應該是這樣的:

shell語法、shell命令、編寫思路(多看別人寫的實例)。

因爲本人也是剛接觸,所以會有說的不對的地方,望指正。

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