This question already has an answer here:
- Shell script read missing last line 8 answers
code snippet is below i'm simply trying to curl url details but loops is acting weirdly it's not ending the loop properly if more than 2 urls has been provided to text file & if one url is provided than not able to read that with out this line || [ -n url ]
.
while IFS= read -r url || [ -n url ] ; do
sh file.sh $url
done < "file.txt"
[ -n url ]
is always true, because the stringu
r
l
is not empty. You may have wanted to write[ -n "$url" ]
. Also, I am not certain that||
is logically correct. – AlexP Aug 8 '18 at 13:13||
is good logic -- we want to loop as long as either theread
reports success, or it populated the variable otherwise (as will happen if the file has extra contents after its last complete line -- of course, the better fix is to correct the file's formatting so all text is followed by a newline). But you're right about the OP needing to use[ -n "$url" ]
. – Charles Duffy Aug 8 '18 at 13:17./file.sh "$url"
, with the quotes around the expansion, and probably putting a</dev/null
on that inner command so it doesn't consume their stream fromfile.txt
. – Charles Duffy Aug 8 '18 at 13:18$url
will remain set from the previous iteration, won't it? – AlexP Aug 8 '18 at 13:48