Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Bash error
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Bash error

exussumexussum Member
edited December 2012 in Help

Im struggling to understand why this happens.

Im wgetting thing by number from a list of numbers with the following script

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

Read the file passed each line try the URL and output it to got-number

If the wget command doesnt exit as 0 (it wasnt a 200 status code) - remove the file wget output

sleep for 4 second and try again

The output of this is using test.txt is

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

How can i stop it trying to remove the first file ?

Comments

  • arieonlinearieonline Member
    edited December 2012
    #!/bin/bash
    cat $1|while read line;do
    out=`wget --limit-rate=50k http://url/$line.json -O got-$line`;
    if [ $out -ne 0 ]; then
        rm got-$line;
    fi
    sleep 4;
    done
    
  • line 4: [: -ne: unary operator expected

    comes back

    It seems to be running the inside of the while with nothing on the first run

    Like a do while loop i guess instead of a while.

    Interestingly the error goes when $1 is replaced with a file name

  • How about something like this?

    #!/bin/bash
    
    for line in $(cat $1)
    do
      wget --limit-rate=50k http://url/$line.json -O got-$line 
      [[ $? -ne 0 && -f "got-$line" ]] && rm -f got-$line
      sleep 4
    done
    
  • luxorluxor Member
    edited December 2012

    May want to check for blank lines or comments in your list.

    #!/bin/bash
    
    cat "$1" | while read line; do
      test -z "$line" -o "${line:0:1}" == '#' && continue
      OUT_FILE="got-$line"
      if ! wget --quiet --limit-rate=50k "http://url/$line.json" -O "$OUT_FILE" ;then
        test -f "$OUT_FILE" && rm "$OUT_FILE"
      fi
      sleep 4
    done
Sign In or Register to comment.