shell的条件表达式
条件表达式,我们非常的常用,可以说,任何编程语言,都离不开条件表达式,但是每种变成语言的写法都不太一样,在shell中,有一种独特的写法。
[root@web02 ~]# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@web02 ~]# [ $? -eq 0 ] && echo '成功' || echo '失败'
成功
条件测试语句
条件测试语句,我们又叫做 test 语句。
格式 | 介绍 |
---|---|
test <条件> | 常规判断命令 |
[[ <条件> ]] | 支持运算符和正则的条件表达式 |
[ <条件> ] | 常用条件表达式 |
[ 条件 ] 不支持 > < 支持 -eq -lt -gt ..
[[ 条件 ]] 支持 > < -eq -lt -gt
test 条件 都支持 命令使用test
文件表达式选项
# 判断普通文件-f file
-f 判断文件是否存在
[root@web02 ~]# test -f /root/j.sh && echo '存在' || echo '不存在'
存在
[root@web02 ~]# test -f /root/sb.sh && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [ -f /tmp/1.txt ] && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [ -f /root/j.sh ] && echo '存在' || echo '不存在'
存在
[root@web02 ~]# [[ -f /root/j.sh ]] && echo '存在' || echo '不存在'
存在
[root@web02 ~]# [[ -f /root/5.sh ]] && echo '存在' || echo '不存在'
不存在
判断目录
-d 判断目录是否存在
[root@web02 ~]# test -d /root/web02 && echo '存在' || echo '不存在'
存在
[root@web02 ~]# test -d /root/web2 && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [ -d /root/web02 ] && echo '存在' || echo '不存在'
存在
[root@web02 ~]# [ -d /root/web2 ] && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [[ -d /root/web2 ]] && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [[ -d /root/web02 ]] && echo '存在' || echo '不存在'
存在
判断文件 -e exists
-e 判断文件是否存在
[root@web02 ~]# test -e /root/j.sh && echo '存在' || echo '不存在'
存在
[root@web02 ~]# test -e /root/web02 && echo '存在' || echo '不存在'
存在
[root@web02 ~]# test -e /root/web && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [ -e /root/web02 ] && echo '存在' || echo '不存在'
存在
[root@web02 ~]# [ -e /root/web2 ] && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [[ -e /root/web2 ]] && echo '存在' || echo '不存在'
不存在
[root@web02 ~]# [[ -e /root/web02 ]] && echo '存在' || echo '不存在'
存在
判断文件是否可读
-r read
[xxx@web02 ~]$ test -r ~/1 && echo '可读' || echo '不可读'
不可读
[xxx@web02 ~]$ chmod 644 1
[xxx@web02 ~]$ test -r ~/1 && echo '可读' || echo '不可读'
可读
判断文件是否可写
-w
[xxx@web02 ~]$ test -w ~/1 && echo '可写' || echo '不可写'
不可写
[xxx@web02 ~]$ test -w ~/1 && echo '可写' || echo '不可写'
可写
判断文件是否可执行
-x
[xxx@web02 ~]$ test -x ~/1 && echo '可执行' || echo '不可执行'
不可执行
[xxx@web02 ~]$ chmod +x 1
[xxx@web02 ~]$ test -x ~/1 && echo '可执行' || echo '不可执行'
可执行
判断文件是有内容
-s
[root@web02 ~]# test -s /root/dengluxinxi.txt && echo '有内容' || echo '无'
有内容
[root@web02 ~]# test -s /root/5.txt && echo '有内容' || echo '无'
无
判断文件是否有软链接
-L
[xxx@web02 ~]$ test -L /tmp/1.yx && echo '有' || echo '没有'
有
[xxx@web02 ~]$ test -L /tmp/1.ss && echo '有' || echo '没有'
没有
判断两个文件谁更新
-nt
[root@web02 ~]# test /root/1.txt -nt /root/j.sh && echo '后者新' || echo '前者新'
前者欣
判断两个文件谁更旧
-ot
[root@web02 ~]# test /root/1.txt -ot /root/j.sh && echo '前者旧' || echo '后者旧'
前者旧
拓展
注册账号
1.注册的账号和密码保存在文件之中
tls 123
zls 456
2.这个用户是否注册过
3.登录的时候 匹配用户和密码是否一致
tls 123
zls 456
------------------------
+---username:----------+
------------------------
+---password:----------+
------------------------
vim denglu.sh
#!/bin/bash
touch account.txt &>/dev/null
echo "+-----------------+"
echo "| 登陆账号 |"
echo "+-----------------+"
# 获取用户输入的账号和密码
echo "+-----------------+ "
read -p "请输入账号:" username
read -sp "请输入密码:" password
# 检查账号和密码是否为空
if [[ -z "$username" ]] || [[ -z "$password" ]]; then
echo "账号密码不能为空"
exit 1
fi
# 检查账号和密码是否包含空格
if [[ "$username" == *" "* ]] || [[ "$password" == *" "* ]]; then
echo "账号和密码不能包含空格"
exit 1
fi
# 检查账号和密码是否匹配
if grep -wq "^$username,$password" account.txt; then
echo "登录成功"
echo "+-----------------+"
else
echo "+-----------------------------+"
echo "| 账号或密码错误,或者没有账号 |"
echo "+-----------------------------+"
sh zhuche.sh
fi
vim zhuche.sh
#!/bin/bash
# 获取用户输入的账号和密码
echo "+-----------------+"
echo "| 注册账号 |"
echo "+-----------------+"
# 获取用户输入的账号和密码
read -rp "请输入要注册的账号:" username
echo
read -rsp "请输入密码:" password
echo
# 检查账号和密码是否为空
if [[ -z "$username" ]] || [[ -z "$password" ]]; then
echo "账号密码不能为空"
exit 1
fi
# 检查账号和密码是否包含空格
if [[ "$username" == *" "* ]] || [[ "$password" == *" "* ]]; then
echo "账号和密码不能包含空格"
exit 1
fi
# 检查账号是否已经注册过
if grep -wq "^$username" account.txt; then
echo "勾八,你已经注册过了"
exit 1
fi
# 保存账号和密码到文件中
echo "$username,$password" >> account.txt
echo "注册成功"
sh denglu.sh