【问题标题】:Problems with bash and its quoting rulesbash 及其引用规则的问题
【发布时间】:2013-10-18 15:28:20
【问题描述】:

我对 bash 的引用规则有疑问,我不知道如何解决。 我想在某种配置文件中有一个变量,可以在其中指定命令行选项列表。就我而言,它用于 rsync: rsync 具有命令行选项“--rsh”,它允许您指定它在另一端使用的远程shell。例如:

rsync -v --rsh="ssh -p4711 -lhttp" /tmp/testfile remoteMachine.x:/tmp/testfile

这很完美!但是,如果您尝试在脚本中执行此操作,您希望允许在变量中指定所有选项,如下所示:

#!/bin/bash

OPTS="-v --rsh=\"ssh -p4711 -lhttp\""
rsync $OPTS testfile remoteMachine.x:/tmp/testfile

执行此脚本时,它将失败并出现以下错误:

rsync: -p4711: unknown option

这是因为 bash 做了一些讨厌的引用/转义,我不知道为什么(你可以看到 bash 用 'bash -x' 做了什么):

+ OPTS='-v --rsh="ssh -p4711 -lhttp"'
+ rsync -v '--rsh="ssh' -p4711 '-lhttp"' testfile remoteMachine.x:/tmp/testfile

我尝试了弱引号和强引号 (", ') 的多种组合,但没有任何效果...您能帮帮我吗,为什么 bash 会这样做以及如何解决这种奇怪的行为?

来自科隆的感谢和问候!

【问题讨论】:

标签: linux bash shell quotes


【解决方案1】:

使用数组:

OPTS=(-v --rsh="ssh -p4711 -lhttp")
rsync "${OPTS[@]}" testfile remoteMachine.x:/tmp/testfile

【讨论】:

  • 谢谢!这有帮助 :) 有点奇怪,我不能把它写在一个字符串中并防止 bash 做这种讨厌的引用,但它可以工作:) 谢谢!
【解决方案2】:

两种选择:通过RSYNC_RSH 环境变量传递--rsh 选项或使用--portlogin@host:path 语法直接将端口和登录选项传递给rsync

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2022-11-15
    • 2021-09-30
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多