Results 1 to 2 of 2

Thread: Problem using Curl inside bash script

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Problem using Curl inside bash script

    Hi All,

    Here is what I am trying to do:

    1. testfile will have urls (one url per line)
    2. The script should read each line and use that line inside the curl command to download it manually

    Here is my script:

    root@Cheetah july13]# cat download.sh
    #!/bin/bash
    file="testfile"
    count=0
    while read -r line
    do
    echo $line
    let count=count+1
    name=$line
    cmd="curl -k -u \"usernameassword\""
    echo $cmd
    cmd2="$cmd -o $count.file $line"
    echo $cmd2
    $cmd
    done < $file

    Here is the testfile output:

    [root@Cheetah july13]# cat testfile
    'url1'
    'url2'
    'url3'

    Here is the issue that I am facing when executing the script:

    [root@Cheetah july13]# ./download.sh
    'url1'
    curl -k -u "usernameassword"
    curl -k -u "usernameassword" -o 1.file 'url1'
    curl: no URL specified!
    curl: try 'curl --help' or 'curl --manual' for more information
    'url1'

    NOTE: I have replaced sensitive information like URLs, and credentials with simple text like url and username/password.

    I have tried removing the single quotes in the testfile but it doesn't work. The reason I need(I am not sure if I really need, please comment) single quotes is that the URL has bunch of special characters like ?, #, ?. I tried without quotes and here is what I got:
    [root@Cheetah july13]# ./download.sh
    url1
    curl -k -u "usernameassword"
    curl -k -u "usernameassword" -o 1.file url1
    curl: no URL specified!
    curl: try 'curl --help' or 'curl --manual' for more information


    Please help.

    Thanks,
    Rakesh

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    453
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    hi,

    please use CODE tag.

    `$cmd' do not have an url, `$cmd2' has.

    don't use variables to store commands and execute them, use functions.
    Code:
    myCurl() {
       curl -k -u "username:password" "$@"
    }
    myCurl -o $countFile "$line"
    bash variables can't have a dot in their names.
    A problem clearly stated is a problem half solved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Slash seperator problem in my bash script
    By frhling in forum Shell scripting
    Replies: 3
    Last Post: 10th December 2012, 04:38 PM
  2. [Solved] Bash shell scripting: Use the variable inside the variable
    By manand in forum Shell scripting
    Replies: 5
    Last Post: 9th May 2012, 10:26 AM
  3. Bash Sockets: curl post data
    By eawedat in forum Shell scripting
    Replies: 3
    Last Post: 27th November 2011, 03:20 PM
  4. Replies: 3
    Last Post: 13th May 2010, 02:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •