我需要將參數傳遞給shell腳本,該腳本經過驗證後需要以相同或修改後的形式傳遞給腳本中的函數。shell函數和參數傳遞問題
#!/bin/sh
func1(){
echo "called func1 with $1" ;
exit 0 ;
}
func2(){
echo "called func2 with $1" ;
exit 0 ;
}
if [ $# -eq 0 ]
then
echo "Usage:" ;
echo "script.sh arg1 arg2" ;
exit -1 ;
fi
if [ "$1" -eq "txt1" ]
then
func1 $2 ;
exit 0 ;
fi
if ["$1" -eq "txt2" ]
then
func2 $2 ;
exit 0;
fi
我得到的迴應是
sh script.sh txt1
sh: txt1: bad number
sh: txt1: bad number
而不是大量的級聯'if's,您可能需要使用'case'語句。 –
順便說一下,它不僅僅是需要引號的測試,以確保內容不加修改地通過;你也希望'func1'$ 2「'和'func2」$ 2「' –