跳至主要內容

curl 方式执行 shell 脚本时如何传参

安格Linuxlinuxcurl大约 1 分钟...

# -s
curl -s http://XXX.com/xx/xx.sh | bash -s arg1 arg2
# 或者 bash <
bash <(curl -s http://XXX.com/xx/xx.sh) arg1 arg2

前言

有时候 shell 脚本可以放在 http 页面上,不用 download,可以直接执行。

通常我们可以用 curl 的方式执行 http 页面上的 shell 脚本。 一般方式是:

curl http://XXX.com/xx/xx.sh | bash

这样脚本就可以在本地机器上执行了。

带参

有时也有需要传入参数的脚本。分为无名参数和具名参数(长选项和短选项)。

无名参

  1. -s 方式
curl -s http://XXX.com/xx/xx.sh | bash -s arg1 arg2
  1. bash <
# <( 之间没有空格
bash <(curl -s http://XXX.com/xx/xx.sh) arg1 arg2

具名参

由于直接调用了bash命令,因此在远程脚本需要传递具名参数时,为了区分是bash命令的参数还是远程脚本的,可以使用--作为区分,可以理解为分割线,--前面的比如-s属于bash,后面的-x abc -y xyz属于远程脚本的参数

curl -L http://XXX.com/xx/xx.sh | bash -s -- -x abc -y xyz

版权声明:本文为 CSDN 博主「Young4Dream」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Young4Dream/article/details/97245065open in new window

上次编辑于:
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3