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

bxgwgixi  于 11个月前  发布在  Shell
关注(0)|答案(5)|浏览(124)

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

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。

qqrboqgw

qqrboqgw1#

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

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

字符串

chhkpiq4

chhkpiq42#

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

ca1c2owp

ca1c2owp3#

正如@Chepner所说,去读BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!。总结一下,你应该如何做这样的事情取决于你的目标是什么。

  • 如果你不需要存储命令,* 不要 *!存储命令是很难正确的,所以如果你不需要,只要跳过这个混乱并直接执行它:
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“>桌面/fb. html set +x
.但如果需要,您也可以使用数组方法执行类似的操作:

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

wtlkbnrh

wtlkbnrh4#

在ubuntu 14.04中安装以下软件

  1. sudo apt-get安装php5-curl
  2. sudo apt-get安装 curl
    然后运行sudo服务apache 2 restart,检查您phpinfo()是否启用了curl“cURL support:enabled”
    然后在shell脚本中检查命令
    结果= curl -L "http://sitename.com/dashboard/?show=api&action=queue_proc&key=$JOBID" 2>/dev/null
    回显$Result
    你会得到响应;
    很好谢谢你
r1wp621o

r1wp621o5#

.sh文件中的内容

output=$(curl -s -v https://<websiteURL>/Login --stderr -)
      echo "My output----------------------------------------------------"
      echo $output

字符串

相关问题