用于接收和重新传递引用参数的 Bash 脚本 [英] Bash script to receive and repass quoted parameters

查看:18
本文介绍了用于接收和重新传递引用参数的 Bash 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让嵌套脚本安全地接收 bash 脚本的引用参数.有什么想法吗?

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

示例:

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

结果:

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

想要的结果

aaa bbb "ccc ddd"
aaa
bbb
ccc ddd

推荐答案

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

请注意,$@"构造不是特定于 bash 的,应该适用于任何 POSIX shell(至少适用于破折号).另请注意,鉴于您想要的输出,您根本不需要额外的引用级别.IE.只需调用上面的脚本,如:

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天全站免登陆