Linux 如何在 shell 脚本中使用 curl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8318678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:37:55  来源:igfitidea点击:

How to use curl in a shell script?

linuxbashubuntucurlsh

提问by san983

I'm trying to run this shell script in order to install RVM in an Ubuntu box

我正在尝试运行这个 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)

but I get the following error

但我收到以下错误

Syntax error: Redirection unexpected

语法错误:重定向意外

Also tested not using the variables, but same result, could you tell what I'm missing?

也没有使用变量进行测试,但结果相同,你能告诉我我错过了什么吗?

采纳答案by Tilo

#!/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

or:

或者:

Execute bash script from URL

从 URL 执行 bash 脚本

回答by ДМИТРИЙ МАЛИКОВ

Firstly, your example is looking quite correct and works well on my machine. You may go another way.

首先,您的示例看起来非常正确,并且在我的机器上运行良好。你可以走另一条路。

curl $CURLARGS $RVMHTTP > ./install.sh

All output now storing in ./install.shfile, which you can edit and execute.

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

回答by Krunal Shah

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