如何将参数传递给已经通过管道进入Python进程的卷曲脚本?

时间:2017-04-13 20:04:56

标签: python shell curl

我在Github gist上有一个Python脚本,我可以用

从我的终端卷曲
curl -s https://gist.githubusercontent.com/.../script.py

脚本有一个执行的main函数,我可以将curl语句的输出传递给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

如何将两个参数传递给文件,作为我的脚本可以读取的标准输入或命令行选项?

2 个答案:

答案 0 :(得分:2)

来自CLI帮助:

  

-:程序从stdin读取(默认;交互模式,如果是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']

答案 1 :(得分:0)

您可以在本地保存脚本然后执行它。

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

您还可以将curl输出保存到文件:

curl some_link > some_file.py