Ole*_*eev 5 bash shell redirect stdin
这个命令工作正常:
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Run Code Online (Sandbox Code Playgroud)
但是,我不明白如何stable
将参数传递给curl下载的shell脚本.这就是为什么我无法在我自己的shell脚本中实现相同功能的原因 - 它给了我./foo.sh: 2: Syntax error: redirection unexpected
:
$ cat foo.sh
#!/bin/sh
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Run Code Online (Sandbox Code Playgroud)
所以,问题是:这个stable
参数究竟是如何获取脚本的,为什么这个命令中有两个重定向,以及如何更改此命令以使其在我的脚本中运行?
Cha*_*ffy 12
那与之无关stable
,它与你的脚本使用有关/bin/sh
,而不是bash
.该<()
语法是不可用在POSIX壳,当作为被调用,它包括的bash /bin/sh
(在这种情况下关断出于兼容性原因非标准功能).
做你的shebang线#!/bin/bash
.
< <()
成语:要清楚发生了什么 - <()
用一个文件名替换,该文件名指的是它运行的命令的输出; 在Linux上,这通常是/dev/fd/##
类型文件名.< <(command)
然后,运行正在获取该文件并将其指向您的stdin ...这与管道的行为非常接近.
要理解为什么这个成语有用,请比较一下:
read foo < <(echo "bar")
echo "$foo"
Run Code Online (Sandbox Code Playgroud)
对此:
echo "bar" | read foo
echo "$foo"
Run Code Online (Sandbox Code Playgroud)
前者有效,因为读取是由同一个shell执行的,后来回应结果.后者不会,因为读取是在子shell中运行的,该子shell是为了设置管道然后被销毁而创建的,因此变量不再存在于后续的echo中.
bash -s stable
:bash -s
表示要运行的脚本将在stdin上进入.所有参数,然后,被馈送到在脚本$@
阵列($1
,$2
等),所以stable
变为$1
在stdin馈入的脚本运行时.