linux - 处理时如何传递+读取参数| (管道)?

标签 linux bash macos ubuntu centos

我正在尝试传递参数并将其打印出来。我不能。

尝试#1

curl www.site.com/bash/bashrc.sh | bash

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3391  100  3391    0     0  66809      0 --:--:-- --:--:-- --:--:-- 67820
There 0 arguments

尝试#2

curl www.site.com/bash/bashrc.sh | bash yellow red green

bash: yellow: No such file or directory
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3391  100  3391    0     0   130k      0 --:--:-- --:--:-- --:--:--  132k
(23) Failed writing body

我的目标是能够访问我在脚本中传递的参数:yellowredgreen

[0] : 庆典

[1] : 黄色

[2]:红色

[3]:绿色


bashrc.sh

#!/bin/bash

for i in $@
do echo $i
done

echo "There $# arguments"

....

我怎样才能做到这一点?

最佳答案

使用-s开关:

curl www.site.com/bash/bashrc.sh | bash -s yellow red green

yellow
red
green
There 3 arguments

根据man bash:

-s  If the -s option is present, or if no arguments remain after option processing,
    then commands are read from the  standard  input. This  option allows the positional
    parameters to be set when invoking an interactive shell.

关于linux - 处理时如何传递+读取参数| (管道)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657967/

相关文章:

c# - 如何在发布版本中附加第三方库

linux - 将标准输出保存到十六进制文件

linux - 在 Ubuntu Linux 上运行 Hadoop(多节点集群)

linux - 我的系统如何知道根名称服务器?

bash - sed 通过 ssh 连接 + `s' 的未知选项

bash - 可变长度的 Sed 替换

linux - bash,如何将标准输出到文件

linux - 无法在 Amazon Linux 实例上禁用 SSLv3

Ruby 1.8.7 世界可写检查? File.world_writable?只有 1.9.x

c++ - Mac OS X Lion 没有/Developer/usr 文件夹