Bash script to download files using Curl

I am trying to bring down a small batch of script to upload files for which the URL is the first parameter and the local file name as the second parameter. In testing, I found out that it disconnects in space in the name of the output file, so I tried to use sed to avoid them, but it does not work.

#!/bin/bash clear echo Downloading $1 echo filename=`sed -e "s/ /\\\ /g" $2` echo $filename echo eval curl -# -C - -o $filename $1 

but i get a message

sed: outfile.txt: no such file or directory

which suggests trying to load the output file as input to sed instead of treating the output file name as a string literal.

What would be the correct syntax here?

+4
source share
4 answers

quoting arguments correctly rather than converting them may be the best approach

It's pretty normal to expect that you need to quote spaces in the arguments for shell scripts

eg.

 #!/bin/bash clear echo Downloading $1 echo `curl -# -C - -o "${2}" "${1}"` 

called so

./myscript http://www.foo.com "my file"

alternatively, avoid spaces with the '\' character as you call them

 ./myscript http://www.example.com my\ other\ filename\ with\ spaces 
+5
source

I agree with cms. Choosing the right input arguments is a much better style — what will you do with the next symbol of the problem? The following is much better.

 curl -# -C - -o "$2" $1 

However, I hate people not answering the question asked, so here is the answer :-)

 #!/bin/bash clear echo Downloading $1 echo filename=`echo $2 | sed -e "s/ /\\\ /g"` echo $filename echo eval curl -# -C - -o $filename $1 
+4
source
 curl -# -C - -o "$2" $1 
+2
source

if $2 is text input then try

  echo $ 2 |  sed 's:: \\\: g'

I generally avoid backslashes in sed delimiters, they are pretty confusing.

0
source

All Articles