9

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.

Vivek Kumar
  • 4,822
  • 8
  • 51
  • 85
  • 1
    ["I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ#BashFAQ.2F050.I.27m_trying_to_put_a_command_in_a_variable.2C_but_the_complex_cases_always_fail.21) – chepner Aug 24 '15 at 13:08

4 Answers4

18

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
Abhishek
  • 3,439
  • 2
  • 20
  • 10
  • No $cmd is not working in my case on ubuntu 14.04.... `vi@vi-Dell-7537(Desktop) $ ./t.sh curl -k "https://www.facebook.com/vivekkumar27june88" curl: (1) Protocol "https not supported or disabled in libcurl vi@vi-Dell-7537(Desktop) $ cat t.sh #!/bin/sh curlCmd="curl -k \"https://www.facebook.com/vivekkumar27june88\"" echo $curlCmd $curlCmd > ~/Desktop/fb.html` – Vivek Kumar Aug 24 '15 at 13:22
  • In this case, `eval` is needed to get the single-quotes in the command string interpreted. But `eval` comes with a huge collection of traps for the unwary, and you're much better off avoiding it. There are much better ways to do this, like storing the command as an array (or not storing it at all!). – Gordon Davisson Aug 24 '15 at 18:36
3

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.
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • HI anubhava! same error even after adding -k flag to curl. – Vivek Kumar Aug 24 '15 at 11:37
  • Check path of curl in your shell by running `type curl` and the check same inside the script. – anubhava Aug 24 '15 at 11:40
  • when running `$type curl` getting the following output `curl is hashed (/usr/bin/curl)`. But from script output is `curl is /usr/bin/curl`. Can you please tell me what hashed means here?? – Vivek Kumar Aug 24 '15 at 11:47
  • Hmm both seem to be running same `curl` binary. Not sure what could be the different from shell to your script. So copy/paste of `curl -k "https://www.facebook.com/vivekkumar27june88" -o ~/Desktop/fb.html` works fine but `bash ./t.sh` gives that error? – anubhava Aug 24 '15 at 11:51
  • But I don't understand why script is making this difference. Can you please explain...Thanks :) – Vivek Kumar Aug 24 '15 at 11:53
  • Not sure why it doesn't work for you as I have tried this `curl` command from shell as well from the script and it has worked fine both ways. – anubhava Aug 24 '15 at 11:55
1

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
    
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
0

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.

Vinay Joshi
  • 61
  • 1
  • 1