zl程序教程

您现在的位置是:首页 >  其它

当前栏目

if条件语句

语句 条件 if
2023-09-14 09:01:13 时间
if判断文件、目录属性
[-f file]判断是否是普通文件,且存在
[-d file]判断是否是目录,且存在
[-e file]判断文件或目录是否存在
[-r file]判断文件是否可读
[-w file]判断文件是否可写
[-x file]判断文件是否可执行

1.判断数之间的大小

#!/bin/bash
#auto if test 
#by zgh 2019

NUM1=100
NUM2=200

if [ $NUME1 > $NUM2 ]

then

        echo "This $NUM1 greate $NUM2 !"

else

        echo "This $NUM1 little $NUM2 !"

 fi

 2.判断一个目录是否存在

#!/bin/bash
#auto if test1
#by authors zgh 2019

DIR=/tmp/20191126

if [ ! -d $DIR ]

then
        mkdir -p $DIR
        echo -e "\033[32m This $DIR Create successs! \033[0m"
else
        echo -e "\033[32m This $DIR is exist,Please exit.\033[0m"
fi

if [ -z "$a" ]这个表示当变量a的值为空时会怎么样
if [ -n "$a" ]表示当前变量a的值不为空
if grep -q ‘1231.txt;then表示如果1.txt种含有'123'的行时会怎么样
if [ ! -e file ];then表示文件不存在时会怎么样
if (( $a < 1 ));then...等同于if [ $a -lt 1 ];then ...
[]中不能使用<,>,==,!....等符号

3.判断一个文件是否存在

#!/bin/bash
#auto test files
#by authors zgh 2019

FILES=/tmp/test.txt

if [ ! -e  $FILES ];then
        echo "ok" >> $FILES
else
        echo -e "\033[32m---------------------------\033[1m"
        cat $FILES
fi

 4.if多条件判断

#!/bin/bash

scores=$1

if [ -z $scores ];then
        echo "Usage: {$0 60|80.}"
        exit
fi

if [[ $scores -gt 85 ]]
then
        echo "very good!";
elif [[ $scores -gt 75 ]]
then
        echo "good!";
elif [[ $scores -gt 60 ]]
then
        echo "pass!";
else
        echo "no pass!";
fi

编写监控网站是否被篡改的脚本

#!/bin/bash
#2020年01月15日
#by author zgh
################
curl -s 'http://192.168.72.130/forum.php'|grep -c "discuz" > /tmp/sh/http.txt
#curl -s  -I  --connect-timeout 3 -m 10  'http://192.168.72.130'  | grep  "HTTP/1.1"  | awk '{print $2}'>http.txt

status=`cat /tmp/sh/http.txt`

#if [[ $status -eq 200 ]] || [[ $status -eq 302 ]]
if [[ $status > 0 ]]

then
        echo $status

else
        echo "0"
fi