Shell腳本中判斷輸入?yún)?shù)個(gè)數(shù)的方法
$#代表了命令行的參數(shù)數(shù)量,可以看以下實(shí)例:
if [ $# != 1 ] ; then
echo "USAGE: $0 TABNAME"
echo " e.g.: $0 CDR_CALL_20040701"
exit 1;
fi
位置參數(shù) $1, $2,..., $N,$#代表了命令行的參數(shù)數(shù)量, $0代表了腳本的名字
-ne 不等于
-----------------------
shell 編程中使用到得if語(yǔ)句內(nèi)判斷參數(shù)
–b 當(dāng)file存在并且是塊文件時(shí)返回真
-c 當(dāng)file存在并且是字符文件時(shí)返回真
-d 當(dāng)pathname存在并且是一個(gè)目錄時(shí)返回真
-e 當(dāng)pathname指定的文件或目錄存在時(shí)返回真
-f 當(dāng)file存在并且是正規(guī)文件時(shí)返回真
-g 當(dāng)由pathname指定的文件或目錄存在并且設(shè)置了SGID位時(shí)返回為真
-h 當(dāng)file存在并且是符號(hào)鏈接文件時(shí)返回真,該選項(xiàng)在一些老系統(tǒng)上無(wú)效
-k 當(dāng)由pathname指定的文件或目錄存在并且設(shè)置了“粘滯”位時(shí)返回真
-p 當(dāng)file存在并且是命令管道時(shí)返回為真
-r 當(dāng)由pathname指定的文件或目錄存在并且可讀時(shí)返回為真
-s 當(dāng)file存在文件大小大于0時(shí)返回真
-u 當(dāng)由pathname指定的文件或目錄存在并且設(shè)置了SUID位時(shí)返回真
-w 當(dāng)由pathname指定的文件或目錄存在并且可執(zhí)行時(shí)返回真。一個(gè)目錄為了它的內(nèi)容被訪問(wèn)必然是可執(zhí)行的。
-o 當(dāng)由pathname指定的文件或目錄存在并且被子當(dāng)前進(jìn)程的有效用戶ID所指定的用戶擁有時(shí)返回真。
UNIX Shell 里面比較字符寫法:
-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-le 小于等于
-ge 大于等于
-z 空串
= 兩個(gè)字符相等
!= 兩個(gè)字符不等
-n 非空串
-------------------------------------------------------------------------
更為詳細(xì)的說(shuō)明:
運(yùn)算符 描述 示例
文件比較運(yùn)算符
-e filename 如果 filename 存在,則為真 [ -e /var/log/syslog ]
-d filename 如果 filename 為目錄,則為真 [ -d /tmp/mydir ]
-f filename 如果 filename 為常規(guī)文件,則為真 [ -f /usr/bin/grep ]
-L filename 如果 filename 為符號(hào)鏈接,則為真 [ -L /usr/bin/grep ]
-r filename 如果 filename 可讀,則為真 [ -r /var/log/syslog ]
-w filename 如果 filename 可寫,則為真 [ -w /var/mytmp.txt ]
-x filename 如果 filename 可執(zhí)行,則為真 [ -L /usr/bin/grep ]
filename1 -nt filename2 如果 filename1 比 filename2 新,則為真 [ /tmp/install/etc/services -nt /etc/services ]
filename1 -ot filename2 如果 filename1 比 filename2 舊,則為真 [ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比較運(yùn)算符 (請(qǐng)注意引號(hào)的使用,這是防止空格擾亂代碼的好方法)
-z string 如果 string 長(zhǎng)度為零,則為真 [ -z $myvar ]
-n string 如果 string 長(zhǎng)度非零,則為真 [ -n $myvar ]
string1 = string2 如果 string1 與 string2 相同,則為真 [ $myvar = one two three ]
string1 != string2 如果 string1 與 string2 不同,則為真 [ $myvar != one two three ]
算術(shù)比較運(yùn)算符
num1 -eq num2 等于 [ 3 -eq $mynum ]
num1 -ne num2 不等于 [ 3 -ne $mynum ]
num1 -lt num2 小于 [ 3 -lt $mynum ]
num1 -le num2 小于或等于 [ 3 -le $mynum ]
num1 -gt num2 大于 [ 3 -gt $mynum ]
num1 -ge num2 大于或等于 [ 3 -ge $mynum ]
腳本示例:
#!/bin/bash
# This script prints a message about your weight if you give it your
# weight in kilos and hight in centimeters.
if [ ! $# == 2 ]; then
echo "Usage: $0 weight_in_kilos length_in_centimeters"
exit
fi
weight="$1"
height="$2"
idealweight=$[$height - 110]
if [ $weight -le $idealweight ] ; then
echo "You should eat a bit more fat."
else
echo "You should eat a bit more fruit."
fi
# weight.sh 70 150
You should eat a bit more fruit.
# weight.sh 70 150 33
Usage: ./weight.sh weight_in_kilos length_in_centimeters
位置參數(shù) $1, $2,..., $N,$#代表了命令行的參數(shù)數(shù)量, $0代表了腳本的名字,
第一個(gè)參數(shù)代表$1,第二個(gè)參數(shù)代表$2,以此類推,參數(shù)數(shù)量的總數(shù)存在$#中,上面的例子顯示了怎么改變腳本,如果參數(shù)少于或者多余2個(gè)來(lái)打印出一條消息。
執(zhí)行,并查看情況。
# bash -x tijian.sh 60 170
+ weight=60
+ height=170
+ idealweight=60
+ '[' 60 -le 60 ']'
+ echo 'You should eat a bit more fat.'
You should eat a bit more fat.
其中-x用來(lái)檢查腳本的執(zhí)行情況。
相關(guān)文章
shell編程基礎(chǔ)之認(rèn)識(shí)與學(xué)習(xí)BASH
本文介紹下,shell基礎(chǔ)編程中有關(guān)bash的相關(guān)知識(shí),有需要的朋友參考學(xué)習(xí)下2013-11-11shell腳本實(shí)戰(zhàn)-while循環(huán)語(yǔ)句
這篇文章主要介紹了shell腳本實(shí)戰(zhàn)-while循環(huán)語(yǔ)句,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12shell之創(chuàng)建文件及內(nèi)容的方法示例
這篇文章主要介紹了shell之創(chuàng)建文件及內(nèi)容的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Linux Shell腳本實(shí)現(xiàn)檢測(cè)tomcat
這篇文章主要介紹了Linux Shell腳本實(shí)現(xiàn)檢測(cè)tomcat的方法,推薦給小伙伴們,需要的朋友可以參考下2015-03-03什么是Shell?Shell腳本基礎(chǔ)知識(shí)詳細(xì)介紹
這篇文章主要介紹了什么是Shell?Shell腳本基礎(chǔ)知識(shí)介紹,本文是一篇Shell腳本入門文章,在本文你可學(xué)到什么是Shell、有多少種Shell、一個(gè)Shell腳本代碼實(shí)例,需要的朋友可以參考下2014-07-07Centos下查看網(wǎng)卡的實(shí)時(shí)流量命令
本文介紹了linux下查看網(wǎng)卡流量的六種方法,linux系統(tǒng)中使用nload、iftop、iostat等工具查看網(wǎng)卡流量,這里我們先來(lái)詳細(xì)講解下 iptraf 方法,需要的朋友參考下。2015-05-05編寫B(tài)ash Shell通過(guò)gnuplot繪制系統(tǒng)性能數(shù)據(jù)圖的方法
這篇文章主要介紹了編寫B(tài)ash Shell通過(guò)gnuplot繪制系統(tǒng)性能數(shù)據(jù)圖的方法,做到可視化數(shù)據(jù)收集,需要的朋友可以參考下2015-07-07