LinuxShell脚本编程基础5--数值,字符串,文件状态测试,((..))和[[..]]的使用

1、数值比较

! /bin/bash
echo "enter a score:"
read num1
if [ $num1 -ge 80 ]
then
 echo "Very Good"
elif [ $num1 -lt 80 -a $num1 -ge 60 ]
then
 echo "Good"
else
 echo "Low"
fi

其中:

  -eq  等于

  -ne  不等于

  -lt  小于

  -le  小于等于

  -gt  大于

  -ge  大于等于

  equal 等于  not equal 不等于  less 小于  greater 大于

  elif 也就是else if

  -a AND

  -o OR

2、((..))和[[..]]的使用

echo "enter a score:"
read num2
if (($num2 >= 80))
then
        echo "Very Good"
elif [[ $num2 -lt 80 && $num2 -ge 60 ]]
then
        echo "Good"
else
        echo "Low"
fi

3、字符串 和 文件状态测试

#! /bin/bash
echo "enter filename or workname:"
read name1
if [ -z $name1 ]
then
        echo "your enter is null"
        exit
else
        if [ -f $name1 ]
        then
                echo "this is a file"
        elif [ -d $name1 ]
        then
                echo "this is a workspace"
        else
                echo "not found file or workspace"
        fi
fi

  -z  为空串(长度为0)时返回真

  -n  为非空串时返回真

  -d  存在并且是一个目录时返回真

  -f  存在并且是正规文件时返回真

原文地址:https://www.cnblogs.com/sylovezp/p/4241010.html