0

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"
johny
  • 1
  • 1
    `[ -n url ]` is always true, because the string `u` `r` `l` is not empty. You may have wanted to write `[ -n "$url" ]`. Also, I am not certain that `||` is logically correct. – AlexP Aug 08 '18 at 13:13
  • @AlexP, the `||` *is* good logic -- we want to loop as long as either the `read` 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 08 '18 at 13:17
  • Also, of course, the OP should also be running `./file.sh "$url"`, with the quotes around the expansion, and probably putting a ` – Charles Duffy Aug 08 '18 at 13:18
  • @CharlesDuffy: But `$url` will remain set from the previous iteration, won't it? – AlexP Aug 08 '18 at 13:48
  • @AlexP, no -- it populates the destination variable with the partial line's content even when it returns false. – Charles Duffy Aug 08 '18 at 13:58
  • @AlexP Thank you for correcting my mistake – johny Aug 09 '18 at 07:40
  • @CharlesDuffy yeah just check it was asked before. Thank you to for assistance – johny Aug 09 '18 at 07:41

0 Answers0