shell脚本“if”测试未按预期工作

shell脚本“if”测试未按预期工作
#!/bin/bash

A="Message Agent is not buffering."
command > file
Buffering_status=`/usr/xpg4/bin/awk -F '[()\t:-]' '{print $NF}' file|sed '/^$/d'|/usr/xpg4/bin/awk '{$1=$1}1'|uniq -u|tail -1`

agent_status_count=`/usr/xpg4/bin/awk -F '[()\t:-]' '{print $NF}' file|sed '/^$/d'|/usr/xpg4/bin/awk '{$1=$1}1'|sed '$d'|sort -u|wc -l`
echo $Buffering_status $agent_status_count

if [[ $A == $Buffering_status  &&  $agent_status_count == 1 ]]
then

echo "executing fine part"

else

echo $Buffering_status $agent_status_count
echo "something wrong"

fi

调试输出:

+ A='Message Agent is not buffering.'
+ command
++ /usr/xpg4/bin/awk -F '[()\t:-]' '{print $NF}' file
++ sed '/^$/d'
++ /usr/xpg4/bin/awk '{$1=$1}1'
++ uniq -u
++ tail -1
+ Buffering_status='Message Agent is not buffering.'
++ /usr/xpg4/bin/awk -F '[()\t:-]' '{print $NF}' file
++ sed '/^$/d'
++ /usr/xpg4/bin/awk '{$1=$1}1'
++ sed '$d'
++ sort -u
++ wc -l
+ agent_status_count='       1'
+ echo Message Agent is not buffering. 1
Message Agent is not buffering. 1
+ [[ Message Agent is not buffering. == Message Agent is not buffering. ]]
+ [[        1 == 1 ]]
+ echo Message Agent is not buffering. 1
Message Agent is not buffering. 1
+ echo 'something wrong'
something wrong

答案1

==是字符串比较,正如kba指出的,“1”与“1”不是同一个字符串。接线员-eq会做你想做的事。

...

if [[ $A == $Buffering_status  &&  $agent_status_count -eq 1 ]]

...

相关内容