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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -