将参数传递给使用 bash -c 调用的脚本 [英] Passing arguments to a script invoked with bash -c

查看:42
本文介绍了将参数传递给使用 bash -c 调用的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我在 GitHub 上创建的 Bash 脚本以确保行为正确性(例如,它正确解析选项).我想这样做而不必在本地克隆存储库,所以我是这样做的:

curl -sSL https://github.com/jamesqo/gid/raw/master/gid |xargs -0 bash -c

我的问题是,如何将参数传递给相关脚本?我尝试了 bash -c --help,但没有奏效,因为它被解释为脚本的一部分.

谢谢!

解决方案

通过将 xargs 与 Bash 的 -c 选项一起使用,您实际上使事情变得过于复杂.

直接下载脚本

您无需克隆存储库即可运行脚本.直接下载就行了:

curl -o gid https://raw.githubusercontent.com/jamesqo/gid/master/gid

现在它已作为 gid 下载,您可以将其作为 Bash 脚本运行,例如,

bash gid --help

您还可以使下载的脚本可执行,以便将其作为常规 Unix 脚本文件运行(使用它的 shebang,#!/bin/bash):

chmod +x gid./gid --help

使用进程替换

如果您想运行脚本而不实际将其保存到文件中,您可以使用 Bash 进程替换:

bash <(curl -sSL https://github.com/jamesqo/gid/raw/master/gid) --help

I'm testing a Bash script I created on GitHub for behavioral correctness (e.g. that it parses options correctly). I want to do this without having to clone the repository locally, so here is how I'm doing it:

curl -sSL https://github.com/jamesqo/gid/raw/master/gid | xargs -0 bash -c

My question is, how can I pass arguments to the script in question? I tried bash -c --help, but that didn't work since it got interpreted as part of the script.

Thanks!

解决方案

You’re actually over-complicating things by using xargs with Bash’s -c option.

Download the script directly

You don’t need to clone the repository to run the script. Just download it directly:

curl -o gid https://raw.githubusercontent.com/jamesqo/gid/master/gid

Now that it’s downloaded as gid, you can run it as a Bash script, e.g.,

bash gid --help

You can also make the downloaded script executable in order to run it as a regular Unix script file (using its shebang, #!/bin/bash):

chmod +x gid
./gid --help

Use process substitution

If you wanted to run the script without actually saving it to a file, you could use Bash process substitution:

bash <(curl -sSL https://github.com/jamesqo/gid/raw/master/gid) --help

这篇关于将参数传递给使用 bash -c 调用的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆