Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash无意中分割命令_Bash_Shell_Command Line Arguments - Fatal编程技术网

Bash无意中分割命令

Bash无意中分割命令,bash,shell,command-line-arguments,Bash,Shell,Command Line Arguments,目前,我有一个rsync命令,由于网络状况不佳,该命令每~15分钟失败一次。我已经编写了一个脚本来重新运行rsync,但是由于bash无意中破坏了我传入的命令,该脚本无法正常工作: $ cat exit-trap.sh #!/bin/bash count=1 while : do echo ============== echo Run \#$count $@ if [[ $? -eq 0 ]] ; then

目前,我有一个rsync命令,由于网络状况不佳,该命令每~15分钟失败一次。我已经编写了一个脚本来重新运行rsync,但是由于bash无意中破坏了我传入的命令,该脚本无法正常工作:

$ cat exit-trap.sh
#!/bin/bash
count=1

while :
do
        echo ==============
        echo Run \#$count
        $@
        if [[ $? -eq 0 ]] ; then
                exit
        fi
        echo Run \#$count failed
        let count++
        sleep 15
done

$ ./exit-trap.sh rsync --output-format="@ %i  %n%L" source::dir target
==============
Run #1
Unexpected remote arg: source::dir
rsync error: syntax or usage error (code 1) at main.c(1348) [sender=3.1.1]
经过一段时间的探索,我猜argv中接收到的rsync是“[“rsync”、“--output format=@”、“I”、“n%L”、“source::dir”、“target”]。输出格式似乎无意中被分割成各个部分,从而导致语法错误。有没有办法解决这个问题

PS:到目前为止,我还尝试了
sh-c$@
sh-c\“$@”
,以及

  • /exit-trap.sh rsync--输出格式=\“@%i%n%L\”源::目录目标

  • /exit-trap.sh rsync--输出格式=\\\“@%i%n%L\\\”源::目录目标

  • /exit-trap.sh“rsync--输出格式=\”@%i%n%L\“源::目录目标”

这些都不起作用。

您需要使用
“$@”
,如下所述:

($@)展开为位置参数,从1开始。当展开发生在双引号内时,每个参数展开为一个单独的字。也就是说,“$@”相当于“$1”$2”


工作得很有魅力!非常感谢。