欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Shell腳本中判斷輸入?yún)?shù)個數(shù)的方法

 更新時間:2014年10月22日 10:50:13   投稿:junjie  
這篇文章主要介紹了Shell腳本中判斷輸入?yún)?shù)個數(shù)的方法,使用內置變量$#即可實現(xiàn)判斷輸入了多少個參數(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語句內判斷參數(shù)

–b 當file存在并且是塊文件時返回真

-c 當file存在并且是字符文件時返回真

-d 當pathname存在并且是一個目錄時返回真

-e 當pathname指定的文件或目錄存在時返回真

-f 當file存在并且是正規(guī)文件時返回真

-g 當由pathname指定的文件或目錄存在并且設置了SGID位時返回為真

-h 當file存在并且是符號鏈接文件時返回真,該選項在一些老系統(tǒng)上無效

-k 當由pathname指定的文件或目錄存在并且設置了“粘滯”位時返回真

-p 當file存在并且是命令管道時返回為真

-r 當由pathname指定的文件或目錄存在并且可讀時返回為真

-s 當file存在文件大小大于0時返回真

-u 當由pathname指定的文件或目錄存在并且設置了SUID位時返回真

-w 當由pathname指定的文件或目錄存在并且可執(zhí)行時返回真。一個目錄為了它的內容被訪問必然是可執(zhí)行的。

-o 當由pathname指定的文件或目錄存在并且被子當前進程的有效用戶ID所指定的用戶擁有時返回真。

UNIX Shell 里面比較字符寫法:

-eq   等于

-ne    不等于

-gt    大于

-lt    小于

-le    小于等于

-ge   大于等于

-z    空串

=    兩個字符相等

!=    兩個字符不等

-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 為符號鏈接,則為真      [ -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 ]

字符串比較運算符 (請注意引號的使用,這是防止空格擾亂代碼的好方法)
-z string               如果 string 長度為零,則為真               [ -z $myvar ]

-n string                      如果 string 長度非零,則為真        [ -n $myvar ]

string1 = string2         如果 string1 與 string2 相同,則為真     [ $myvar = one two three ]

string1 != string2        如果 string1 與 string2 不同,則為真     [ $myvar != one two three ]

算術比較運算符

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代表了腳本的名字,

第一個參數(shù)代表$1,第二個參數(shù)代表$2,以此類推,參數(shù)數(shù)量的總數(shù)存在$#中,上面的例子顯示了怎么改變腳本,如果參數(shù)少于或者多余2個來打印出一條消息。

執(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用來檢查腳本的執(zhí)行情況。

相關文章

  • shell編程基礎之認識與學習BASH

    shell編程基礎之認識與學習BASH

    本文介紹下,shell基礎編程中有關bash的相關知識,有需要的朋友參考學習下
    2013-11-11
  • shell腳本實戰(zhàn)-while循環(huán)語句

    shell腳本實戰(zhàn)-while循環(huán)語句

    這篇文章主要介紹了shell腳本實戰(zhàn)-while循環(huán)語句,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • shell之創(chuàng)建文件及內容的方法示例

    shell之創(chuàng)建文件及內容的方法示例

    這篇文章主要介紹了shell之創(chuàng)建文件及內容的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • Linux Shell腳本實現(xiàn)檢測tomcat

    Linux Shell腳本實現(xiàn)檢測tomcat

    這篇文章主要介紹了Linux Shell腳本實現(xiàn)檢測tomcat的方法,推薦給小伙伴們,需要的朋友可以參考下
    2015-03-03
  • 又拍云存儲同步腳本

    又拍云存儲同步腳本

    為了可以自動把網(wǎng)站上的css、js以及圖片等靜態(tài)資源放在又拍云存儲上,訪問速度明顯提高不少,下面簡單介紹下實現(xiàn)步驟,需要的朋友可以參考下
    2013-11-11
  • 什么是Shell?Shell腳本基礎知識詳細介紹

    什么是Shell?Shell腳本基礎知識詳細介紹

    這篇文章主要介紹了什么是Shell?Shell腳本基礎知識介紹,本文是一篇Shell腳本入門文章,在本文你可學到什么是Shell、有多少種Shell、一個Shell腳本代碼實例,需要的朋友可以參考下
    2014-07-07
  • 腳本自動添加crontab示例

    腳本自動添加crontab示例

    這篇文章主要介紹了腳本自動添加crontab示例,需要的朋友可以參考下
    2014-04-04
  • Centos下查看網(wǎng)卡的實時流量命令

    Centos下查看網(wǎng)卡的實時流量命令

    本文介紹了linux下查看網(wǎng)卡流量的六種方法,linux系統(tǒng)中使用nload、iftop、iostat等工具查看網(wǎng)卡流量,這里我們先來詳細講解下 iptraf 方法,需要的朋友參考下。
    2015-05-05
  • 用shell命令讀取與輸出數(shù)據(jù)的代碼

    用shell命令讀取與輸出數(shù)據(jù)的代碼

    本文為大家介紹使用shell命令進行讀取與輸出數(shù)據(jù)的方法,其中涉及了文件輸出、重定向、管道等相關知識,有興趣的朋友可以參考下
    2013-02-02
  • 編寫B(tài)ash Shell通過gnuplot繪制系統(tǒng)性能數(shù)據(jù)圖的方法

    編寫B(tài)ash Shell通過gnuplot繪制系統(tǒng)性能數(shù)據(jù)圖的方法

    這篇文章主要介紹了編寫B(tài)ash Shell通過gnuplot繪制系統(tǒng)性能數(shù)據(jù)圖的方法,做到可視化數(shù)據(jù)收集,需要的朋友可以參考下
    2015-07-07

最新評論