【问题标题】:How do I pass in arguments to a curled script, that has been piped into the Python process?如何将参数传递给已通过管道传输到 Python 进程中的卷曲脚本?
【发布时间】:2017-04-13 20:04:55
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python shell curl


    【解决方案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']
    

    【讨论】:

      【解决方案2】:

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

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

      您还可以使用以下命令将 curl 输出保存到文件中:

      curl some_link > some_file.py
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-11
        • 2016-07-03
        • 1970-01-01
        • 2021-01-11
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 2013-05-31
        相关资源
        最近更新 更多