0

我想写类似的东西

EXEC="sudo su -m root -c \"java Something\""
$EXEC &

但我收到以下错误:

Something": -c: line 0: unexpected EOF while looking for matching `"'
Something": -c: line 1: syntax error: unexpected end of file

如果我在命令行上编写命令,它就会执行。如果我将它存储在一个变量中并试图推断它 - 它不会。为什么?

4

1 回答 1

0

试试这个:

exec="ls -l \"/a b c\""
$exec

你会看到类似的东西:

ls: cannot access "/a: No such file or directory
ls: cannot access b: No such file or directory
ls: cannot access c": No such file or directory

这准确地显示了问题出在哪里 - 也就是说 - 变量的扩展是在分词之后完成的。

要使其工作,您可以使用 eval:

=$ eval "$exec"
ls: cannot access /a b c: No such file or directory

甚至:

=$ sh -c "$exec"

或者更好的是,不要运行这样的命令。不确定它的目的是什么,但考虑避免在变量中构建完整的命令行。

于 2013-02-12T10:23:27.877 回答