首页 > 代码库 > if 判断练习

if 判断练习

例子:

#!/bin/sh

a=$1

b=$2

if [ $a -gt $b ]

then

  echo "yes,$a >= $b"

fi


if [ $a -le $b ]

  then

  echo "yes,$a <= $b"

fi



判断/server/scripts目录下if3.sh文件是否存在,不存在则创建:

#!/bin/sh

FILEPATH="/server/scripts"

if [ -e "$FILEPATH/if3.sh" ]

then

  echo "$FILEPATH/if3.sh exist."

fi

if [ ! -e "$FILEPATH/if3.sh" ]

then 

  [ ! -d $FILEPATH ] && mkdir -p $FILEPATH

  [ -d $FILEPATH ] && {

 cd $FILEPATH

 touch if3.sh

 echo "if3.sh is touch."

}

fi


判断系统内存是否小于100M,小于100M则发邮件报警:

#!/bin/sh

cur_free=`free -m|awk ‘/buffers\// {print $NF}‘`

if [ $cur_free -lt 100 ];

then 

echo "current memory is $cur_free."|mai -s "current memory is $cur_free." 23124@qq.com

fi


技术分享技术分享

技术分享

技术分享

技术分享

技术分享


if 判断练习