藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月26日 11:18


請教為什麼我的getops中的case沒起作用啊

高手幫幫忙,我寫了個簡單的getops以便學習,可是系統當我輸入./getopts.sh -x -y YANG -z GE *的時候,只有for裡的echo顯示出數據了,為什麼沒有打印出xopt,yopt,zopt的值啊,請問該如何做才能解決這個問題啊,下面是程序getopts.sh(我用的是redhat9.0)
while getopts ":xy:z:" opt;
do
case $opt in
        X ) xopt='-x set';;
        Y ) yopt="-y set and called with $OPTARG";;
        Z ) zopt="-z set and called with $OPTARG";;
        \? ) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...'
          exit 1
  esac
done
shift $(($OPTIND - 1))
echo ${xopt: -'did not use -x'}
echo ${yopt: -'did not use -y'}
echo ${zopt: -'did not use -z'}
echo "Remaining command-line arguments are:"
for f in "$@"
do
  echo -e "$f"
done

shift那段沒用吧,因為OPTIND是有自動shift的作用

注意大小寫

我把後面的全都註釋掉了,只留下while跟case,結果輸入./getopts.sh -x -y YANG -z GE 後還是什麼都沒顯示出來。。。
while getopts ":xy:z:" opt;
do
case $opt in
        X ) xopt='-x set';;
        Y ) yopt="-y set and called with $OPTARG";;
        Z ) zopt="-z set and called with $OPTARG";;
        \? ) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...'
          exit 1
  esac
done
echo $xopt
echo $yopt
echo $zopt

-->

大小寫??是這樣嗎?運行結果卻是 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...『 哭......如果還沒改對請說詳細點啊,謝謝
while getopts ":xy:z:" opt;
do
case $opt in
        x ) xopt='-x set';;
        y ) yopt="-y set and called with $OPTARG";;
        z ) zopt="-z set and called with $OPTARG";;
        \? ) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...'
          exit 1
  esac
done
echo $xopt
echo $yopt
echo $zopt


改對了,是用了除xyz以外的參數了吧?

為什麼你的while getopts 「:」在參數前有個:呢?只有需要時間參數的時候才用:啊!
應該x:才對啊。。。並且,你的while這行的最後,沒有;號吧

-->

-->

-->
我還是喜歡那個寫字的

終於明白了,原來帶了:x的getopts一但把命令寫成..-x h後程序就只會對x的進行處理,如果後面還帶有其他的參數也會忽略掉,至於那個'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...『 是我自己用了大寫的X Y的緣故哈,謝謝各位幫助哈



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com