如果条件问题| AIX-bash |将文件中的值与硬编码的值进行比较

时间:2019-06-20 21:15:00

标签: bash unix

if [ $data == $dis ] 该行未在脚本中提供预期结果

在终端上尝试过,工作正常。脚本中应为“ true”,然后要从文件中删除该行

root@M5-L-G01FQY9:/mnt/c/Users/Vinita.a.wadhwani/Downloads/Rough# d="<disabled></disabled>"
root@M5-L-G01FQY9:/mnt/c/Users/Vinita.a.wadhwani/Downloads/Rough# c="<disabled></disabled>"
root@M5-L-G01FQY9:/mnt/c/Users/Vinita.a.wadhwani/Downloads/Rough# if [ $d == $c ]
> then
> echo "true"
> fi
true

代码如下:

dirmon=$1
action=$2
node=$(grep -l $dirmon *)
line=$(grep -n -m 1 $dirmon $node | sed  's/\([0-9]*\).*/\1/')
echo 'Performing' $action 'action on dirmon-'$dirmon 'which is present in node-'$node
no=$(($line+20))
data=$(sed -n "${no}p" $node) #gives results as <disabled></disabled>
echo $data
dis="<disabled></disabled>"
echo $dis
if [ $data == $dis ]
then
echo 'true'
fi
#echo 'false'

预期结果:

root@M5-L-G01FQY9:/mnt/c/Users/Vinita/Downloads/Rough# ./On* YELLOW start
Performing start action on dirmon-YELLOW which is present in node-NODE.txt
<disabled></disabled>
<disabled></disabled>
true

实际结果

root@M5-L-G01FQY9:/mnt/c/Users/Vinita/Downloads/Rough# ./On* YELLOW start
Performing start action on dirmon-YELLOW which is present in node-NODE.txt
<disabled></disabled>
<disabled></disabled>

1 个答案:

答案 0 :(得分:0)

原因是空白,由于空白导致比较失败。

一个有助于识别问题set -x的巨大命令用于调试。

修改后的代码:

#!/bin/bash
set -x
dirmon=$1
action=$2
node=$(grep -l $dirmon *)
line=$(grep -n -m 1 $dirmon $node | sed  's/\([0-9]*\).*/\1/')
echo 'Performing' $action 'action on dirmon-'$dirmon 'which is present in node-'$node
no=$(($line+20))
data=$(sed -n "${no}p" $node| sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' )
echo $data
dis="<disabled></disabled>"
echo $dis
if [ "$data" = "$dis" ]
then
echo 'true'
fi
#echo 'false'