Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a loop set up like the following

#!/bin/bash
cat $1|while read line;do
    wget --limit-rate=50k http://url/$line.json -O got-$line;
    if [ "$?" -ne "0" ]; then
        rm got-$line;
    fi
    sleep 4;
done

But when i call it

./script inputfile.txt

I get the following error

rm: cannot remove `got-inputfile.txt': No such file or directory
--2012-12-30 12:18:06--  (start of wget output and everything works normal)

Like the loop is running before there is any input

Any suggestions ?

share|improve this question
    
The first line goes to stderr and the next to stdout. These are buffered and order is not guaranteed. –  jordanm Dec 30 '12 at 20:27
    
it doesn't happen when $1 is replaced with the actual file name. so does the stderr line only occur when a variable file is used ? –  user1281385 Dec 30 '12 at 20:32
    
run it with -x - either put set -x into the script or run as bash -x script ... and check the stderr for what is really happening. –  peterph Dec 30 '12 at 21:42
    
will try that. thanks –  user1281385 Dec 30 '12 at 22:49
    
Already mentioned quote the URL (with double quotes you don't need to change anything else) I would also change the logic to if ! wget ...; then. Slightly shorter and possibly easier to read... –  Gert van den Berg Dec 31 '12 at 7:02
add comment

1 Answer

I suspect url actually contains a & which causes the wget command to be run in background. You'd need to quote that url.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.