bash - How to check if while loop was executed? -
cat list.txt | grep '^http://.*\.osm\..*$' | while read line; fn=$(basename $line) do_something() done # todo: check if did
in case grep command returns nothing, won't enter loop , do_something() won't executed.
i can't check what's in $fn outside of while loop, see bash variable scope.
what's least invasive solution checking if do_something() executed here?
since while
loop runs in sub shell, cannot propagate values parent shell. there still ways wanted, though. here's one:
if grep -q '^http://.*\.osm\..*$' list.txt # loop run while read line # line done < <(grep '^http://.*\.osm\..*$' list.txt) else # no matches, loop not run # else fi
it has side effect of running grep
twice, avoided saving output of grep
, post-processing it, suggested in answer, in ways bit simpler understand...
Comments
Post a Comment