淺談Shell腳本參數(shù)與交互及常見(jiàn)問(wèn)題
一、Shell編程-腳本參數(shù)與交互及常見(jiàn)問(wèn)題
在執(zhí)行一個(gè)腳本程序時(shí),會(huì)經(jīng)常需要向腳本傳遞一些參數(shù),并根據(jù)輸入的參數(shù)值生成相應(yīng)的數(shù)據(jù)或執(zhí)行特定的邏輯。
1.1 向腳本傳遞參數(shù)
執(zhí)行Shell腳本時(shí)可以帶有參數(shù),在Shell腳本中有變量與之對(duì)應(yīng)進(jìn)行引用。這類變量的名稱很特別,分別是0、1、2、3...被稱為位置變量。
位置變量是由 0 開(kāi)始,其中 0 變量預(yù)留用來(lái)保存實(shí)際腳本的名字,1 變量對(duì)應(yīng)腳本程序的第 1個(gè)參數(shù),依次類推。與其他變量一樣,可以在Shell 中通過(guò)“$”符號(hào)來(lái)引用位置變量的值。
[root@localhost 20190105]# vi paral.sh #!/bin/bash #顯示腳本名 echo 'The script name is '$0 #顯示第1個(gè)參數(shù) echo 'The 1th parameter is '$1 #顯示第2個(gè)參數(shù) echo 'The 2th parameter is '$2 #顯示第3個(gè)參數(shù) echo 'The 3th parameter is '$3 #顯示第4個(gè)參數(shù) echo 'The 4th parameter is '$4 #顯示第5個(gè)參數(shù) echo 'The 5th parameter is '$5 #顯示第6個(gè)參數(shù) echo 'The 6th parameter is '$6 #顯示第7個(gè)參數(shù) echo 'The 7th parameter is '$7 #顯示第8個(gè)參數(shù) echo 'The 8th parameter is '$8 #顯示第9個(gè)參數(shù) echo 'The 9th parameter is '$9 [root@localhost 20190105]# ./paral.sh Ni hao , Nice to meet you ! The script name is ./paral.sh The 1th parameter is Ni The 2th parameter is hao The 3th parameter is , The 4th parameter is Nice The 5th parameter is to The 6th parameter is meet The 7th parameter is you The 8th parameter is ! The 9th parameter is//空值 [root@localhost 20190105]#
1.2 用戶交互
使用 read 命令可以從鍵盤上讀取數(shù)據(jù),然后賦給指定的變量,在Shell腳本中實(shí)現(xiàn)與用戶的數(shù)據(jù)交互。
read命令的格式
read 變量1 [變量2...]
read命令可以從鍵盤上讀取到多個(gè)變量的值,用戶輸入數(shù)據(jù)時(shí),數(shù)據(jù)間以空格或者 Tab鍵作為分隔。
如果變量個(gè)數(shù)與輸入的數(shù)據(jù)個(gè)數(shù)相同,則依次對(duì)應(yīng)賦值;
如果變量個(gè)數(shù)大于輸入的數(shù)據(jù)個(gè)數(shù),則從左到右對(duì)應(yīng)賦值;如果沒(méi)有數(shù)據(jù),則以之對(duì)應(yīng)的變量為空;
如果變量個(gè)數(shù)少于輸入的數(shù)據(jù)個(gè)數(shù),則從左到右對(duì)應(yīng)賦值,最后一個(gè)變量被賦予剩余的所有數(shù)據(jù)。
通過(guò) read 命令讀取鍵盤上輸入的數(shù)據(jù)保存到變量中,同時(shí)把變量值顯示在屏幕上,當(dāng)用戶輸入 exit 時(shí)結(jié)束程序。
[root@localhost 20190105]# vi read1.sh #!/bin/bash #初始化變量的值 input1='' #設(shè)置 input1 變量值為空 input2='' #設(shè)置 input2 變量值為空 input3='' #設(shè)置 input3 變量值為空 input4='' #設(shè)置 input4 變量值為空 #until 循環(huán),當(dāng) input1 變量的值為 exit 時(shí)退出該循環(huán) until [ "$input1" = exit ] do echo 'Please input the values:' #讀取鍵盤輸入的數(shù)據(jù) read input1 input2 input3 input4 #輸入的不是 exit 時(shí)把用戶輸入的數(shù)據(jù)顯示在屏幕上 if [ "$input1" != exit ] then echo 'input1: '$input1#輸出變量 input1 的值 echo 'input2: '$input2#輸出變量 input2 的值 echo 'input3: '$input3#輸出變量 input3 的值 echo 'input4: '$input4#輸出變量 input4 的值 echo #當(dāng)輸入為 exit 時(shí)顯示退出腳本的提示 else echo 'Exit the script.' fi done [root@localhost 20190105]# chmod +x read1.sh [root@localhost 20190105]# ./read1.sh Please input the values: How do you do //輸入的數(shù)據(jù)個(gè)數(shù)與變量個(gè)數(shù)相等 input1: How input2: do input3: you input4: do Please input the values: Welcome to beijing //輸入的數(shù)據(jù)個(gè)數(shù)小于變量個(gè)數(shù) input1: Welcome input2: to input3: beijing input4: Please input the values: let's go //輸入的數(shù)據(jù)個(gè)數(shù)小于變量個(gè)數(shù) input1: let's input2: go input3: input4: Please input the values: Nice to meet you,too!//輸入的數(shù)據(jù)個(gè)數(shù)大于變量個(gè)數(shù) input1: Nice input2: to input3: meet input4: you,too! Please input the values: //結(jié)束程序 exit Exit the script. [root@localhost 20190105]#
運(yùn)行結(jié)果可以看出:
- 當(dāng)變量個(gè)數(shù)大于輸入的數(shù)據(jù)個(gè)數(shù)時(shí),沒(méi)有數(shù)據(jù)與之對(duì)應(yīng)的變量的值為空;
- 當(dāng)變量個(gè)數(shù)小于輸入的數(shù)據(jù)個(gè)數(shù)時(shí),最后一個(gè)變量會(huì)被賦予剩余的所有數(shù)據(jù);
1.3 特殊變量
特殊變量及說(shuō)明

[root@localhost 20190105]# vi vall.sh #!/bin/bash echo 'The value of $# is: '$# //輸出$#變量的值 echo 'The value of $* is: '$* //輸出$*變量的值 echo 'The value of $@ is: '$@ //輸出$@變量的值 echo 'The value of $$ is: '$$ //輸出$$變量的值 echo 'The value of $! is: '$! //輸出$!變量的值 echo 'The value of $- is: '$- //輸出$-變量的值 echo 'The value of $? is: '$? //輸出$?變量的值 [root@localhost 20190105]# ./vall.sh how do you do The value of $# is: 4 //輸出4變量的值 The value of $* is: how do you do //輸出how do you do變量的值 The value of $@ is: how do you do //輸出how do you do變量的值 The value of $$ is: 9040 //輸出9040變量的值 The value of $! is: //輸出變量的值 The value of $- is: hB //輸出hB變量的值 The value of $? is: 0 //輸出0變量的值 [root@localhost 20190105]#
1.4 Shell編程常見(jiàn)問(wèn)題
1.4.1 如何屏蔽命令的輸出結(jié)果
Linux 默認(rèn)會(huì)創(chuàng)建一個(gè)設(shè)備文件/dev/null(空設(shè)備),所有輸出到該設(shè)備的信息都會(huì)被屏蔽。通過(guò)把命令的輸出重定向到設(shè)備/dev/null,可以屏蔽命令的輸出結(jié)果。
命令 > /dev/null
屏蔽命令的錯(cuò)誤輸出
命令 2> /dev/null
屏蔽命令的正常以及錯(cuò)誤輸出
命令 > /dev/null 2> /dev/null
例如:要在 Shell 代碼中使用 grep 命令查找文件是否存在某個(gè)關(guān)鍵字,但是又希望屏幕 grep 命令的輸出。
if grep jack /etc/passwd > /dev/null then echo "jack found" fi
如果 /etc/passwd 文件中有 jack 關(guān)鍵字的信息,將會(huì)顯示 jack found,但不會(huì)輸出 grep 命令的執(zhí)行結(jié)果。
1.4.2 如何把一條命令分成多行編寫
Linux 的 Shell 腳本功能非常強(qiáng)大,它允許用戶通過(guò)管道方式把多個(gè)命令組合在一起,但因此往往也導(dǎo)致在一行 Shell 腳本代碼中編寫的命令過(guò)長(zhǎng),難以閱讀,為了使腳本的結(jié)構(gòu)更加清晰,可以把一行 Shell 腳本代碼分成多行進(jìn)行編寫。
使用兩個(gè)管道符把ps、grep 和 awk 命令組合。
[root@localhost ~]# ps -ef | grep sshd | awk '{print $2}'
4478
12821
22028
在一行代碼中把多個(gè)命令組合在一起,難以閱讀。Shell 提供了一個(gè)特殊字符“\”,可以把一行代碼分成多行進(jìn)行編寫。
[root@localhost ~]# ps -ef | \
> grep ssh | \
> awk '{print $2}'
4478
12821
23375
[root@localhost ~]#
到此這篇關(guān)于淺談Shell腳本參數(shù)與交互及常見(jiàn)問(wèn)題的文章就介紹到這了,更多相關(guān)Shell腳本參數(shù)與交互內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信