Bash - 如何执行从不同脚本输出的脚本

时间:2016-09-15 17:34:52

标签: linux bash shell exec eval

我有一个生成bash脚本的CLI。如何在不重定向到.sh文件的情况下立即对其进行评估?

示例是更改以下内容:

~: toolWhichGeneratesScript > tmp.sh
~: chmod +x tmp.sh
~: ./tmp.sh

类似于:

~: toolWhichGeneratesScript | evaluate

3 个答案:

答案 0 :(得分:2)

您可以传递命令以使用bash -c(或sh -c):

运行
bash -c "$(toolWhichGeneratesScript)"
   -c        If the -c option is present, then  commands
             are read from the first non-option argument
             command_string.   If  there  are  arguments
             after the command_string, they are assigned
             to the positional parameters, starting with
             $0.

与shell的管道不同,这使得stdin可以自由地与脚本运行的提示和程序进行交互。

答案 1 :(得分:1)

shell从标准输入中读取其脚本:

toolWhichGeneratesScript | sh

(事实上,交互式shell也是如此;它的标准输入恰好是终端。)

请注意,您需要知道要使用哪个 shell;如果您的工具输出bash个扩展名,那么您必须将其传递给bash。此外,如果生成的脚本本身需要从标准输入读取,则会出现一些问题。

答案 2 :(得分:0)

尝试这样做:toolWhichGeneratesScript | bash的

相关问题