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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -