字符串比较不正常 [英] String comparison not Working properly

查看:99
本文介绍了字符串比较不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code:

x=$(awk -v i=$h -v j=17 'FNR == i {printf "%s ", $j}' newiptables.log)
s="SPT=80"

引用的日志文件是:

The log file referenced is:

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

然后需要检查脚本中的下一个条件是:

Then the next condition in the script needs to be checked is:

if [[ "$x" == "$s" ]]
then 
 < process if condition is true>
else
 < process if condition is false>
fi

if条件不工作

The if condition is not working

推荐答案

必要的修复:

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,这将中断与$的的比较。

As noted in the comments, the use of "%s " with a trailing space in the awk script means that the trailing space is stored in "$x" and this will break the comparison with "$s".

这篇关于字符串比较不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆