简体   繁体   English

这个Shell / Awk命令在做什么?

[英]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 ? 嗨,我对正则表达式和转义字符的所有层都迷失了,所以从我上面发布的代码中,我特别想了解F后面的字段分隔符之后的内容,因此我们需要一个引号,以便使用反斜杠对其进行转义,但是为什么在-F之后没有空格? and why is org.apache.coyote.tomcat4.CoyoteConnector placed between the single quote and what are the slashes that are surrounding it ? 为什么将org.apache.coyote.tomcat4.CoyoteConnector放在单引号和其周围的斜线之间?

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. 它从包含org.apache.coyote.tomcat4.CoyoteConnector行之后的每一行,从第一个双引号之后的文本,直到第二个双引号或该行的末尾,从路径存储在JBOSS_APP_CFG变量中的文件JBOSS_APP_CFGAPP_PORT变量。

Awk options accept values when they follow the option character in the same argument, as well as in the next argument. 当Awk选项在同一参数以及下一个参数中跟随选项字符时,它们会接受值。 Ie the -F\\" argument, assigns value " to option -F (field separator). -F\\"参数,将值"分配给选项-F (字段分隔符)。 The backspace prevents shell from interpreting the double quote as a character starting double quoting. 退格键可防止Shell将双引号解释为以双引号开头的字符。

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. 单引号用于防止shell以任何方式解释Awk脚本,将其拆分为空格处的单词,并将其作为多个参数(而不是一个)提供给Awk。

The /org.apache.coyote.tomcat4.CoyoteConnector/ is the pattern part from the pattern-action statement (the action is within curly braces, coming next). /org.apache.coyote.tomcat4.CoyoteConnector/是pattern-action语句中的模式部分(该动作在花括号内,接下来是)。 See http://www.gnu.org/software/gawk/manual/html_node/Patterns-and-Actions.html 参见http://www.gnu.org/software/gawk/manual/html_node/Patterns-and-Actions.html

The pattern part matches lines containing org.apache.coyote.tomcat4.CoyoteConnector . 模式部分匹配包含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} 它确实读取了存储在变量${JBOSS_APP_CFG}
Search for org.apache.coyote.tomcat4.CoyoteConnector 搜索org.apache.coyote.tomcat4.CoyoteConnector
Get the next line and print filed number 2 获取下一行并打印字段2
Then store this in variable APP_PORT 然后将其存储在变量APP_PORT
The -F\\" tells awk to use " as filed separator You can also write it like this -F\\"告诉awk使用"作为字段分隔符,您也可以这样写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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