【问题标题】:Why my IF statement is not working?为什么我的 IF 语句不起作用?
【发布时间】:2012-07-21 18:08:09
【问题描述】:

我的 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

当我在 IF 语句中使用-eq 时,程序会显示这个UP^M: 0403-009 The specified number is not valid for this command.

有什么想法吗??


编辑。找到答案:

由于某种原因,程序在末尾添加了一个返回字符。

在获取列值后添加:inst=$(echo $inst | tr -d '\r') 可以解决问题。

【问题讨论】:

  • 您是否将脚本从窗口机器转移到了 unix 机器?在这种情况下,dos2unix your_script_file 可能会对您有所帮助。
  • 它显示什么,您希望它显示什么?
  • @LucM:不,没有从 Windows 机器转移它
  • @beny23:程序在行尾添加了一个返回字符,这就是为什么它从来不是“UP”或“DN”

标签: shell unix scripting if-statement ksh


【解决方案1】:

找到答案:

由于某种原因,程序在末尾添加了一个返回字符。

在获取列值后添加:inst=$(echo $inst | tr -d '\r') 可以解决问题。

【讨论】:

    【解决方案2】:

    源文件上的dos2 unix也可能有帮助,someFile.txt

    【讨论】:

    • 我实际上是在程序中创建 someFile.txt,“someFile.txt”只是一个例子。它实际上是在创建一个 CSV 文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多