我有一个如下脚本:
#!/bin/bash
declare -A aaa
while [ "$1" != "" ];
do
case "$1" in
-e|--env)
if [ ! "$2" == "" ] && [ ! "$3" == "" ]; then
aaa[$2]="$3"
fi
shift
shift
shift
;;
*)
echo "Error"
shift
;;
esac
done
for K in "${!aaa[@]}";
do
str="$str --build-arg $K=\"${aaa[$K]}\""
done
#echo $str
./test_args.sh $str
它被称为:
./multi_args.sh -e arg1 "Hello Man!" -e arg2 Bye
在第二个脚本test_args.sh
中,我获得了构建的字符串str
并打印参数。
test_args.sh:
#!/bin/bash
while [ "$1" != "" ];
do
echo "$1"
shift
done
它应该像这样打印:
arg1
Hello Man!
arg2
Bye
但它会打印:
arg1
Hello
Man!
arg2
Bye