Linux:$* 不将单词组保留为单个参数

发布于 2025-01-13 12:07:39 字数 779 浏览 1 评论 0原文

我有一个父脚本和一个子脚本,

父脚本:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-01-20 12:07:39

对于 ksh,您可以执行以下操作:

#!/bin/ksh
echo "1:$1"
echo "2:$2"
if [[ $(basename $0) == 'tst' ]]
then
    # bash: P="${*@K}"
    P="$1"
    shift
    while [[ $# -gt 0 ]]
    do
        P="\"$P\" \"$1\""
        shift
    done

    echo "P:$P"
    eval ./tst2 $P
fi

也将其链接到 tst2 (ln tst tst2)。

请注意,我大约 20 年没有使用过 ksh,因此可能有一种更快的方法来创建 P(就像 bash 中一样)。

$ ./tst a 'b c d'
1:a
2:b c d
P:"a" "b c d"
1:a
2:b c d

对于 bash 你可以这样做:

#!/bin/bash
echo "1:$1"
echo "2:$2"
if [[ $(basename $0) == 'tst' ]]
then
    P="${*@K}"
    echo "P:$P"
    eval ./tst2 $P
fi

For ksh, you could do something like:

#!/bin/ksh
echo "1:$1"
echo "2:$2"
if [[ $(basename $0) == 'tst' ]]
then
    # bash: P="${*@K}"
    P="$1"
    shift
    while [[ $# -gt 0 ]]
    do
        P="\"$P\" \"$1\""
        shift
    done

    echo "P:$P"
    eval ./tst2 $P
fi

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).

$ ./tst a 'b c d'
1:a
2:b c d
P:"a" "b c d"
1:a
2:b c d

For bash you could just do:

#!/bin/bash
echo "1:$1"
echo "2:$2"
if [[ $(basename $0) == 'tst' ]]
then
    P="${*@K}"
    echo "P:$P"
    eval ./tst2 $P
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文