shell 函数和参数传递问题
shell functions and argument passing issue
我需要将参数传递给 shell 脚本,该脚本在验证后需要传递给脚本中的函数,无论是原样还是修改后。
#!/bin/sh
func1(){
echo "called func1 with " ;
exit 0 ;
}
func2(){
echo "called func2 with " ;
exit 0 ;
}
if [ $# -eq 0 ]
then
echo "Usage:" ;
echo "script.sh arg1 arg2" ;
exit -1 ;
fi
if [ "" -eq "txt1" ]
then
func1 ;
exit 0 ;
fi
if ["" -eq "txt2" ]
then
func2 ;
exit 0;
fi
我得到的回复是
sh script.sh txt1
sh: txt1: bad number
sh: txt1: bad number
您使用了错误的字符串比较运算符。您要使用 =
,而不是 -eq
(比较整数)。
请注意 [
只是 shell 的内部命令,因此您需要用空格将其与其参数分开。 (在您的脚本的第三个测试中。)
我需要将参数传递给 shell 脚本,该脚本在验证后需要传递给脚本中的函数,无论是原样还是修改后。
#!/bin/sh
func1(){
echo "called func1 with " ;
exit 0 ;
}
func2(){
echo "called func2 with " ;
exit 0 ;
}
if [ $# -eq 0 ]
then
echo "Usage:" ;
echo "script.sh arg1 arg2" ;
exit -1 ;
fi
if [ "" -eq "txt1" ]
then
func1 ;
exit 0 ;
fi
if ["" -eq "txt2" ]
then
func2 ;
exit 0;
fi
我得到的回复是
sh script.sh txt1
sh: txt1: bad number
sh: txt1: bad number
您使用了错误的字符串比较运算符。您要使用 =
,而不是 -eq
(比较整数)。
请注意 [
只是 shell 的内部命令,因此您需要用空格将其与其参数分开。 (在您的脚本的第三个测试中。)