gpt4 book ai didi

bash - 从另一个脚本调用一个 Bash 脚本,通过引号和空格传递参数

转载 作者:行者123 更新时间:2023-11-29 08:41:05 26 4
gpt4 key购买 nike

我在 Linux 上做了两个测试 bash 脚本来弄清楚问题。

TestScript1 看起来像:

    echo "TestScript1 Arguments:"
echo "$1"
echo "$2"
echo "$#"
./testscript2 $1 $2

TestScript2 看起来像:

    echo "TestScript2 Arguments received from TestScript1:"
echo "$1"
echo "$2"
echo "$#"

当我按以下方式执行testscript1时:

    ./testscript1 "Firstname Lastname" testmail@example.com

期望的输出应该是:

    TestScript1 Arguments:
Firstname Lastname
testmail@example.com
2
TestScript2 Arguments received from TestScript1:
Firstname Lastname
testmail@example.com
2

但实际输出是:

    TestScript1 Arguments:
Firstname Lastname
testmail@example.com
2
TestScript2 Arguments received from TestScript1:
Firstname
Lastname
3

我该如何解决这个问题?我想获得所需的输出而不是实际输出。

最佳答案

在 Testscript 1 中引用你的参数:

echo "TestScript1 Arguments:"
echo "$1"
echo "$2"
echo "$#"
./testscript2 "$1" "$2"

关于bash - 从另一个脚本调用一个 Bash 脚本,通过引号和空格传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16988427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com