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

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










你可能感兴趣的:(Linux)