从另一个脚本调用一个 Bash 脚本并传递带引号和空格的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16988427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:49:22  来源:igfitidea点击:

Calling one Bash script from another Script passing it arguments with quotes and spaces

bashshelldouble-quotessalt-stack

提问by nmadhok

I made two test bash scripts on Linux to make the problem clear.

我在 Linux 上做了两个测试 bash 脚本来明确问题。

TestScript1 looks like:TestScript1 看起来像:
    echo "TestScript1 Arguments:"
    echo ""
    echo ""
    echo "$#"
    ./testscript2  
TestScript2 looks like:TestScript2 看起来像:
    echo "TestScript2 Arguments received from TestScript1:"
    echo ""
    echo ""
    echo "$#"
When i execute testscript1 in the following way:当我以下列方式执行 testscript1 时:
    ./testscript1 "Firstname Lastname" [email protected]  
The desired Output should be:所需的输出应该是:
    TestScript1 Arguments:  
    Firstname Lastname  
    [email protected]  
    2
    TestScript2 Arguments received from TestScript1:  
    Firstname Lastname  
    [email protected]  
    2  
But the actual output is:但实际输出是:
    TestScript1 Arguments:  
    Firstname Lastname  
    [email protected]  
    2
    TestScript2 Arguments received from TestScript1:  
    Firstname
    Lastname      
    3  

How do i solve this problem? I want to get the desired output instead of the actual output.

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

采纳答案by Markku K.

Quote your args in Testscript 1:

在 Testscript 1 中引用您的参数:

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

回答by Olivier Dulac

You need to use : "$@"(WITH the quotes) or "${@}"(same, but also telling the shell where the variable name starts and ends).

您需要使用 :("$@"带引号)或"${@}"(相同,但也要告诉 shell 变量名的开始和结束位置)。

(and do NOT use : $@, or "$*", or $*).

(并且不要使用 : $@, or "$*", or $*)。

ex:

前任:

#testscript1:
echo "TestScript1 Arguments:"
for an_arg in "$@" ; do
   echo "${an_arg}"
done
echo "nb of args: $#"
./testscript2 "$@"   #invokes testscript2 with the same arguments we received

I'm not sure I understood your other requirement ( you want to invoke './testscript2' in single quotes?) so here are 2 wild guesses (changing the last line above) :

我不确定我是否理解您的其他要求(您想在单引号中调用 './testscript2'?)所以这里有 2 个大胆的猜测(更改上面的最后一行):

'./testscript2' "$@"  #only makes sense if "/path/to/testscript2" containes spaces?

./testscript2 '"some thing" "another"' "$var" "$var2"  #3 args to testscript2

Please give me the exact thing you are trying to do

请给我你正在尝试做的确切的事情

edit: after his comment saying he attempts tesscript1 "$1" "$2" "$3" "$4" "$5" "$6" to run : salt 'remote host' cmd.run './testscript2 $1 $2 $3 $4 $5 $6'

编辑:在他的评​​论说他尝试 tesscript1 "$1" "$2" "$3" "$4" "$5" "$6" 运行后: salt 'remote host' cmd.run './testscript2 $1 $2 $3 $4 $5 $6'

You have many levels of intermediate: testscript1 on host 1, needs to run "salt", and give it a string launching "testscrit2" with arguments in quotes...

您有许多中间级别:主机 1 上的 testscript1,需要运行“salt”,并给它一个字符串,启动“testscrit2”,参数用引号...

You could maybe "simplify" by having:

您可以通过以下方式“简化”:

#testscript1

#we receive args, we generate a custom script simulating 'testscript2 "$@"'
theargs="''"
shift
for i in "$@" ; do
   theargs="${theargs} '$i'"
done

salt 'remote host' cmd.run "./testscript2 ${theargs}"

if THAt doesn't work, then instead of running "testscript2 ${theargs}", replace THE LAST LINE above by

如果这不起作用,那么不要运行“testscript2 ${theargs}”,而是将上面的最后一行替换为

echo "./testscript2 ${theargs}" >/tmp/runtestscript2.$$  #generate custom script locally ($$ is current pid in bash/sh/...)
scp /tmp/runtestscript2.$$ user@remotehost:/tmp/runtestscript2.$$ #copy it to remotehost
salt 'remotehost' cmd.run "./runtestscript2.$$" #the args are inside the custom script!
ssh user@remotehost "rm /tmp/runtestscript2.$$" #delete the remote one
rm /tmp/runtestscript2.$$ #and the local one

回答by Linsong Guo

I found following program works for me

我发现以下程序对我有用

test1.sh 
a=xxx
test2.sh $a

in test2.sh you use $1to refer variable ain test1.sh

在 test2.sh 中,您$1用来引用atest1.sh 中的变量

echo $1

回声 $1

The output would be xxx

输出将是 xxx