Linux:$* 不将单词组保留为单个参数
我有一个父脚本和一个子脚本,
父脚本:
echo 'Parent script 1st arg '$1
echo 'Parent script 2nd arg '$2
PARAMS=$*
ksh child_script.ksh $PARAMS
子脚本:
echo 'Child script 1st arg '$1
echo 'Child script 2nd arg '$2
我通过传递两个参数来执行父脚本,如下所示
ksh parent_script.ksh ABC 'DEF XYZ'
我得到的输出为,
Parent script 1st arg ABC
Parent script 2nd arg DEF XYZ
Child script 1st arg ABC
Child script 2nd arg DEF
我想将 DEF XYZ 作为单个参数传递,这就是为什么我用单引号括起来并传递相同的内容。它被父脚本视为单个参数。稍后,当使用 $* 对其进行分配时,不会保留单词分组。相反,它被作为两个不同的论点传递。请告知为什么参数没有按原样通过,有什么方法可以纠正吗?
预期输出,
Parent script 1st arg ABC
Parent script 2nd arg DEF XYZ
Child script 1st arg ABC
Child script 2nd arg DEF XYZ
I am having a parent script and a child script,
Parent script:
echo 'Parent script 1st arg '$1
echo 'Parent script 2nd arg '$2
PARAMS=$*
ksh child_script.ksh $PARAMS
Child script:
echo 'Child script 1st arg '$1
echo 'Child script 2nd arg '$2
I am executing the parent script by passing two argument as below
ksh parent_script.ksh ABC 'DEF XYZ'
I am getting the output as,
Parent script 1st arg ABC
Parent script 2nd arg DEF XYZ
Child script 1st arg ABC
Child script 2nd arg DEF
I want to pass DEF XYZ as a single argument which is why I enclosed in single quotes and passed the same. It is considered as a single argument by the parent script. Later, when it is assigned using $* the grouping of words is not preserved. Rather it is getting passed as two different argument. Please advice why the arguments are not passed as it is and is there any way to correct it?
Expected output,
Parent script 1st arg ABC
Parent script 2nd arg DEF XYZ
Child script 1st arg ABC
Child script 2nd arg DEF XYZ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 ksh,您可以执行以下操作:
也将其链接到 tst2 (
ln tst tst2
)。请注意,我大约 20 年没有使用过 ksh,因此可能有一种更快的方法来创建 P(就像 bash 中一样)。
对于 bash 你可以这样做:
For ksh, you could do something like:
link this to tst2 as well (
ln tst tst2
).Note, I haven't used ksh in about 20 years so there may be a quicker way to create P (as there is in bash).
For bash you could just do: