UNIX Shell控制结构—IF

时间:2021-03-29 18:54:47
流控制(Decision Making)
IF语句有三种格式:
第一种:if ... fi statement

下面是一个实例:
cat if1.sh
#!/bin/sh
a=10
b=20
#①
if [ $a -eq $b ]; then
  echo "a is equal to b";
fi
if [ $a -gt $b ]; then
  echo "a is great than b";
fi
#②
if [ $a -lt  $b ]
then
  echo "a is less than b";
fi
# the EOF
注意:
①条件和处理命令分开的一种写法:
if 条件; then
处理命令
fi
②条件和处理命令分开的另一种写法:
if 条件
then
处理命令
fi
这里需要根据个人习惯去选择。

上面的例子中,变量的值是赋死了的,若要给此脚本传递两个参数,可做如下修改:
cat if1.sh
#!/bin/sh
# a=10
# b=20
if [ $1 -eq $2 ]; then
  echo "the first number is equal to the second";
fi
if [ $1 -gt $2 ]; then
  echo "the first number is great than the second";
fi
if [ $1 -lt  $2 ]
then
  echo "the first number is less than the second";
fi
# the EOF

给脚本传递参数,只需要在sh命令后面添加即可,使用空格隔开:
sh if1.sh 1 2
the first number is less than the second
sh if1.sh 12 1
the first number is great than the second
sh if1.sh 1 1
the first number is equal to the second

第二种:if ... else ... fi,具体如下:
if [ expression ]
then
    statement(s) to be sxecuted if expression is true
else
    statement(s) to be sxecuted if expression is not true
fi

一个简单的实例
cat ifparam.sh
#!/bin/sh
if [ $# -lt 3 ]; then
   echo "Usage:`basename $0` arg1 arg2 arg3" >&2
   exit 1
fi
#EOF
echo "arg1:$1"
echo "arg1:$2"
echo "arg1:$3"
脚本注解:
1.$#表示参数输入的个数
2.basename $0打印文件的名称
3.若输入小于三个参数则,将输出一个信息,这个信息被当做是错误信息(>&2)。
执行脚本:
sh ifparam.sh scott tom
Usage:ifparam.sh arg1 arg2 arg3

sh ifparam.sh scott tom jim
arg1:scott
arg1:tom
arg1:jim

再来看一个测试:
cat ifeditor.sh
#!/bin/csh
#if [ -z $EDITOR ]①
#if [ -z "`echo $EDITOR`" ]③
#下面这种写法不能正确的计算出环境的值
#因为wc -c计算包括新的空的行
#if [ `echo $EDITOR | wc -c` -eq 0 ]②
if [ -z "`echo $EDITOR`" ]
 then
   echo "Your EDITOR environment is not set"
else
   echo "Using $EDITOR as the default editor"
fi
#EOF
sh ifeditor.sh
Your EDITOR environment is not set
这里使用三种方式去检测环境变量是否设置;
①直接报错:语法错误
②正确写法,使用test检测,-z表示如果为设置则长度为0返回值为true
③wc -c计算包括了空行,所以不准确

第三种:if ... elif ... fi,具体如下:
if [ expression 1 ]; then
   statement(s) to be sxecuted if expression 1 is true
elif [ expression 2 ]; then
   statement(s) to be sxecuted if expression 2 is true
elif [ expression 3 ]; then
   statement(s) to be sxecuted if expression 3 is true
else
   statement(s) to be sxecuted if no expression is true
fi
下面是一个比较两个数字大小的例子:
cat elif.sh
#!/bin/sh
if [ $1 == $2 ]; then
   echo "the first number is equal to the next"
elif [ $1 -gt $2 ]; then
   echo "the first number is great than the next"
elif [ $1 -lt $2 ]; then
   echo "the first number is great than the next"
else
   echo "None of the condition met"
fi
#EOF
当不输入任何数字的时候,也就是为空,结果如下:
sh elif.sh
the first number is equal to the next
当输入数字的时候,如下:
sh elif.sh 10 20
elif.sh: test: unknown operator ==
这种情况,我们可以将其错误信息输入到一个文件当中,如下:
sh elif.sh 10 20 > log.txt 2>&1
cat log.txt
elif.sh: test: unknown operator ==
当将“==”号修改为-eq,结果如下:
sh elif.sh 10 20
the first number is less than the next

下面是一个if的实例,包括这三种命令格式;
脚本的作用是创建一个目录,如果不输入任何值,则打印脚本的作用说明;
输入则提示是否创建,输入非提示,则报错误,否则按提示走。
#!/bin/sh
DIR=$1
if [ "$DIR" = "" ]; then
   echo "Usage:`basename $0` directory to create" >&2
   exit 1
fi


if [ -d $DIR ]; then
   echo "Directory $DIR exists"
else
   echo "The Directory does exist"
   echo -n "Create it now?[y..n]:"
   read ANS
   if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]; then
      echo "creating now"
      mkdir $DIR > log.txt 2>&1
      if [ $? -ne 0 ]; then
         echo "Errors creating the directory $DIR" >&2
         exit 1
      fi
      echo "Creating successful"
   elif [ "$ANS" = "n" ] || [ "$ANS" = "N" ]; then
      echo "Giving up creating directory $DIR"
   else
      echo "Bad input"
   fi
fi
#EOF
1.不输入参数
sh ifmkdir.sh
Usage:ifmkdir.sh directory to create
2.输入一个存在的目录,提示目录已经存在:
sh ifmkdir.sh test
Directory test exists
查看确实有test目录:
[ -d test ]
echo $?
0
创建test1目录:sh ifmkdir.sh test1
The Directory does exist
Create it now?[y..n]:y
creating now
Creating successful
3.执行脚本,但不想创建目录:
sh ifmkdir.sh test2
The Directory does exist
Create it now?[y..n]:n
Giving up creating directory test2
4.执行脚本时,不按照提示输入:
sh ifmkdir.sh test2
The Directory does exist
Create it now?[y..n]:d

Bad input

更多信息,参考:Decision making