我需要将参数传递给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“' –