bash脚本接收和再通过参数报价 [英] Bash script to receive and repass quoted parameters

查看:89
本文介绍了bash脚本接收和再通过参数报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图去安全地嵌套脚本接收的bash脚本的引用参数。任何想法?

test.sh

 #!/斌/庆典
回声$ *
庆典myecho.sh $ *

myecho.sh

 #!/斌/庆典
 回声$ 1
 回声$ 2
 回声$ 3
 回声$ 4个

示例:

 的bash test.sh AAA BBB'CCC DDD'

结果:

  AAA BBBCCC DDD
AAA
BBB
CCC
DDD

通缉的结果。

  AAA BBBCCC DDD
AAA
BBB
CCC DDD


解决方案

 #!/斌/庆典
回声$ *
庆典myecho.sh$ @

请注意在$ @建设是不是来砸具体,并应与任何POSIX的shell工作(它至少有几许呢)。还要注意,给你想要的输出,你并不需要在所有引用的额外级别。 I.E.只需拨打上面的​​脚本,如:

  ./ test.sh 1 23 4

I'm trying to get quoted parameters of a bash script to safely be received by a nested script. Any ideas?

test.sh

#!/bin/bash
echo $*
bash myecho.sh $*

myecho.sh

#!/bin/bash
 echo $1
 echo $2
 echo $3
 echo $4

Sample:

bash test.sh aaa bbb '"ccc ddd"'

Result:

aaa bbb "ccc ddd"
aaa
bbb
"ccc
ddd"

Wanted result

aaa bbb "ccc ddd"
aaa
bbb
ccc ddd

解决方案

#!/bin/bash
echo $*
bash myecho.sh "$@"

Note the "$@" construct is not bash specific and should work with any POSIX shell (it does with dash at least). Note also that given the output you want, you don't need the extra level of quoting at all. I.E. just call the above script like:

./test.sh 1 2 "3 4"

这篇关于bash脚本接收和再通过参数报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆