如何从变量运行脚本命令?
我试着用管道运行命令。如何从变量运行脚本命令?
基本:
single="ls -l"
$single
预期
管它的工作原理:
multi="ls -l | grep e"
$multi
ls: |: No such file or directory
ls: grep: No such file or directory
ls: e: No such file or directory
...难怪
bash < $multi
$multi: ambiguous redirect
下一个尝试
bash $multi
/bin/ls: /bin/ls: cannot execute binary file
只有
echo $multi > tmp.sh
bash tmp.sh
工作。
有没有办法执行更复杂的命令,而无需创建脚本执行?
您正在演示shell与内核之间的区别。
“ls -l”可由系统execve()调用执行。详情请参阅man execve
,但这可能对您而言太多了。
“ls -l | grep e”需要shell解释来设置管道。没有使用shell,'|'字符只是传递给execve()作为ls的参数。这就是为什么你看到“没有这样的文件或目录”错误。
解决方案:
cmd="ls -l | grep e"
bash -c "$cmd"
。永远不要将命令放入一个变量并尝试运行它。如果你想捕捉的输出简单地执行它
ls -l |grep
,使用$()
var=$(ls -l |grep ..)
命令来自不同的服务器,应该在目标机器上运行。因此它不能被硬编码。 – stacker 2010-08-12 16:29:55
见http://mywiki.wooledge.org/BashFAQ/050 – ghostdog74 2010-08-13 00:22:21
你需要一个heredoc
正确地做到这一点。在回答POSIX compliant way to see if a function is defined in an sh script时,我详细介绍了如何将脚本读入变量,以编程方式解析它以获取信息和/或根据需要修改它,然后从另一个脚本或shell函数中执行它。这基本上是你想要做什么,以及heredoc
使得有可能,因为它提供了一个文件描述符:
% multi='ls -l | grep e'
% sh <<_EOF_
> ${multi}
> _EOF_
< desired output >
这将解决您的简单的例子情况。看到我的其他答案更多。
-Mike
查看[BashFAQ/050](http://mywiki.wooledge.org/BashFAQ/050)。 – 2010-08-12 17:39:43