簡體   English   中英

用shell腳本進行bash並啟動交互式bash

[英]Piping a shell script to bash and launch interactive bash

考慮example.com上的以下shell腳本

#/bin/bash

export HELLO_SCOPE=WORLD
eval $@

現在,我想以最簡單的方式下載並執行帶有參數的shell腳本,並能夠啟動設置了HELLO_SCOPE變量的交互式bash終端。

我努力了

curl http://example.com/hello_scope.sh | bash -s bash -i

但是它立即退出外殼。 據我了解,這是因為curls stdout (腳本)仍然是bash的stdin ,從而阻止了bash的交互啟動(因為這需要我的鍵盤為stdin )。

有沒有一種方法可以避免這種情況,而無需執行使用shell腳本創建臨時文件的額外步驟?

你可以source它:

# open a shell
. <(curl http://example.com/hello_scope.sh)
# type commands ...

您可以下載該腳本(例如使用wget )並獲取該腳本的source ,不是嗎?

script_name="hello_scope.sh"
[[ -f  $script_name ]] && rm -rf "$script_name"
wget "http://example.com/$script_name" -O "$script_name" -o /dev/null 
&& chmod u+x "$script_name" 
&& source "$script_name"

您可以使用. "$script_name" . "$script_name"而不是source "$script_name".符合POSIX)。 您可以在腳本中編寫之前的代碼,並通過設置變量$HELLO_SCOPE source代碼以使其具有交互式shell。

最后,您可以在遠程Shell腳本中刪除eval行。

暫無
暫無

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

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