2012-08-01 73 views
0

我已參數命名爲「國家」和「城市」在UNIX Shell腳本傳遞定製參數

運行腳本的常用方法是 Script.ksh「印度」,「孟買」 .. ..它將運行良好。

但是,我的要求是...我要運行該腳本 Script.ksh -country「印度」 - 城市「德里」

Culd任何一個,請讓我離開這個

在此先感謝...

+0

是'getopt'作爲系統中的命令嗎? – 2012-08-01 22:31:36

+0

你能舉一個例子嗎?使用這個命令? 我不知道。 – par181 2012-08-03 20:45:30

回答

1

的樣本用法:

#!/bin/sh 

setopt() { 
    if [ -n "$1" -a -n "$2" ]; then 
    optname=opt_${1#--} 
    optval="\"$2\"" 
    eval $optname="$optval" 
    shift 
    shift 
    setopt "[email protected]" 
    fi 
} 

eval setopt $(getopt -a -l city:,country: -o "" -- "[email protected]") 

echo "City is ${opt_city}" 
echo "Country is ${opt_country}" 

你可以使用沒有getopt的技術是相同的,但getopt具有標準化名稱和識別縮寫(至少GNU getopt)的額外好處。

$ ./opttest -city "New Delhi" -country India 
City is New Delhi 
Country is India 

$ ./opttest -ci "New Delhi" -co India 
City is New Delhi 
Country is India 
+0

非常感謝你的邪惡otto .......... – par181 2012-08-07 20:20:21