對(duì)shell中常見(jiàn)參數(shù)及判斷命令介紹
Shell常用特殊變量
經(jīng)常會(huì)在shell命令中,看到$0, $#, $*, $@, $?, $$這樣的取值,這些代表什么呢?
變量 | 含義 |
---|---|
$0 | 當(dāng)前腳本的文件名 |
$n | 傳遞給腳本或函數(shù)的參數(shù)。n 是一個(gè)數(shù)字,表示第幾個(gè)參數(shù)。例如,第一個(gè)參數(shù)是$1,第二個(gè)參數(shù)是$2 |
$# | 傳遞給腳本或函數(shù)的參數(shù)個(gè)數(shù) |
$* | 傳遞給腳本或函數(shù)的所有參數(shù) |
$@ | 傳遞給腳本或函數(shù)的所有參數(shù)。被雙引號(hào)(” “)包含時(shí),與 $* 稍有不同 |
$? | 上個(gè)命令的退出狀態(tài),或函數(shù)的返回值。成功返回0,失敗返回1 |
$$ | 當(dāng)前Shell進(jìn)程ID。對(duì)于 Shell 腳本,就是這些腳本所在的進(jìn)程ID |
$* 和 $@ 都是將參數(shù)一個(gè)一個(gè)返回
"$*"將所有參數(shù)當(dāng)做一個(gè)整體字符串返回 , "$@"將參數(shù)一個(gè)一個(gè)返回
常用判斷參數(shù)
在shell命令文件中還經(jīng)常會(huì)看到類似與if [ -z "${SPARK_HOME}" ]; then這樣的判斷語(yǔ)句?是不是也和我一樣很疑惑-z是什么含義?
下面是幾個(gè)常見(jiàn)的參數(shù),供查詢使用:
-a file exists. -b file exists and is a block special file. -c file exists and is a character special file. -d file exists and is a directory. -e file exists (just the same as -a). -f file exists and is a regular file. -g file exists and has its setgid(2) bit set. -G file exists and has the same group ID as this process. -k file exists and has its sticky bit set. -L file exists and is a symbolic link. -n string length is not zero. -o Named option is set on. -O file exists and is owned by the user ID of this process. -p file exists and is a first in, first out (FIFO) special file or named pipe. -r file exists and is readable by the current process. -s file exists and has a size greater than zero. -S file exists and is a socket. -t file descriptor number fildes is open and associated with a terminal device. -u file exists and has its setuid(2) bit set. -w file exists and is writable by the current process. -x file exists and is executable by the current process. -z string length is zero.
判斷命令
shell中除了有上邊這樣用來(lái)判斷文件是否存在的參數(shù),當(dāng)然還有判斷兩個(gè)數(shù)是否相等這樣更常規(guī)的命令
例如,if [ $# -gt 0 ]這樣判斷傳入?yún)?shù)個(gè)數(shù)是否為0
命令 | 含義 |
---|---|
-eq | 等于 |
-ne | 不等于 |
-gt | 大于 |
-lt | 小于 |
ge | 大于等于 |
le | 小于等于 |
以上這篇對(duì)shell中常見(jiàn)參數(shù)及判斷命令介紹就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
shell通過(guò)sed上下兩行合并成一行的實(shí)現(xiàn)
本文主要介紹了shell通過(guò)sed上下兩行合并成一行的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03shell腳本ssh遠(yuǎn)程執(zhí)行命令給變量賦值的問(wèn)題解決
本文主要介紹了shell腳本ssh遠(yuǎn)程執(zhí)行命令給變量賦值的問(wèn)題解決,就是從A機(jī)器通過(guò)SSH方式到B機(jī)器,并執(zhí)行相關(guān)的命令,具有一定的參考價(jià)值,感興趣的可以了解一下2023-07-07Shell腳本實(shí)現(xiàn)上傳zip壓縮文件到FTP服務(wù)器
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)上傳zip壓縮文件到FTP服務(wù)器,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-12-12Shell腳本中實(shí)現(xiàn)切換用戶并執(zhí)行命令操作
這篇文章主要介紹了Shell腳本中實(shí)現(xiàn)切換用戶并執(zhí)行命令操作,看了示例代碼就秒懂了,原來(lái)如此簡(jiǎn)單,需要的朋友可以參考下2014-12-12shell for循環(huán)、循環(huán)變量值付給其他shell腳本的方法
今天小編就為大家分享一篇shell for循環(huán)、循環(huán)變量值付給其他shell腳本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06一天一個(gè)shell命令 linux文本操作系列-tree命令詳解
這篇文章主要介紹了一天一個(gè)shell命令 linux文本操作系列-tree命令詳解,需要的朋友可以參考下2016-06-06