tags:

views:

37

answers:

1
DOWNLOAD_PATH="sample.ext"
RATE_LIMIT="300K"
mkdir ../$DOWNLOAD_PATH
BASE_COMMAND="screen wget --continue --directory-prefix=../$DOWNLOAD_PATH --tries=2 --input-file=$DOWNLOAD_PATH"
$("${BASE_COMMAND} --limit-rate=${RATE_LIMIT}")

This does not work, but throws an error:

line 5: screen wget --continue --directory-prefix=../sample.ext --tries=2 --input-file=sample.ext --limit-rate=300K: No such file or directory

However, if i do

DOWNLOAD_PATH="sample.ext"
RATE_LIMIT="300K"
mkdir ../$DOWNLOAD_PATH
BASE_COMMAND="screen wget --continue --directory-prefix=../$DOWNLOAD_PATH --tries=2 --input-file=$DOWNLOAD_PATH"
COMPLETE_COMMAND="${BASE_COMMAND} --limit-rate=${RATE_LIMIT}"
$($COMPLETE_COMMAND)

everything works just fine... Why?

+4  A: 

Because when you put "${BASE_COMMAND} --limit-rate=${RATE_LIMIT}" in quotes, bash interprets it as a single word and tries to find a command with that name. It's looking for a program that literally has the name

screen wget --continue --directory-prefix=../sample.ext --tries=2 --input-file=sample.ext --limit-rate=300K

spaces and all. Obviously that doesn't exist. The solution is to leave that unquoted, so bash will interpret it as a list of space-separated words and understand that the command itself is just screen.

By the way, there's no need to use $() either unless you're trying to capture the output of the command. This would be fine:

DOWNLOAD_PATH="sample.ext"
RATE_LIMIT="300K"
mkdir ../$DOWNLOAD_PATH
BASE_COMMAND="screen wget --continue --directory-prefix=../$DOWNLOAD_PATH --tries=2 --input-file=$DOWNLOAD_PATH"
${BASE_COMMAND} --limit-rate=${RATE_LIMIT}
David Zaslavsky
Thanks! That solves it. I really should learn more about bash scripting, so that I wont repeat stupid mistakes like this one...
meanandvicious