您的位置:首页 > 运维架构 > Shell

linux shell 传递带空格的数组

2016-11-25 14:45 204 查看
本测试有两个程序,程序a接收所有的参数,并且过滤掉-skipTrash,然后调用程序b,程序b非常简单,只是把所有的参数都打印出来,证明参数是正确的。

程序a的源代码如下:

#!/bin/bash
array=()
for((i=1;i<=$#;i++)); do
if [ "${!i}" != "-skipTrash" ]; then
array[${i}]="${!i}"
fi
done
sh b "${array[@]}"


程序b的源代码如下:

#!/bin/bash
echo "in proc b"
echo "Number of parameters:" $#
for((i=1;i<=$#;i++)); do
echo ${!i}
done


测试结果:

[houzhizhen@localhost bin]$ sh a "a b" "c d"  wer -skipTrash adfw a b
in proc b
Number of parameters: 6
a b
c d
wer
adfw
a
b
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: