LINUX SHELL条件判断

Posted zengkefu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LINUX SHELL条件判断相关的知识,希望对你有一定的参考价值。

算术运算的条件判断
[] [[]]:
-eq
-ne
-lt
-le
-gt
-ge

(( )):
>
<
>=
<=
=

[[email protected] ~]# if (( 2 == 3));then echo ‘123‘; fi
[[email protected] ~]# if (( 2 >= 3));then echo ‘123‘; fi
[[email protected] ~]# if (( 2 <= 3));then echo ‘123‘; fi
123
[[email protected] ~]# if (( 2 < 3));then echo ‘123‘; fi
123
[[email protected] ~]# if (( 2 > 3));then echo ‘123‘; fi



字符串的条件判断

-z
-n
=
==
!=
<
>

文件属性的条件判断

-f
-d
-c
-w
-L
-x
-e
-b
-r



#!/bin/bash
if [ -e demo.sh ];then
echo "文件存在"
fi

fpath="/etc/passwd"

if [ -e $fpath ];then
echo file exists;
else
echo file no exists;
fi


[ -e "/etc/hosts" ] || (echo ‘/etc/hosts not exist";exit 1)

if [ "$?" -eq 1 ];then
exit
fi

echo "/etc/hosts 文件存在"


declare -i a
a=20
if [ $a -eq 20 ];then
echo "var a 20"
fi


if [ $a -gt 10 ];then
echo ‘var >10‘;
fi


if [ "$LOGNAME" != "ROOT" ];then
echo "root "
fi


if [ "Bill" >"Apple" ];then
echo " BILL >APPLE"
fi

str="Bill"
if [ -n $str];then
echo "234"
fi

 

#!/bin/bash
NUM1=100
NUM2=200
if (($NUM1 > $NUM2));then
echo "ok"
else
echo "ok1"
fi

 

#!/bin/bash
Dir=/tmp/20140909
if [ ! -d $Dir ];then
mkdir -p $Dir
echo -e "\033[32mthis $Dir exist\033[0m"
else
echo -e "\033[32mthis $Dir is exist,please exit.\033[0m"

fi


#!/bin/bash
FILES=/tmp/test.txt
if [ -f $FILES ];then
echo "ok">>$FILES
else
cat $FILES
fi

 

-a: 逻辑表达式 -a 逻辑表达式
-o: 逻辑表达式 -o 逻辑表达式

 

以上是关于LINUX SHELL条件判断的主要内容,如果未能解决你的问题,请参考以下文章

LINUX SHELL条件判断

Linux Shell编程 条件判断语法

Linux | shell脚本-条件判断if和循环语句for

Linux Shell角本中的条件判断

Linux学习8之Shell编程--条件判断

Linux实操篇—— Shell 编程入门变量运算符条件判断流程控制