繁体   English   中英

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

[英]curl command not executing via shell script in bash

我正在学习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。

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

试试eval:

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

仅将此脚本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.

正如@Chepner所说,请阅读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

    set -x curl“ https://www.facebook.com/vivekkumar27june88 ”>〜/ Desktop / fb.html set + x

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

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

在ubuntu 14.04中安装以下软件

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

然后运行sudo service apache2 restart检查你的phpinfo()启用curl“cURL支持:启用”

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

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

echo $ RESULT

你会得到回应;

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM