【发布时间】:2016-03-27 23:51:42
【问题描述】:
我需要将参数传递给 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
【问题讨论】:
-
您可能想要使用
case语句,而不是大量的级联ifs。 -
顺便说一句,不仅仅是测试需要引号来确保内容在没有修改的情况下通过;你还想要
func1 "$2"和func2 "$2"
标签: linux bash function shell arguments