[英]Bash single quotes inside double quotes
我要執行命令:
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內部,我可以檢查$? 命令后,然后顯示失敗字符串(如果$?)。 -ne0。如何還通過命令傳遞字符串?
不要混用代碼和數據。 分別傳遞參數(這是sudo
和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
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
。
由於引用方法並不重要,也不會傳遞給命令或函數,因此輸出的引用可能與此處不同。 它們在功能上仍然相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.