bash - How to avoid calling external utility (grep) twice while maintaining return code & output? -
i have following bash function return property value java-style property file. property wasn't found, should return non-zero. if found, property's value printed & return code must zero.
function property_get() {     local pfile="$1"     local pname="$2"     if egrep "^${pname}=" "$pfile" 2>&1 >/dev/null;         local line="$(egrep "^${pname}=" "$pfile")"         printf "${line#*=}"         return 0 # success     else         return 1 # property not found     fi }   the question is: how avoid calling egrep twice? first exec status code, 2nd property value. if use $(grep parameters) notation, grep launched in subshell , can't it's return code , won't able determine success or failure of property searching.
this should work:
... local line if line=$(egrep "^${pname}=" "$pfile" 2>/dev/null); ...      
Comments
Post a Comment