2017-04-01 171 views
0

#!/usr/bin/env zsh ./client localhost 5000 Bryan (my command)爲測試服務器創建腳本

我想檢查我的命令的參數是正確的,在腳本的zsh這意味着:

if 
first argument = int 
second argument = int 
third argument = string 
fi 

回答

0

只要是明確的,所有這些條件必須腳本繼續是真的,是嗎?此外,請注意「字符串」幾乎意味着任何字符(A-Z,0-9,標點符號) - 除控制字符以外的任何字符。假如是這樣的話,怎麼樣像這樣:

re='^[0-9]+$' 
    echo " " 
    echo "Parm 1: $1" 
    if ! [[ "$1" =~ $re ]]; then 
    echo "The first parameter must be an integer." 
    exit 1 
    fi 

    echo " " 
    echo "Parm 2: $2" 
    if ! [[ $2 =~ $re ]]; then 
    echo "The second parameter must be an integer." 
    exit 2 
    fi 

    re='^[[:graph:]]+$' 
    echo " " 
    echo "Parm 3: $3" 
    if ! [[ $3 =~ $re ]]; then 
    echo "The third parameter must be an string." 
    exit 3 
    fi 
    echo " " 
    echo "Congratulations -- your parms are valid\!" 
    echo " " 

編輯:下面是一些樣品運行:

~/test1$ 
~/test1$./tmp 123 456 abdc 

Parm 1: 123 

Parm 2: 456 

Parm 3: abdc 

Congratulations -- your parms are valid\! 

~/test1$./tmp abc 123 456 

Parm 1: abc 
The first parameter must be an integer. 
~/test1$./tmp 12b 123 abc 

Parm 1: 12b 
The first parameter must be an integer. 
~/test1$./tmp 123 4h5 abc 

Parm 1: 123 

Parm 2: 4h5 
The second parameter must be an integer. 
~/test1$./tmp 123 jkl asdf 

Parm 1: 123 

Parm 2: jkl 
The second parameter must be an integer. 
~/test1$ 
~/test1$ 

希望這有助於!

+0

請注意,如果我的假設是錯誤的,請告訴我,我可以更新答案。 –

+0

呃。我只是注意到你特別要求提供zsh--這是我不熟悉的。可能存在語法上的差異,但邏輯保持不變。 –

+0

謝謝你羅傑的幫助! –

相關問題