简体   繁体   中英

curl command not executing via shell script in bash

I'm learning shell scripting! for the same I've tried downloading the facebook page using curl on ubuntu terminal.

t.sh content

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

Getting error when running the script as

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

But if the run the command directly then it is working fine.

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

I will appreciate if anyone let me know the mistake I am doing in the script.

I've verified that curl library have ssl enabled.

A command embedded within a parenthesis runs as a sub-shell so your environment variables will be missing.

Try eval:

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

Create your script t.sh as this single line only:

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

As per 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.

As @Chepner said, go read BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail! . To summarize, how you should do things like this depends on what your goal is.

  • If you don't need to store the command, don't ! Storing commands is difficult to get right, so if you don't need to, just skip that mess and execute it directly:

     curl "https://www.facebook.com/vivekkumar27june88" > ~/Desktop/fb.html 
  • If you want to hide the details of the command, or are going to use it a lot and don't want to write it out each time, use a function:

     curlCmd() { curl "https://www.facebook.com/vivekkumar27june88" } curlCmd > ~/Desktop/fb.html 
  • If you need to build the command piece-by-piece, use an array instead of a plain string variable:

     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 
  • If you want to print the command, the best way to do it is usually with set -x :

    set -x curl " https://www.facebook.com/vivekkumar27june88 " > ~/Desktop/fb.html set +x

    ...but you can also do something similar with the array approach if you need to:

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

Install following softwares in ubuntu 14.04

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

then run sudo service apache2 restart check your phpinfo() is enable with curl "cURL support: enabled"

Then check your command in shell script

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

echo $RESULT

You will get response;

Thanks you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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