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

超、凢脫俗 2022-07-27 15:00 259阅读 0赞

1. 错误

#!/bin/bash
platform=$1

if[ “$platform” = “ibmaix64” ]
then
echo “$platform”

else

  1. 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

发表评论

表情:
评论列表 (有 0 条评论,259人围观)

还没有评论,来说两句吧...

相关阅读