双引号内的单引号

时间:2014-02-20 19:47:08

标签: ios xcode macos bash

我想执行命令:

xcodebuild -exportArchive -exportFormat IPA -archivePath myApp.xcarchive -exportPath myApp.ipa -exportProvisioningProfile 'myApp adhoc'

只需在终端中执行,上述命令就可以正常工作。但是,我试图在bash中的包装函数内执行命令。包装函数通过传递命令然后基本执行该命令来工作。例如,调用wrapperFunction:

wrapperFunction "xcodebuild -exportArchive -exportFormat IPA -archivePath myApp.xcarchive -exportPath myApp.ipa -exportProvisioningProfile 'myApp adhoc'"

和wrapperFunction本身:

wrapperFunction() {
    COMMAND="$1"
    $COMMAND
}

问题是'myApp adhoc'中的单引号,因为当通过wrapperFunction运行命令时,我收到错误:error: no provisioning profile matches ''myApp'。它没有获取配置文件'myApp adhoc'

的全名

编辑:所以说我还想把另一个字符串传递给wrapperFunction,而这个字符串不是要执行的命令的一部分。例如,如果命令失败,我想传递一个字符串来显示。在wrapperFunction里面我可以检查$?在命令之后然后显示失败字符串,如果$? -ne 0.我怎么能用命令传递一个字符串?

1 个答案:

答案 0 :(得分:3)

不要混合代码和数据。单独传递参数(这是sudofind -exec所做的):

wrapperFunction() {
    COMMAND=( "$@" )   # This follows your example, but could
    "${COMMAND[@]}"   # also be written as simply "$@" 
}

wrapperFunction xcodebuild -exportArchive -exportFormat IPA -archivePath myApp.xcarchive -exportPath myApp.ipa -exportProvisioningProfile 'myApp adhoc'

提供自定义错误消息:

wrapperFunction() { 
    error="$1" # get the first argument
    shift      # then remove it and move the others down
    if ! "$@"  # if command fails
    then 
      printf "%s: " "$error"  # write error message
      printf "%q " "$@"       # write command, copy-pastable
      printf "\n"             # line feed
    fi
}
wrapperFunction "Failed to frub the foo" frubber --foo="bar baz"

这会生成消息Failed to frub the foo: frubber --foo=bar\ baz

由于引用方法并不重要,并且没有传递给命令或函数,因此输出可能会像这里一样被引用。它们在功能上仍然相同。

相关问题