Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a loop set up like the following

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

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 ? – exussum 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 – exussum 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

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

Your Answer


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.