2012-07-23 72 views
2

我的if語句不能正常工作,我不明白爲什麼。爲什麼我的IF語句不起作用?

這是我

for LINE in $(cat someFile.txt) 
do 
inst=$(echo $LINE | awk -F, '{print $4}') 
echo $inst # To verify is displaying correctly 
if [ $inst == "UP" ] 
then 
    echo "Program skips this when $inst is UP" 
elif [ $inst == "DN" ] 
then 
    echo "Program skips this when $inst is DN" 
fi 
done 

當我使用-eq爲IF語句,程序會顯示這個UP^M: 0403-009 The specified number is not valid for this command.

任何想法?


編輯。找到答案:

由於某種原因程序在最後添加了返回字符。

在獲得列值後立即添加:inst=$(echo $inst | tr -d '\r')可解決問題。

+0

您是否已將您的腳本從窗口機器轉移到unix?在這種情況下,'dos2unix your_script_file'可能會幫助你。 – 2012-07-23 13:40:20

+0

它顯示什麼,你期望它顯示什麼? – beny23 2012-07-23 13:43:15

+0

@LucM:不,沒有從Windows機器上傳輸它 – esausilva 2012-07-23 13:45:27

回答

0

找到了答案:

出於某種原因,該方案是在末處加入一個回車符。

在獲得列值後立即添加:inst=$(echo $inst | tr -d '\r')可解決問題。

0

dos2 unix上的源文件也會有所幫助,在someFile.txt

+0

我是實際上在程序中創建someFile.txt,「someFile.txt」僅僅是一個例子。它實際上是創建一個CSV文件 – esausilva 2012-07-23 13:47:59