2017-08-29 62 views
0

我有一个简单的if else循环内while,但它没有给我正确的输出需要。如果循环赢,而不工作在shell脚本

#!/bin/bash 

set -x 
inputFile=/data2/example/output1.txt 
while read col1 col2 col3; do 
if [ $col2=='Success' ] 
then 
echo " This is Green" 
else 
echo " This is Red" 
fi 
done < ${inputFile} 

输出我得到的是:

+ read col1 col2 col3 
+ '[' Success==Success ']' 
+ echo ' This is Green' 
This is Green 
+ read col1 col2 col3 
+ '[' Success==Success ']' 
+ echo ' This is Green' 
This is Green 
+ read col1 col2 col3 
+ '[' Failed==Success ']' 
+ echo ' This is Green' 

这不是进入else部分,不断显示This is Green

+0

"$col2" = "Success" 

那么,什么是你输入什么呢? – Danieboy

回答

0

这是一个小的语法错误:

if [ "$col2" == Success ] 

这解决了我的问题。

0

这是您将变量与字符串进行比较的方式。改用的

$col2 == 'Success'