awk if else语句中存在语法错误,该语句是我从另一个问题获取代码而无法修复的。 Bash one-liner code to output unique values。
有人可以更正此声明吗?
awk 'BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3} {print 0} else {if(output==0} {print 3} else {print output}}'
调试输出
awk 'BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}'
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
答案 0 :(得分:1)
if(output==0}
和if(output==3}
应该以封闭括号)
结尾,而不是封闭括号}
。
对于嵌套的if语句,您应该使用else if
,并且这些花括号仅对于多个操作是必需的。
END {if(output==3) print 0; else if(output==0) print 3; else print output}
只是为了好玩:
END {print output==3? 0: output==0? 3: output}
答案 1 :(得分:0)
错误消息中有一些线索,例如:
awk: cmd. line:1: ... END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
这真的很清楚。