【问题标题】:Pipe params into script管道参数到脚本
【发布时间】:2013-09-05 04:19:52
【问题描述】:

我可以将参数导入使用 curl 下载的脚本中,并让脚本接受它们为 $1、$2 等。试图让这个说 Hello world

$ curl mysite.com
echo hello $1

$ echo world | curl mysite.com
echo hello $1

$ echo world | $(curl mysite.com)
hello $1

【问题讨论】:

  • 为什么要将参数通过管道传递给脚本而不是将值放在前面? ./hello.sh $(echo "world") 在这个特定的示例中,这是对命令 echo 的无用使用。
  • 我实际上是从 curl 中提取脚本。 curl http://.com 下载文本 hello $1
  • 对不起,我还是不明白你的意思!如果curl 下载了一个包含hello $1 的脚本,那么就像我提到的,你仍然可以执行./hello.sh <some argument>

标签: bash pipe sh


【解决方案1】:

您可以将文本从curl 传送到sh

curl http://example.com/ | sh /dev/stdin world

/dev/stdin 脚本名称显然不能移植到没有/dev/stdin 的平台上。你可以用类似的方法解决这个问题

sh -c "$(curl http://example.com/)" _ world

sh -c '...' 之后的第一个参数进入$0,因此为此提供了虚拟参数_

无论哪种方式,除非您对 URL 完全有信心,否则这是非常不安全的。参见例如http://blog.classicalcode.com/2012/11/curl-pipe-sh-exploit-proof-of-concept/

对于没有curl 的测试,您可以简单地将curl http://example.com/ 替换为echo 'echo "hello $1"',或者只在标准输入上提供一个静态字符串。

sh /dev/stdin world <<<'echo "hello $1"'

(如果您的 shell 不是 Bash,或者您停留在 1990 年代中期而 Bash 2.x 和 Debian Potato 是热门新闻,您可以将 &lt;&lt;&lt;'here string' 替换为常规的 here document。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2013-10-24
    • 2016-07-03
    相关资源
    最近更新 更多