New here? Register and read our Community Rules. Accounts are approved by hand.
Bash error
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 ?
0
Comments
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?
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