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 ?

stderrand the next tostdout. These are buffered and order is not guaranteed. – jordanm Dec 30 '12 at 20:27-x- either putset -xinto the script or run asbash -x script ...and check thestderrfor what is really happening. – peterph Dec 30 '12 at 21:42if ! wget ...; then. Slightly shorter and possibly easier to read... – Gert van den Berg Dec 31 '12 at 7:02