shell编程之条件表达式


在linux中可以作为 if 语句的表达式的语句有三种形式

一、命令形式

命令执行成功,等于返回0 (比如grep ,找到匹配)
执行失败,返回非0 (grep,没找到匹配)

if commend
then codes
fi

例子

read -p"输入用户名:" name
if id -u $name > /dev/null 2>&1#
then echo "$name exist"
else 
echo "$name does not exist!"

输入用户名: adins
adins does not exist

二、函数形式

函数执行成功,返回true,执行失败返回false。

#!/bin/bash
fun(){
    
    
   echo "adins"
}
if fun
then echo test
fi

三、test、[]形式

实际上test和[]形式的表达式 作用相同。
推荐一篇博客: Linux Bash Shell编程(八):条件判断与示例

猜你喜欢

转载自blog.csdn.net/qq_55796594/article/details/126163972