简体   繁体   English

重击双引号内的单引号

[英]Bash single quotes inside double quotes

I want to execute the command: 我要执行命令:

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

The above command works fine when simply executing in terminal. 仅在终端中执行时,上述命令可以正常工作。 However, I am attempting to execute the command inside a wrapper function in bash. 但是,我试图在bash的包装函数内执行命令。 The wrapper function works by being passed a command and then executing that command basically. 包装函数的工作方式是传递命令,然后基本执行该命令。 For example, a call to the wrapperFunction: 例如,对wrapperFunction的调用:

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

and the wrapperFunction itself: 而wrapperFunction本身:

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

The problem is the single quotes in 'myApp adhoc' , because when running the command through wrapperFunction I get the error: error: no provisioning profile matches ''myApp' . 问题是'myApp adhoc'的单引号,因为通过wrapperFunction运行命令时出现错误: error: no provisioning profile matches ''myApp' It is not picking up the full name of the provisioning profile 'myApp adhoc' 它没有选择配置文件'myApp adhoc'的全名

EDIT: so say I also wanted to pass another string to wrapperFunction that was not part of the command to be executed. 编辑:所以我也想将另一个字符串传递给wrapperFunction,这不是要执行的命令的一部分。 For example I wanted to pass a string to display if the command fails. 例如,我想传递一个字符串以显示命令是否失败。 Inside wrapperFunction I could check $? 在wrapperFunction内部,我可以检查$? after the command and then display the failure string if $? 命令后,然后显示失败字符串(如果$?)。 -ne 0. How can I also pass a string with the command? -ne0。如何还通过命令传递字符串?

Don't mix code and data. 不要混用代码和数据。 Pass the arguments separately (this is what sudo and find -exec does): 分别传递参数(这是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'

To provide a custom error message: 要提供自定义错误消息:

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"

This produces the message Failed to frub the foo: frubber --foo=bar\\ baz . 这将产生消息“ Failed to frub the foo: frubber --foo=bar\\ baz

Since the method of quoting isn't important and isn't passed to commands or functions, the output may be quoted differently like here. 由于引用方法并不重要,也不会传递给命令或函数,因此输出的引用可能与此处不同。 They will still be functionally identical. 它们在功能上仍然相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM