注:
格式1、格式2:一个条件一个命令;
格式3:一个条件两个命令;
格式4:两个条件三个命令,注意条件的写法。
例1:
#!/bin/sh
#ifTest
#to show the method of if
echo -e "Enter the first integer:\c"
read FIRST
echo -n "Enter the second integer:"
read SECOND
if [ "$FIRST" -gt "$SECOND" ]
then
echo "$FIRST is greater than $SECOND"
elif [ "$FIRST" -lt "$SECOND" ]
then
echo "$FIRST is less than $SECOND"
else
echo "$FIRST is equal to $SECOND"
fi
执行:
[[email protected] sh]# ./test.sh
Enter the first integer:7
Enter the second integer:7
7 is equal to 7
[[email protected] sh]# ./test.sh
Enter the first integer:8
Enter the second integer:7
8 is greater than 7
[[email protected] sh]#
例2:
#!/bin/bash
#5.sh
declare
a STRING;
b STRING;
BEGIN
a=$1
b=$2
if [ "$a" = "$b" ] ;then
echo "a=b"
else
echo "a!=b"
fi
END;
时间: 2024-10-20 14:34:25