我要执行命令:
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
}
Vấn đề là 'myApp adhoc'
中的单引号,因为通过 wrapperFunction 运行命令时出现错误:error: no provisioning profile matches ''myApp'
。它没有获取配置文件的全名 'myApp adhoc'
biên tập: 所以说我还想将另一个字符串传递给 wrapperFunction,它不是要执行的命令的一部分。例如,如果命令失败,我想传递一个字符串来显示。在 wrapperFunction 内部我可以检查 $?在命令之后然后显示失败字符串 if $? -ne 0. 我怎样才能用命令传递一个字符串?
不要混合代码和数据。分别传递参数(这是 sudo
Và find -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
sau đó
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
.
由于引用方法并不重要,也不会传递给命令或函数,因此输出可能会像此处那样以不同方式引用。它们在功能上仍然相同。
Tôi là một lập trình viên xuất sắc, rất giỏi!