我试图在if条件下比较来自csv文件的字符串读取。以下是我编写的示例代码:
export IFS="~"
while read a b c
do
echo "line Num:$a"
if [ "$b"=="sat" ];then
echo "Its saturday"
echo $c
elif [ "$b"=="sun" ];then
echo "Its sunday"
echo $c
else
echo "Its weekday"
echo $c
fi
done < "$1"
以下是csv文件:
1~sat~enjoy
2~sun~enjoy
3~mon~work
4~tue~work
5~sun~enjoy
以下是我得到的输出:
line Num:1
Its saturday
enjoy
line Num:2
Its saturday
enjoy
line Num:3
Its saturday
enjoy
line Num:4
Its saturday
enjoy
line Num:5
Its saturday
enjoy
需要以下输出:
line Num:1
Its saturday
enjoy
line Num:2
Its sunday
enjoy
line Num:3
Its weekday
work
line Num:4
Its weekday
work
line Num:5
Its sunday
enjoy
知道如何实现这个目标吗?
答案 0 :(得分:0)
试
export IFS="~"
while read a b c
do
echo "line Num:$a"
if [ "$b" = "sat" ];then
echo "Its saturday"
echo $c
elif [ "$b" = "sun" ];then
echo "Its sunday"
echo $c
else
echo "Its weekday"
echo $c
fi
done < "$1"
不要使用"$foo"=="$bar"
,因为bash只会看到一个令牌。
您也可以尝试
case "$b" in
sat) echo "It's saturday" ;;
sun) echo "It's sunday" ;;
*) echo "It's weekday" ;;
esac
echo $c