简体   繁体   中英

What is this Shell/Awk command doing ?

APP_PORT=`${AWK} -F\" ' \
                       /org.apache.coyote.tomcat4.CoyoteConnector/ { 
                          getline
                          print $2
                       }' ${JBOSS_APP_CFG} 2>/dev/null`

Hi i am bit lost with all the layers of regexp and escape characters, so from the code i posted above i specialy want to understand what comes immediately after the F for field separator, so we want a quote so we escape it with the backslash but why is there no space after the -F ? and why is org.apache.coyote.tomcat4.CoyoteConnector placed between the single quote and what are the slashes that are surrounding it ?

Thanks

It assigns the text after the first double quote and until the second double quote or end of line, from each line following a line containing org.apache.coyote.tomcat4.CoyoteConnector , from a file with path stored in JBOSS_APP_CFG variable, to the APP_PORT variable.

Awk options accept values when they follow the option character in the same argument, as well as in the next argument. Ie the -F\\" argument, assigns value " to option -F (field separator). The backspace prevents shell from interpreting the double quote as a character starting double quoting.

The single quotes are used to prevent shell from interpreting the Awk script in any way, splitting it into words at whitespace and supplying it to Awk as several arguments, instead of one.

The /org.apache.coyote.tomcat4.CoyoteConnector/ is the pattern part from the pattern-action statement (the action is within curly braces, coming next). See http://www.gnu.org/software/gawk/manual/html_node/Patterns-and-Actions.html

The pattern part matches lines containing org.apache.coyote.tomcat4.CoyoteConnector . The action part reads next line and outputs its second field (fields being separated by " ).

It does read file stored in variable ${JBOSS_APP_CFG}
Search for org.apache.coyote.tomcat4.CoyoteConnector
Get the next line and print filed number 2
Then store this in variable APP_PORT
The -F\\" tells awk to use " as filed separator You can also write it like this

awk -v FS='"' (code) file
or
awk (code) FS='"' file
or 
awk (code) FS=\" file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM