Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 893
Vivek Kumar Avatar asked Aug 24 '15 11:08

Vivek Kumar


People also ask

Why is curl command not working?

We might have come across errors like “curl: command not found” while working in the terminal. This type of error comes due to only one reason: the relevant package is not installed. Curl is a very popular data transfer command-line utility used for downloading and uploading data from or to the server.

Is curl a shell command?

The curl command transfers data to or from a network server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). It is designed to work without user interaction, so it is ideal for use in a shell script.


3 Answers

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
like image 192
Abhishek Avatar answered Oct 17 '22 08:10

Abhishek


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.
like image 27
anubhava Avatar answered Oct 17 '22 06:10

anubhava


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
    
like image 44
Gordon Davisson Avatar answered Oct 17 '22 06:10

Gordon Davisson