2017-04-13 27 views
1

我有一個Python腳本在Github的要點,我可以從我的終端捲曲與我如何將參數傳遞給一個已經傳入Python進程的捲曲腳本?

curl -s https://gist.githubusercontent.com/.../script.py 

腳本有被執行的一個主要功能,我可以管捲曲語句的輸出到Python,它執行腳本。

curl -s https://gist.githubusercontent.com/.../script.py | python 

上述說明適用,但我想爲腳本提供一些命令行參數,而不必將其下載到文件中。我現在面臨的問題是Python的命令把它後面是什麼來執行,而不是作爲管道文件的參數中的任何文字,所以

curl -s https://gist.githubusercontent.com/.../script.py | python arg1 arg2 

不工作,也沒有

curl -s https://gist.githubusercontent.com/.../script.py arg1 arg2 | python 

如何將兩個參數傳遞給文件,作爲標準輸入或腳本可以讀取的命令行選項?

回答

1

從CLI幫助:

-:程序從標準輸入讀取(默認值;交互模式下,如果一個tty)

所以你想要的是:

curl -s https://gist.githubusercontent.com/.../script.py | python - arg1 arg2 

不過請注意,這-也將出現在sys.argv(到位腳本的文件名)

$ echo "import sys; print sys.argv" | python - arg1 arg2 
['-', 'arg1', 'arg2'] 
+0

這工作完全按照我需要它。 – iamseiko

0

您可以在本地保存腳本,然後執行它。

curl -o some_filename.py some_link 
python some_filename.py arg1 arg2 

還可以節約捲曲輸出,到文件:

curl some_link > some_file.py