我觉得这很容易,但我已经浪费了几个小时.
我想从bash脚本中运行以下cmake命令.在一个终端我会打字
cmake -G "Unix Makefiles" .
Run Code Online (Sandbox Code Playgroud)
它的工作原理.如果我在bash脚本中完全复制它也可以.
但该脚本适用于多个平台,它可能是"MSYS Makefiles"而不是"Unix Makefiles".因此,我想将命令放在一个变量中,其中内容依赖于平台并执行它.然而,这是我被卡住的地方.我尝试了我能想到的单/双引号的每一个组合,但无处可去.
我想要的是一些事情
c="cmake . -G \"Unix Makefiles\""
exec $c
Run Code Online (Sandbox Code Playgroud)
但它总会导致以下一些变化:
CMake Error: Could not create named generator "Unix
Run Code Online (Sandbox Code Playgroud)
我意识到我能做到
if test [... this is Unix ...]
cmake . -G "Unix Makefiles"
else
cmake . -G "MSYS Makefiles
fi
Run Code Online (Sandbox Code Playgroud)
但由于必须多次拨打电话,我宁愿避免这种情况.
有什么建议吗?
最好不要eval
不必要地使用.尽量不要将命令放在变量中.您可以将选项作为变量
if [ ... ]
string="Unix makefiles"
else
string="MSYS Makefiles"
else
string="...."
fi
cmake -G "$string" # Just call the command normally
Run Code Online (Sandbox Code Playgroud)
用于eval
告诉 shell 重新解析命令行:
c="cmake . -G \"Unix Makefiles\""
eval "$c"
Run Code Online (Sandbox Code Playgroud)
或者,我喜欢使用数组来避免不必要的反斜杠和eval
:
# Store command in 4-element array: ["cmake", ".", "-G", "Unix Makefiles"].
# No backslash escapes needed.
c=(cmake . -G "Unix Makefiles")
# Ugly syntax for expanding out each element of an array, with all the spaces and
# quoting preserved to ensure that "Unix Makefiles" remains a single word.
"${c[@]}"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5621 次 |
最近记录: |