簡體   English   中英

帶參數的 wget bash 腳本

[英]wget bash script with parameters

我正在使用 bash 腳本

wget -O - https://myserver/install/Setup.sh | bash

如何將參數傳遞給上述腳本以使其運行? 就像是

wget -O - https://myserver/install/Setup.sh parameter1 | bash

您還可以使用以下命令運行腳本:

wget -qO - 'https://myserver/install/Setup.sh' | bash -s parameter1

請參閱: man bash選項-s

 -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell or when reading input through a pipe.

或者使用-c選項。

bash -c "$(wget -qO - 'https://myserver/install/Setup.sh')" '' parameter1

''將參數$0定義為空字符串。 在基於普通文件的腳本調用中,參數$0包含調用者腳本名稱。

請參閱: man bash選項-c

 -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, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages.

bash (或sh或類似的)命令的標准格式是bash scriptfilename arg1 arg2 ... 如果您省略所有第一個參數(要運行的腳本的名稱或路徑),它會從 stdin 讀取腳本。 不幸的是,沒有辦法撇開第一個論點而忽略其他論點。 幸運的是,您可以將/dev/stdin作為第一個參數傳遞並獲得相同的效果(至少在大多數 unix 系統上):

wget -O - https://myserver/install/Setup.sh | bash /dev/stdin parameter1

如果您在一個沒有 /dev/stdin 的系統上,您可能需要尋找另一種方式來顯式指定 stdin(/dev/fd/0 或類似的東西)。

編輯:Léa Gris 建議bash -s arg1 arg2 ...可能是更好的方法。

暫無
暫無

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

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