簡體   English   中英

Bash-如何執行從其他腳本輸出的腳本

[英]Bash - How to execute a script which is output from a different script

我有一個CLI生成一個bash腳本。 如何在不重定向到.sh文件的情況下立即進行評估?

示例是更改以下內容:

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

像這樣:

~: toolWhichGeneratesScript | evaluate

您可以傳入命令以使用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. 

與通過管道傳遞到外殼不同,這使stdin可以自由地與腳本運行的提示和程序進行交互。

Shell從標准輸入讀取其腳本:

toolWhichGeneratesScript | sh

(實際上,交互式外殼程序也是如此;它的標准輸入恰好是一個終端。)

注意,您需要知道要使用哪個外殼程序。 如果您的工具輸出bash擴展名,則必須將其通過管道傳輸到bash 另外,如果生成的腳本本身需要從標准輸入中讀取,則您會遇到一些問題。

嘗試執行以下操作:toolWhichGeneratesScript | 重擊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM