在bash脚本调用带有空格的命令行自变量 [英] Invoking a command line with spaces from a variable in a bash script

查看:460
本文介绍了在bash脚本调用带有空格的命令行自变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这将是容易的,但我已经浪费了几个小时这一点。

I thought it would be easy, but I already wasted a few hours on this.

我想从一个bash脚本中运行以下命令的CMake。在终端我键入

I want to run the following cmake command from inside a bash script. In a terminal I would type

cmake -G "Unix Makefiles" .

和它的作品。如果我复制此正是bash脚本里面它的工作原理也是如此。

and it works. If I copy this exactly inside the bash script it works as well.

但脚本是为了在多个平台上工作,它可能是MSYS Makefile文件,而不是Unix的Makefile文件。因此,我希望把命令在一个变量,其中的内容取决于平台并执行它。然而,这是我卡住了。我试过单/双引号我能想到的每一个组合,但毫无进展。

But the script is meant to work on multiple platforms, and it could be "MSYS Makefiles" instead of "Unix Makefiles". Therefore, I want to put the command in a variable, where the contents depends on the platform and execute it. However this is where I got stuck. I tried every combination of single/double quotes I could think of, but got nowhere.

我要的是沿东西线

c="cmake . -G \"Unix Makefiles\""
exec $c

但它总是会导致以下一些变化:

but it always results in some variation of the following:

CMake Error: Could not create named generator "Unix

我知道我能做到

if test [... this is unix ...]
    cmake . -G "Unix Makefiles"
else
    cmake . -G "MSYS Makefiles
fi

但由于该调用已进行好几次我宁愿避免它。

but since this call has to be made several times I'd rather avoid it.

任何建议?

推荐答案

这是最好不要使用评估不必要的。尽量不要把命令变量中。
你可以把这些选项,就好像

It is best not to use eval unnecessarily. Try not to put the command inside a variable. You can put the options as a variable though

if [ ... ]
  string="Unix makefiles"
else
  string="MSYS Makefiles"
else
  string="...."
fi
cmake -G "$string" #just call the command normally

这篇关于在bash脚本调用带有空格的命令行自变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆