I want to concatenate arguments of xcodebuild as s

2019-09-17 23:22发布

I'm trying to build xcode project with shell, and now face a problem. the command xcodebuild has many args, and i may use some of them in some cases. I have a short example here, the destination arg has a space in it:

build_cmd='xcodebuild -project MyApp.xcodeproj'
destination="platform=iOS,name=generic/iOS Device"

if [ $# -ge 1 ];then
build_cmd=${build_cmd}' -destination '${destination}
fi

#echo $build_cmd
$build_cmd

but it failed to run, result with:

xcodebuild: error: Unknown build action 'Device'.

What's wrong here? Somebody help?


I want to give another example if you never use xcodebuild command:

filename="a b c.txt"
cmd='vi '${filename}
$cmd    # i wish to open "a b c.txt", but it opens 3 files, a, b, c.txt

how to run command with args which have space?

标签: shell
1条回答
别忘想泡老子
2楼-- · 2019-09-17 23:47

BASH FAQ entry #50: "I'm trying to put a command in a variable, but the complex cases always fail!"

#!/bin/bash

build_cmd=(xcodebuild -project MyApp.xcodeproj)
destination="platform=iOS,name=generic/iOS Device"

if [ $# -ge 1 ];then
  build_cmd+=(-destination "$destination")
fi

"${build_cmd[@]}"
查看更多
登录 后发表回答