我想创建一个脚本,从命令行接受参数并将它们分配给内部变量,并在适当时调用“函数”。
假设我有一个包含以下内容的bash脚本:
test1(){
echo "this is a test"
if [ -z $var1 ]; then echo $var1 fi
if [ -z $var2 ]; then echo $var2 fi
}
usage(){
echo "to use this script you need arguments 1 and 2".
}
var1=""
var2=""
# Loop to read options and arguments
while [ $1 ]; do
case "$1" in
'-h') usage;;
'-1') var1="$2";;
'-2') var2="$2";;
'-c') test1;;
esac
shift
done
我希望我的bash脚本读取所有提供的参数并将它们分配给内部变量然后执行任何相关的函数。 让我们说下面的输入:
INPUT: mybash -c -1="xxxxx" -2="ccccc"
OUTPUT: test1 is executed without -1 and -2 to supply the value
EXPECTED OUTPUT: test1 is executed after -1 and -2 have valorised var1 and var2
由于