我的代码:
x=$(awk -v i=$h -v j=17 'FNR == i {printf "%s ", $j}' newiptables.log) s="SPT=80"
引用的日志文件是:
Dec 26 09:17:51 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=313 TOS=0x00 PREC=0x00 TTL=64 ID=59334 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK PSH URGP=0 Dec 26 09:17:52 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=1440 TOS=0x00 PREC=0x00 TTL=64 ID=47303 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK URGP=0 Dec 26 09:17:52 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=1440 TOS=0x00 PREC=0x00 TTL=64 ID=47559 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK URGP=0
然后,需要检查脚本中的下一个条件是:
if [[ "$x" == "$s" ]] then < process if condition is true> else < process if condition is false> fi
如果条件不起作用
必要的修复:
s="SPT=80" # Not STP=80
此后已在问题中解决。
可能必要的修复:
if [[ "$x" == "$s" ]] # Space between if and [[ highly recommended then : it matches else : it does not match fi
如注释中所述,"%s "在awk脚本中使用尾随空格表示将尾随空格存储在其中"$x",这将破坏与的比较"$s"。
"%s "
awk
"$x"
"$s"