淺談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)行引用。這類(lèi)變量的名稱(chēng)很特別,分別是0、1、2、3...被稱(chēng)為位置變量。
位置變量是由 0 開(kāi)始,其中 0 變量預(yù)留用來(lái)保存實(shí)際腳本的名字,1 變量對(duì)應(yīng)腳本程序的第 1個(gè)參數(shù),依次類(lèi)推。與其他變量一樣,可以在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 用戶(hù)交互
使用 read 命令可以從鍵盤(pán)上讀取數(shù)據(jù),然后賦給指定的變量,在Shell腳本中實(shí)現(xiàn)與用戶(hù)的數(shù)據(jù)交互。
read命令的格式
read 變量1 [變量2...]
read命令可以從鍵盤(pán)上讀取到多個(gè)變量的值,用戶(hù)輸入數(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 命令讀取鍵盤(pán)上輸入的數(shù)據(jù)保存到變量中,同時(shí)把變量值顯示在屏幕上,當(dāng)用戶(hù)輸入 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:' #讀取鍵盤(pán)輸入的數(shù)據(jù) read input1 input2 input3 input4 #輸入的不是 exit 時(shí)把用戶(hù)輸入的數(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 如何把一條命令分成多行編寫(xiě)
Linux 的 Shell 腳本功能非常強(qiáng)大,它允許用戶(hù)通過(guò)管道方式把多個(gè)命令組合在一起,但因此往往也導(dǎo)致在一行 Shell 腳本代碼中編寫(xiě)的命令過(guò)長(zhǎng),難以閱讀,為了使腳本的結(jié)構(gòu)更加清晰,可以把一行 Shell 腳本代碼分成多行進(jìn)行編寫(xiě)。
使用兩個(gè)管道符把ps、grep 和 awk 命令組合。
[root@localhost ~]# ps -ef | grep sshd | awk '{print $2}' 4478 12821 22028
在一行代碼中把多個(gè)命令組合在一起,難以閱讀。Shell 提供了一個(gè)特殊字符“\”,可以把一行代碼分成多行進(jìn)行編寫(xiě)。
[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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux echo命令的使用及三種實(shí)現(xiàn)方式
這篇文章主要介紹了Linux echo命令的使用及三種實(shí)現(xiàn)方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Shell實(shí)現(xiàn)強(qiáng)制釋放內(nèi)存腳本分享
這篇文章主要介紹了Shell實(shí)現(xiàn)強(qiáng)制釋放內(nèi)存腳本分享,本文直接給出實(shí)現(xiàn)代碼,并對(duì)每一句代碼都做了講解了,需要的朋友可以參考下2015-02-02shell批量創(chuàng)建文件并重新命名的實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于shell批量創(chuàng)建文件并重新命名的相關(guān)資料,文中還介紹了批量刪除文件以及文件更名的多種方法,每種方法都給出了詳細(xì)實(shí)例代碼,需要的朋友可以參考下2021-07-07shell自定義函數(shù)的6個(gè)特點(diǎn)總結(jié)
這篇文章主要介紹了shell自定義函數(shù)的6個(gè)特點(diǎn)總結(jié),也是使用shell自定義函數(shù)的一些注意事項(xiàng)總結(jié),以及小技巧介紹,需要的朋友可以參考下2014-07-07Cygwin下安裝vim后,vim中退格鍵無(wú)法正常使用的解決方法
下面小編就為大家?guī)?lái)一篇Cygwin下安裝vim后,vim中退格鍵無(wú)法正常使用的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02一天一個(gè)shell命令 linux文本內(nèi)容操作系列-cut命令詳解
這篇文章主要介紹了一天一個(gè)shell命令 linux文本內(nèi)容操作系列-cut命令詳解,需要的朋友可以參考下2016-06-06