3

I'm trying to knock together a little batch script for downloading files, that takes a URL as its first parameter and a local filename as its second parameter. In testing I've learned that its tripping up on spaces in the output filename, so I've tried using sed to escape them, but its not working.

#!/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 the message

sed: outfile.txt: No such file or directory

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

What would be the correct syntax here?

4 Answers 4

5

quoting the arguments correctly, rather than transforming them might be a better approach

It's quite normal to expect to have to quote spaces in arguments to shell scripts

e.g.

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

called like so

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

alternatively, escape the spaces with a '\' as you call them

./myscript http://www.example.com my\ other\ filename\ with\ spaces
Sign up to request clarification or add additional context in comments.

5 Comments

Just reading up on quoting arguments in bash now. Thanks for the tip
Thanks for expanding upon this. Annoyingly, the machine I need to test it on has just lost its network connection, but I trust you on this and it makes sense to me now. Out of curiosity, what it the significance of the braces around the parameter numbers in your version?
It's how I was taught to expand shell variables. I think it's just so that you can disambiguate the variable from any other text, if you want to interpolate them into strings. e.g. FOO=foo; echo "${FOO}d" will print 'food'
What's with the backticks? Do you really want to take the output from curl, string-split it, and run it as a command?
I just put them in in place of the eval, I guess? It was six years ago. The useful part of the answer was the variable quoting.
4

I agree with cms. Quoting the input arguments correctly is much better style - what will you do with the next problem character? The following is much better.

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

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

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

2 Comments

guilty as charged! I have expanded my answer to include working "improved" examples.
There's still faulty quoting here. -o "$filename" "$1". And the eval is purely security-bug-fodder (what if your URL contains $(rm -rf ~)?)
2
curl -# -C - -o "$2" $1

Comments

0

if $2 is a text input then try

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

I generally avoid backslashes in sed delimiters are they are quite confusing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.