专题(九)if

一、知识点

  格式为:

     if [ 条件 ];then

      .......

     else

     ........

    if

多条件格式为:

     if [ 条件 ];then

      .......

     elif

     ........

    if

数值操作符使用 (( )) :< 、>、=

逻缉操作符使用 [[ ]]  :< 、>、=、==、!=

条件操作符使用 []: -lt , -eq , -gt , -ge 、-le

 

二、案例

1、多条件判断

a) 两条件判断

1、如果a>b且a<c 
 if (( a > b )) && (( a < c ))    // 使用数值操作符,这里的 a\b\c 均是 shell 变量
  
   或者 

   if [[ $a > $b ]] && [[ $a < $c ]] // 逻辑操作符,使用 $shell变量 获取具体值

    或者 
    
     if [ $a -gt $b -a $a -lt $c ]    // 条件操作符

2、如果a>b或a<c 
if (( a > b )) || (( a < c )) 或者 if [[ $a > $b ]] || [[ $a < $c ]] 或者 if [ $a -gt $b -o $a -lt $c ]

b) 三条件判断

  当 a 的值为 master 并且 b 的值为 backup 或 unknown ,则条件成立

if [ $a == "master" ] && ([ $b == "backup" ] || [ $b == "unknown" ]);then
    has_change=1
  fi

PS: 这里为后面 或 添加了 (), 使 () 内的条件和 && 的前面条件成并列判断

c) if ---elif -fi   (另一种多条件判断)

if [ $a -eq 1 ];then
   echo " a is 1"
elif [ $a -eq 2 ];then
  echo " a is 2"
else
  echo " a is 3"
fi

2、是否为数字

if [ -n "$(echo $1| sed -n "/^[0-9]\+$/p")" ];then 
  echo "$1 is number." 
else 
  echo 'no.' 
fi 


或

if [ -n `echo $1| sed -n "/^[0-9]\+$/p"` ];then 
  echo "$1 is number." 
else 
  echo 'no.' 
fi 

3、判断用户是否存在

#判断用户是否存在passwd中
i
=`cat /etc/passwd | cut -f1 -d':' | grep -w "$1" -c` if [ $i -le 0 ]; then echo "User $1 is not in the passwd" return 0 else #显示用户存在 echo "User $1 is in then passwd" return 1 fi

4、判断字符串是否空

 result=$(echo $ip | grep "${ip2}")      
 if [[ "$result" != "" ]];then       //第一种方式
      echo "result is not empty"
 fi

 if [ -n "$result" ];then                  //第二种方式:使用 测试操作符, 这个 $result 可以是一个shell执行语句,如:  if [ ! -n "$(echo $port| sed -n "/^[0-9]\+$/p")" ]
      echo "result is not empty"
 fi
if [ -z "$result" ];then      //第三种方式判断
echo "result is empty"
fi

5、字符串数字比较

VERSION="2.1.100"
if [[ "$VERSION" > "2.2.100" ]]
then
   DATA_DIR=/data
else
   DATA_DIR=../data
fi

a="test"
b="test"
if [ "$a" == "$b" ];then
echo "a and b have same value"
fi

if [ "$a" != "$b" ];then
echo " a and b do not have samve value"
fi

6、字符串是否以某个字符开头

 

if [[ $str == z* ]];then
    echo "str 字符串是以z开头"
fi

 

7、目录是否存在

build_dir=$(pwd)/build
if [ -d "$build_dir" ]      //目录存在时,条件成立
then
    rm -rf $build_dir
fi

if [ ! -d "$curr_dir/../logs" ];then      //目录不存在,条件成立
    mkdir $curr_dir/../logs
fi

8、文件是否存在

build_f=$(pwd)/build
if [ -f "$build_f" ]
then
   rm -f $build_f
fi

9、文件其它的判断

-r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真

 

 

参考资料:

https://blog.csdn.net/weixin_37569048/article/details/80039941

https://www.cnblogs.com/xibuhaohao/p/11542169.html

https://blog.csdn.net/feit2417/article/details/81603390

-r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真

posted @ 2021-05-18 15:04  夏之夜  阅读(102)  评论(0编辑  收藏  举报