如何在shell脚本中使用curl?

26

我正在尝试运行这个shell脚本来在Ubuntu中安装RVM

#!/bin/bash
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"

bash < <(curl $CURLARGS $RVMHTTP)

但我收到以下错误:

语法错误:重定向意外

我也尝试过不使用变量,但结果相同,你能告诉我错在哪里吗?


您的问题已经在这里得到了回答:https://dev59.com/6G025IYBdhLWcg3w4qIx - favoretti
我在我的Mac上尝试了一下,它可以正常工作。我猜测这是Ubuntu的Bash(我相信是dash)。你可能需要检查dash的man页面。 - jaypal singh
http://linux.die.net/man/1/dash - jaypal singh
如果你收到“重定向意外”的错误提示,这可能意味着你使用的是 sh 而不是 bash,尽管已经在 shebang 行中指定了。 - tripleee
3个回答

26
#!/bin/bash                                                                                                                                                                                     
CURL='/usr/bin/curl'
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"

# you can store the result in a variable
raw="$($CURL $CURLARGS $RVMHTTP)"

# or you can redirect it into a file:
$CURL $CURLARGS $RVMHTTP > /tmp/rvm-installer

或者:

从URL执行Bash脚本


6
url=”http://shahkrunalm.wordpress.com“
content=”$(curl -sLI “$url” | grep HTTP/1.1 | tail -1 | awk {‘print $2′})”
if [ ! -z $content ] && [ $content -eq 200 ]
then
echo “valid url”
else
echo “invalid url”
fi

4

首先,你的示例看起来相当正确,并且在我的机器上运行良好。你可以选择另一种方式。

curl $CURLARGS $RVMHTTP > ./install.sh

现在所有的输出都存储在 ./install.sh 文件中,您可以编辑并执行该文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接