您的位置:首页 > 运维架构 > Shell

shell if else 用法 syntax error near unexpected token `then'

2016-03-24 14:30 621 查看

1. 错误

#!/bin/bash

platform=$1

if[ "$platform" = "ibmaix64" ]

then

echo "$platform"

else

echo "hello ooo"

fi

Error : syntax error near unexpected token `then'

原因: 条件语句 [ 符号的两边都要留空格

if [ $platform = "winx64" ]-----------------------right 绿色标记为空格

if[ $platform = "winx64" ]-----------------------wrong

if [$platform = "winx64" ]-----------------------wrong

2. 总结

2.1 空格

a.定义变量时, =号的两边不可以留空格

b.if语句 [ 符号的两边都要留空格

c.字符串比较, =两边要留空格

d. if 和then 在同一行 要加;

2.2 if 语句

#!/bin/sh

a=10

b=20

if [ $a == $b ]

then

echo "a is equal to b"

fi

#!/bin/sh

a=10

b=20

if [ $a == $b ]

then

echo "a is equal to b"

else

echo "a is not equal to b"

fi

#!/bin/sh

a=10

b=20

if [ $a == $b ]

then

echo "a is equal to b"

elif [ $a -gt $b ]

then

echo "a is greater than b"

elif [ $a -lt $b ]

then

echo "a is less than b"

else

echo "None of the condition met"

fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: