curl 命令未通过 bash 中的 shell 脚本执行

问题描述 投票:0回答:5

我正在学习 shell 脚本!同样,我尝试在 ubuntu 终端上使用 curl 下载 facebook 页面。

t.sh内容

vi@vi-Dell-7537(Desktop) $ cat t.sh 
curlCmd="curl \"https://www.facebook.com/vivekkumar27june88\""
echo $curlCmd
($curlCmd) > ~/Desktop/fb.html

运行脚本时出现错误

vi@vi-Dell-7537(Desktop) $ ./t.sh 
curl "https://www.facebook.com/vivekkumar27june88"
curl: (1) Protocol "https not supported or disabled in libcurl

但是如果直接运行命令那么它就可以正常工作。

vi@vi-Dell-7537(Desktop) $ curl "https://www.facebook.com/vivekkumar27june88"
<!DOCTYPE html>
<html lang="hi" id="facebook" class="no_js">
<head><meta chars.....

如果有人让我知道我在脚本中犯的错误,我将不胜感激。

我已经验证curl库启用了ssl。

linux bash shell curl gnome-terminal
5个回答
18
投票

嵌入在括号中的命令作为子shell运行,因此您的环境变量将丢失。

尝试评估:

curlCmd="curl 'https://www.facebook.com/vivekkumar27june88' > ~/Desktop/fb.html"
eval $curlCmd

3
投票

创建您的脚本

t.sh
仅作为这一行:

curl -k "https://www.facebook.com/vivekkumar27june88" -o ~/Desktop/fb.html

根据

man curl

-k, --insecure

(SSL) This option explicitly allows curl to perform "insecure" SSL connections transfers.  
All  SSL  connections  are  attempted  to be made secure by using the CA certificate bundle
installed by default. This makes all connections considered "insecure" fail unless -k,
--insecure is used.

-o file

Store output in the given filename.

1
投票

正如 @Cepner 所说,请阅读 BashFAQ #50:我试图将命令放入变量中,但复杂的情况总是会失败!。总而言之,你应该如何做这样的事情取决于你的目标是什么。

  • 如果您不需要存储命令,!存储命令很难正确,所以如果不需要,就跳过那堆乱七八糟的东西,直接执行它:

    curl "https://www.facebook.com/vivekkumar27june88" > ~/Desktop/fb.html
    
  • 如果您想隐藏命令的详细信息,或者要经常使用它并且不想每次都写出来,请使用函数:

    curlCmd() {
        curl "https://www.facebook.com/vivekkumar27june88"
    }
    
    curlCmd > ~/Desktop/fb.html
    
  • 如果您需要逐段构建命令,请使用数组而不是纯字符串变量:

    curlCmd=(curl "https://www.facebook.com/vivekkumar27june88")
    for header in "${extraHeaders[@]}"; do
        curlCmd+=(-H "$header")   # Add header options to the command
    done
    if [[ "$useSilentMode" = true ]]; then
        curlCmd+=(-s)
    fi
    
    "${curlCmd[@]}" > ~/Desktop/fb.html    # This is the standard idiom to expand an array
    
  • 如果你想打印命令,最好的方法通常是使用

    set -x
    :

    设置-x 卷曲“https://www.facebook.com/vivekkumar27june88”>~/Desktop/fb.html 设置+x

    ...但如果需要,您也可以使用数组方法执行类似的操作:

    printf "%q " "${curlCmd[@]}"    # Print the array, quoting as needed
    printf "\n"
    "${curlCmd[@]}" > ~/Desktop/fb.html
    

0
投票

在ubuntu 14.04中安装以下软件

  1. sudo apt-get install php5-curl
  2. sudo apt-get安装curl

然后运行 sudo service apache2 restart 检查你的 phpinfo() 是否启用了curl“cURL support:enabled”

然后在 shell 脚本中检查你的命令

结果=

curl -L "http://sitename.com/dashboard/?show=api&action=queue_proc&key=$JOBID" 2>/dev/null

回显$RESULT

您将得到回复;

谢谢你。


0
投票

.sh 文件中的内容

      output=$(curl -s -v https://<websiteURL>/Login --stderr -)
      echo "My output----------------------------------------------------"
      echo $output
© www.soinside.com 2019 - 2024. All rights reserved.