shell腳本語句語法使用(超詳細)

博文大綱:shell

  • 一、對談式腳本——read語句
  • 二、shell腳本中的測試的字符
  • 三、判斷腳本舉例
  • 四、條件判斷——if語句
  • 五、條件判斷——case語句
  • 六、條件判斷——function函數結合case語句
  • 七、循環判斷——while、until語句
  • 八、固定循環——for...do...done語句
  • 九、循環——cut、set結合for語句
  • 十、其餘腳本類型舉例
  • 十一、shell腳本的追蹤與debug

shell腳本的編寫越規範越好,那麼一般在每一個shell腳本的開頭幾行會有以下幾個方面相關的註釋信息:vim

  • 腳本使用的shell,如/bin/bash;
  • 腳本的版本信息;
  • 腳本的做者與聯絡方式;
  • 腳本的history(編寫時間記錄);
  • 腳本內較特殊的指令,使用絕對路徑的方式來下達;
  • 腳本運行是須要的環境變量預先宣告與設置。

廢話很少說,直接上使用語法案例:bash

一、對談式腳本——read語句

shell變量除了能夠直接賦值或腳本傳參外,還可使用read命令從標準輸入中得到,read是bash內置命令,可使用help read查看幫助。ide

read的讀入功能就至關於交互式接受用戶輸入,而後給變量賦值。函數

經常使用參數以下:測試

  • -p: 設置提示信息。
  • -t:設置輸入等待的時間,單位默認是秒。

使用舉例
基本使用:ui

[root@localhost ~]# read -t 10 -p "qing shu ru:" num #讀入一個輸入,賦值給num,num變量前要空格。
# -t  10則表示10秒後就超時退出
qing shu ru:34      #輸入34
[root@localhost ~]# echo ${num}      #輸出變量值
34
[root@localhost ~]# read -p "qing shu ru:" a1 a2    #也能夠一次定義兩個變量
qing shu ru:23 45
[root@localhost ~]# echo ${a1} ${a2}
23 45

需求①:
first name 與 2. last name,最後而且在屏幕上顯示:「Your full name is: 」的內容:this

[root@localhost ~]# vim 1.sh   #編輯腳本,內容以下

#!/bin/bash
echo -e "yong lai xian shi wen jian full name:\n"
read -p "qing shu ru fir filename:" firname
read -p "qing shu ru sec filename:" secname
echo -e "\nyour full name is ${firname}${secname}."
#其中echo後面的「\n」表示換行
[root@localhost ~]# sh 1.sh      #執行腳本
yong lai xian shi wen jian full name:

qing shu ru fir filename:xie         #手動輸入文件名開頭
qing shu ru sec filename:feng         #手動輸入文件名結尾

your full name is xiefeng.     #它將自動將開頭和結尾結合起來並輸出

需求②:
假設我想要建立三個空的文件(經過touch),filename最開頭由當前用戶輸入決定,假設使用者輸入 filename 好了,那今天的日期是 2021/03/29 ,我想要之前天、昨天、今天的日期來建立這些文件。debug

[root@localhost ~]# vim 2.sh     #編輯腳本

#!/bin/bash
echo -e "yi ci chuang jian san ge file.\n"
read -p "qing shu ru filename:" filename
filename=${filename:-file}
date1=$(date --date '1 days ago' +%Y%m%d)
date2=$(date --date '2 days ago' +%Y%m%d)
date3=$(date +%Y%m%d)
file1="${filename}${date1}"
file2="${filename}${date2}"
file3="${filename}${date3}"
touch "${file1}"
touch "${file2}"
touch "${file3}"
[root@localhost ~]# sh 2.sh   #執行腳本
yi ci chuang jian san ge file.

qing shu ru filename:xie           #輸入自定義的文件名開頭

[root@localhost ~]# find /root -name "xie*"   #查看是否建立成功
/root/lv20210329
/root/lv20210331
/root/lv20210330

需求③:
若是咱們要使用者輸入兩個變量,而後將兩個變量的內容相乘,最後輸出相乘的結果。3d

[root@localhost ~]# vim 3.sh      #編輯腳本

#!/bin/bash
echo -e "\nzhe shi yi ge suan cheng fa de jiao ben:\n"
read -p "qing shu ru yi ge shu zi:" A
read -p "qing shu ru di er ge shu zi:" B
sum=`echo "scale=4; ${A} * ${B}" | bc`
echo -e "\n${A}x${B} ==> ${sum}."
[root@localhost ~]# sh 3.sh        #執行腳本

zhe shi yi ge suan cheng fa de jiao ben:

qing shu ru yi ge shu zi:3        #輸入第一個數
qing shu ru di er ge shu zi:4     #輸入第二個數

3x4 ==> 12.                           #輸出的結果

二、shell腳本中的測試的字符

shell腳本語句語法使用(超詳細)
shell腳本語句語法使用(超詳細)
shell腳本語句語法使用(超詳細)
shell腳本語句語法使用(超詳細)
上面全部的測試都是經過test進行的,可使用「[ ]」來代替,將要測試的類型及指定的名字寫在「[ ]」 便可,可是中括號裏面兩邊必須有空格。(推薦使用中括號「[ ]」)

三、判斷腳本舉例

需求①:

  1. 這個文件是否存在,若不存在則給予一個「Filename does not exist」的訊息,並中斷程序;
  2. 若這個文件存在,則判斷他是個文件或目錄,結果輸出「Filename is regular file」或「Filename is directory」
  3. 判斷一下,執行者的身份對這個文件或目錄所擁有的權限,並輸出權限數據!
[root@localhost ~]# vim 4.sh     #編輯腳本

#!/bin/bash
echo "yong lai ce shi wen jian huo dirctory."
read -p "qing shu ru yi ge wen jian ming:" filename
test -z ${filename} && echo -e "\nqing shu ru yi ge filename." && exit 0
test ! -e ${filename} && echo "filename does not exitst." && exit 0
test -f ${filename} && filetype="file"
test -d ${filename} && filetype="directory"
test -r ${filename} && prem="read"
test -w ${filename} && prem="${prem}+write"
test -x ${filename} && prem="${prem}+exe"
echo -e "\nthis is a ${filetype},it's perm.. is ${prem}."
[root@localhost ~]# sh 4.sh      #執行腳本
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/root    #輸入一個目錄名

this is a directory,it's perm.. is read+write+exe.      #腳本執行後輸出的結果
[root@localhost ~]# sh 4.sh     #再執行腳本
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/etc/passwd      #輸入一個文件

this is a file,it's perm.. is read+write.       #腳本執行後輸出的結果

需求②:

一、當執行一個程序的時候,這個程序會讓用戶輸入Y或N。
二、若是使用者輸入Y或y時,就會顯示OK,continue.
三、若是使用者輸入N或n時,就會顯示ON,interrupt.
四、若是不是Y/y/N/n以內的字符,那麼將會死循環這個腳本,直到手動退出,或輸入正確的值(其實稍做改動,能夠改成若默認按回車的話能夠等於輸入「Y」,自行研究吧)。

[root@localhost ~]# vim 5.sh     #編輯腳本

#!/bin/bash
while [ "${yn}" != "Y" -o "${yn}" != "y" -o "${yn}" != "N" -o "${yn}" != "n" ]
do
read -p "qing shu ru 'Y' or 'N':" yn
[ "${yn}" == "Y" -o "${yn}" == "y" -o "${yn}" == "" ] && echo -e "\nOK,continue." && exit 0
[ "${yn}" == "N" -o "${yn}" == "n" ] && echo -e "\nON,interrupt." && exit 0
done
[root@localhost ~]# sh 5.sh     #下面是屢次執行腳本,測試是否達到需求
qing shu ru 'Y' or 'N':

OK,continue.
[root@localhost ~]# sh 5.sh 
qing shu ru 'Y' or 'N':y

OK,continue.
[root@localhost ~]# sh 5.sh
qing shu ru 'Y' or 'N':n

ON,interrupt.
[root@localhost ~]# sh 5.sh
qing shu ru 'Y' or 'N':u
qing shu ru 'Y' or 'N':i
qing shu ru 'Y' or 'N':N

ON,interrupt.

需求③:
一、程序的文件名爲什麼?
二、共有幾個參數?
三、若參數的個數小於 2 則告知使用者參數數量太少
四、所有的參數內容爲什麼?
五、第一個參數爲什麼?
六、第二個參數爲什麼

[root@localhost ~]# vim 6.sh     #編輯腳本以下

#!/bin/bash
echo -e "\ncheng xu de wen jian ming shi ${0}"
echo -e "\nyi gong you $# ge can shu."
[ $# -lt 2 ] && echo "can shu tai shao le ." && exit 0
echo  "your whole parameter is ==> '$*'."
echo "the 1st parameter ${1}."
echo "the 2nd parameter ${2}."
[root@localhost ~]# sh 6.sh a b c     #執行腳本

cheng xu de wen jian ming shi 6.sh

yi gong you 3 ge can shu.
your whole parameter is ==> 'a b c'.
the 1st parameter a.
the 2nd parameter b.
[root@localhost ~]# sh 6.sh a      #再次執行腳本

cheng xu de wen jian ming shi 6.sh

yi gong you 1 ge can shu.
can shu tai shao le .
#爲了避免爲難本身,上面我用了拼音,多多體諒[ 捂臉 ]。

需求④:
查看本機都是否開啓了www / ftp / mail服務,並將結果直觀的顯示出來

[root@localhost ~]# vim 11.sh 

#!/bin/bash
file="/dev/shm/a.txt"
netstat -anpt  > ${file}
awk -F : '{print $4}' ${file} | awk '{print $1}' | grep "80" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "www service is up\n"
fi
awk '{print $4}' ${file} | egrep "20|21" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "ftp service is up\n"
fi
awk '{print $4}' ${file} | grep "25" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "mail service is up\n"
fi
[root@localhost ~]# sh 11.sh     #執行腳本測試
mail service is up

[root@localhost ~]# systemctl start httpd    #啓動www服務再測試
[root@localhost ~]# sh 11.sh 
www service is up

mail service is up

需求⑤:
都知道腳本後面的第一段是$1,第二段是$2....那麼是否能夠進行偏移呢,假設讓本來的$2變爲$1。

[root@localhost ~]# vim 7.sh      #編輯腳本以下

#!/bin/bash
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift 3
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
#「上面默認的shift」參數是偏移1個位置,也能夠指定偏移的參數,如「shift 3」則表示向後偏移三個
[root@localhost ~]# sh 7.sh a b c    #執行腳本,而且追加三個參數
total parameter number is ==> 3
your whole parameter is ==> a b c 
total parameter number is ==> 2
your whole parameter is ==> b c 
total parameter number is ==> 2
your whole parameter is ==> b c 
#從輸出結果能夠發現,偏移是累加的,第一次偏移了默認1位,
#第二次偏移了3位,那麼實際已經偏移了原始參數的4位(由於累加)
#可是參數只有三個,因此它會循環偏移,因此結果仍是b和c。

關於上面腳本中的「$#」、「$*」的解釋能夠參考以下解釋:

shell腳本語句語法使用(超詳細)

四、條件判斷——if語句

需求①:

一、當執行一個程序的時候,這個程序會讓用戶輸入Y或N。
二、若是使用者輸入Y或y或者直接按下回車鍵時,就會顯示OK,continue.
三、若是使用者輸入N或n時,就會顯示ON,interrupt.
四、若是不是Y/y/N/n以內的字符,那麼將輸出「I don't know what your choice is」

[root@localhost ~]# vim 66.sh     #編寫腳本

#!/bin/bash
read -p "Please input (Y/N): " yn
if [ "${yn}" == "Y" -o "${yn}" == "y" -o "${yn}" == "" ];
        then
           echo "OK, continue" 
           exit 0
elif [ "${yn}" == "N" -o "${yn}" == "n" ];
         then
           echo "ON, interrupt!" 
           exit 0
        else
           echo "I don't know what your choice is"
fi
[root@localhost ~]# sh 66.sh    #屢次執行,進行測試
Please input (Y/N): 
OK, continue
[root@localhost ~]# sh 66.sh 
Please input (Y/N): n
ON, interrupt!
[root@localhost ~]# sh 66.sh 
Please input (Y/N): dd
I don't know what your choice is

需求②:

判斷192.168.1.0-10的主機存活,測試是否能夠ping通,並輸出IP地址對應的主機是「up」仍是「down」,兩種實現方法,以下:
方法1:

[root@localhost ~]# vim 8.sh     #編寫腳本

#!/bin/bash
i=0
network=192.168.1.
while [ ${i} -le 10 ]
do
          ping -c 3 -i 0.2 -w 3 ${network}${i} &> /dev/null
if [ $? -eq 0 ]
        then
          echo "host ${network}${i} is up"
        else
          echo "host ${network}${i} is down"
fi
         let i++
done
[root@localhost ~]# sh 8.sh      #執行腳本
host 192.168.1.0 is down
host 192.168.1.1 is down
host 192.168.1.2 is up
host 192.168.1.3 is down
host 192.168.1.4 is down
host 192.168.1.5 is down
host 192.168.1.6 is down
host 192.168.1.7 is down
host 192.168.1.8 is up
host 192.168.1.9 is down
host 192.168.1.10 is down

方法2:

[root@localhost ~]# vim 21.sh      #腳本內容以下

#!/bin/bash
network="192.168.1."
for host in $(seq 1 10)
do
ping -c 1 -w 1 ${network}${host} &> /dev/null
if [ $? -eq 0 ]
        then
          echo "${network}${host} is up."
else
        echo "${network}${host} is down."
fi
done
[root@localhost ~]# sh 21.sh     #執行腳本
192.168.1.1 is down.
0

需求③:

一、判斷 $1 是否爲 hello,若是是的話,就顯示 "Hello, how are you ?";
2.、若是沒有加任何參數,就提示使用者必需要使用的參數下達法;
三、 而若是加入的參數不是 hello ,就提醒使用者僅能使用 hello 爲參數。

[root@localhost ~]# vim 10.sh        #編寫腳本

#!/bin/bash
if [ "${1}" == "hello" ]
        then
          echo "hello,how are you ?"
          exit 0
elif [ "${1}" == "" ]
        then
          echo "qing shi yong zi fu 'hello'."
          exit 0
else
          echo "jin neng shi yong hello zi fu." 
fi
[root@localhost ~]# sh 10.sh              #屢次執行進行測試
qing shi yong zi fu 'hello'.
[root@localhost ~]# sh 10.sh  he
jin neng shi yong hello zi fu.
[root@localhost ~]# sh 10.sh  hello
hello,how are you ?

五、條件判斷——case語句

需求①:

一、判斷 $1 是否爲 hello,若是是的話,就顯示 "Hello, how are you ?";
2.、若是沒有加任何參數,就提示使用者必需要使用的參數下達法;
三、 而若是加入的參數不是 hello ,就提醒使用者僅能使用 hello 爲參數。

[root@localhost ~]# vim 12.sh        #編寫腳本

#!/bin/bash
case ${1} in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> {${0} someword}" 
;;
*)
echo "Usage ${0} {hello}" 
;;
esac
[root@localhost ~]# sh 12.sh hello     #屢次執行,進行測試
Hello, how are you ?
[root@localhost ~]# sh 12.sh hell
Usage 12.sh {hello}
[root@localhost ~]# sh 12.sh 
You MUST input parameters, ex> {12.sh someword}

需求②:
讓使用者可以輸入 one, two, three ,而且將使用者的變量顯示到屏幕上,若是不是 one, two, three 時,就告知使用者僅有這三種選擇。

[root@localhost ~]# vim 13.sh      #編寫腳本

#!/bin/bash
echo "qing shu ry one two three."
read -p "qing shu ru:" shuzi
case $shuzi in
"one")
        echo "bian liang shi $shuzi"
;;
"two")
        echo "bian liang shi $shuzi"
;;
"three")
        echo "bian liang shi $shuzi"
;;
*)
        echo "only neng shu ru one|two|three"
;;
esac
[root@localhost ~]# sh 13.sh       #屢次執行,進行測試
qing shu ry one two three.
qing shu ru:one           #根據提示輸入字符
bian liang shi one
[root@localhost ~]# sh 13.sh    #屢次執行,進行測試
qing shu ry one two three.
qing shu ru:two         #根據提示輸入字符
bian liang shi two
[root@localhost ~]# sh 13.sh     #屢次執行,進行測試
qing shu ry one two three.
qing shu ru:ddd         #根據提示輸入字符
only neng shu ru one|two|three

六、條件判斷——function函數結合case語句

需求②:
讓使用者可以輸入 one, two, three ,而且將使用者的變量以大寫的方式顯示到屏幕上,若是不是 one, two, three 時,就告知使用者僅有這三種選擇。

[root@localhost ~]# vim 14.sh 

function aaa(){
        echo -n  "this is  "
}
case $1 in
one)
aaa
        echo "$1." | tr 'a-z' 'A-Z'
;;
two)
aaa
        echo "$1." | tr 'a-z' 'A-Z'
;;
three)
aaa
        echo "$1." | tr 'a-z' 'A-Z'
;;
*)
        echo "qing shu ru {one|two|three}."
;;
esac
[root@localhost ~]# sh 14.sh     #執行進行測試
qing shu ru {one|two|three}.
[root@localhost ~]# sh 14.sh one     #再次執行
this is  ONE.

關於function函數的解釋舉例:
假以下達:「 sh show123-2.sh one 」 這表示在 腳本內的 $1 爲 "one" 這個字串。可是在 printit()內的 $1 則與這個 one 無關。

以下:

[root@localhost ~]# vim 15.sh 

#!/bin/bash
function printit(){
        echo "Your choice is ${1}" 
}
case ${1} in
"one")
printit 1 11
;;
"two")
printit 2 22
;;
"three")
printit 3 33
;;
*)
echo "Usage ${0} {one|two|three}" 
;;
esac
[root@localhost ~]# sh 15.sh one    #屢次執行進行測試,以便查看輸出的$1是哪裏的值
Your choice is 1
[root@localhost ~]# sh 15.sh two
Your choice is 2
#若將下面的變量改成$2,那麼將會輸出十一、2二、33等,發現這裏的${變量}是引用的哪裏的了麼?
function printit(){
        echo "Your choice is ${2}" 
}
#自行測試吧!

七、循環判斷——while、until語句

需求①——while語句
假設我要讓使用者輸入 yes 或者是 YES 才結束程序的執行,不然就一直進行告知使用者輸入字串。

[root@localhost ~]# vim 16.sh     #編寫腳本

#!/bin/bash
while [ "${yn}" != "yes" -a "${yn}" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[root@localhost ~]# sh 16.sh      #執行腳本測試
Please input yes/YES to stop this program: yes
OK! you input the correct answer.
[root@localhost ~]# sh 16.sh      #執行腳本測試
Please input yes/YES to stop this program: dd
Please input yes/YES to stop this program: f
Please input yes/YES to stop this program: YES
OK! you input the correct answer.

需求②——until語句
與上面的需求同樣,只是換成until語句。

[root@localhost ~]# vim 17.sh      #編寫腳本以下

#!/bin/bash
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
        read -p "qing shu ru 'yes' or 'YES':" yn
done
echo "OK!!!"
[root@localhost ~]# sh 17.sh     #屢次執行進行測試
qing shu ru 'yes' or 'YES':yes
OK!!!
[root@localhost ~]# sh 17.sh      #屢次執行進行測試
qing shu ru 'yes' or 'YES':df
qing shu ru 'yes' or 'YES':YES
OK!!!
#不難發現,until與while正好相反,until是某個測試條件成立,則不循環;
#while是若是某個測試條件成立,則循環

需求③
求1到100的累積相加的和。
這個題目仍是比較經典又簡單的,以下(實現的方法有不少,自行研究吧):
方法1:

[root@localhost ~]# vim 18.sh     #編輯腳本

#!/bin/bash
i=0
sum=0
while [ ${i} -lt 100 ]
do
        i=$(( ${i}+1 ))
        sum=$(( ${sum}+${i} ))
done
echo "1+2+3+4....+100 de jie guo shi ${sum}."
[root@localhost ~]# sh 18.sh     #執行腳本
1+2+3+4....+100 de jie guo shi 5050.       #5050就是結果

方法2:

[root@localhost ~]# vim 18.sh      #腳本內容以下

#!/bin/bash
i=0
while [ ${i} -le 100 ]
do
        sum=$(( ${sum}+${i} ))
        let i++
done
echo "1+2+3+4....+100 de jie guo shi ${sum}."
[root@localhost ~]# sh 18.sh      #執行腳本
1+2+3+4....+100 de jie guo shi 5050.

方法3(比前兩種方式更加智能些,能夠以交互式的方式,指定累積加到某個數字):

[root@localhost ~]# vim 23.sh      #內容以下

#!/bin/bash
read -p "Please input a number, I will count for 1+2+...+your_input:" nu
s="0"
for ((i=1;i<=${nu}; i++))
do
s=$((${s}+${i}))
done
echo "the result of '1+2+...+${nu}' is ==> ${s}"
[root@localhost ~]# sh 23.sh     #執行
Please input a number, I will count for 1+2+...+your_input:100    #輸入100
the result of '1+2+...+100' is ==> 5050     #執行後輸出的結果
[root@localhost ~]# sh 23.sh        #再執行
Please input a number, I will count for 1+2+...+your_input:99   #累積加到99
the result of '1+2+...+99' is ==> 4950     #執行後輸出的結果

八、固定循環——for...do...done語句

需求①

第一次循環時, $name 的內容爲 內容1 ;
第二次循環時, $name的內容爲 內容2 ;
第三次循環時, $name的內容爲 內容3 ;
實現該需求有兩種方法,若須要循環的內容較多,建議採用方法1。
方法1:

[root@localhost ~]# vim 19.sh      #編寫腳本文件

#!/bin/bash
name=`cat /dongwu.txt`
for names in ${name}
do
echo "There are ${names}s.... "
done
[root@localhost ~]# vim /dongwu.txt     #編寫腳本中指定的文件

dog
cat
elephant
[root@localhost ~]# sh 19.sh     #執行並查看是否將上面文件中的內容依次輸出
There are dogs.... 
There are cats.... 
There are elephants....

方法2:

[root@localhost ~****]# vim 19.sh     #編寫腳本文件

#!/bin/bash
for name in dogs cats elephants
do
echo "There are ${name}s.... "
done

[root@localhost ~]# sh 19.sh     #執行腳本
There are dogss.... 
There are catss.... 
There are elephantss....

九、循環——cut、set結合for語句

需求①
因爲系統上面的各類賬號都是寫在/etc/passwd 內的第一個字段,那麼怎麼經過管線命令的cut捉出單純的賬號名稱後,以id分別檢查使用者的識別碼與特殊參數呢?

腳本以下:

[root@localhost ~]# vim 20.sh     #編寫腳本

#!/bin/bash
users=`cut -d ':' -f1 /etc/passwd`
for username in ${users}
do
id ${username}
done
[root@localhost ~]# sh 20.sh      #執行腳本
uid=0(root) gid=0(root) 組=0(root)
uid=1(bin) gid=1(bin) 組=1(bin)
uid=2(daemon) gid=2(daemon) 組=2(daemon)
uid=3(adm) gid=4(adm) 組=4(adm)
uid=4(lp) gid=7(lp) 組=7(lp)
           ........................#省略部份內容

十、其餘腳本類型舉例

需求1:
讓用戶輸入某個目錄文件名,而後自動找出某目錄內的文件名的權限。

[root@localhost ~]# vim 22.sh     #腳本內容以下

#!/bin/bash
read -p "qing shu ru yi ge dirctory name:" dirname
if [ "${dirname}" == "" -o ! -d "${dirname}" ]
        then
           echo "qing shu ru zheng que de dirctroy name."
           exit 1
fi
filelist=`ls ${dirname}`
for filename in ${filelist}
do
[ -r ${filename} ] && perm="read"
[ -w ${filename} ] && perm="${perm}+write"
[ -x ${filename} ] && perm="${perm}+exe"
echo "${filename} de quan xian shi ${perm}."
done
[root@localhost ~]# sh 22.sh     #執行腳本
qing shu ru yi ge dirctory name:/root     #根據提示指定一個目錄
10.sh de quan xian shi read+write.
11.sh de quan xian shi read+write.
12.sh de quan xian shi read+write.
13.sh de quan xian shi read+write.
14.sh de quan xian shi read+write.
15.sh de quan xian shi read+write.
16.sh de quan xian shi read+write.
17.sh de quan xian shi read+write.
18.sh de quan xian shi read+write.
19.sh de quan xian shi read+write.
1.sh de quan xian shi read+write+exe.
            ...................#省略部份內容

需求2:

搭配亂數來挑選(至關於拋硬幣),這裏好比說不知道要吃什麼,執行如下腳原本決定吃什麼。

[root@localhost ~]# vim 24.sh      #內容以下,十一、22....假設爲食物代號

#!/bin/bash
eat[1]="11"
eat[2]="22"
eat[3]="33"
eat[4]="44"
eat[5]="55"
eat[6]="66"
eat[7]="77"
eat[8]="88"
eat[9]="99"
eatnum=9
check=`echo "${RANDOM} % 10" | bc `
echo "your may eat ${eat[${check}]}"
[root@localhost ~]# sh 24.sh     #屢次執行,進行測試,會發現每次輸出的結果都不同
your may eat 22
[root@localhost ~]# sh 24.sh 
your may eat 55
[root@localhost ~]# sh 24.sh 
your may eat 99

十一、shell腳本的追蹤與debug

shell腳本語句語法使用(超詳細)

****用法:

[root@localhost ~]# sh -n 5.sh      #若是語法沒問題,則不會有任何輸出
[root@localhost ~]# sh -n 5.sh     #故意寫錯後,再測試
5.sh:行3: 未預期的符號 `do' 附近有語法錯誤
5.sh:行3: `do'
[root@localhost ~]# sh -x 6.sh        #將6.sh的執行過程都顯示出來
+ echo -e '\ncheng xu de wen jian ming shi 6.sh'

cheng xu de wen jian ming shi 6.sh
+ echo -e '\nyi gong you 0 ge can shu.'

yi gong you 0 ge can shu.
+ '[' 0 -lt 2 ']'
+ echo 'can shu tai shao le .'
can shu tai shao le .
+ exit 0

———————— 本文至此結束,感謝閱讀 ————————

相關文章
相關標籤/搜索