- 格式1:if 条件 ; then 语句; fi#(if开头fi结尾若是....而后)
- 格式2:if 条件; then 语句; else 语句; fi#(若是...而后...不然...)
- 格式3:if …; then … ;elif …; then …; else …; fi#(若是....而后....然而...而后.....)
- 逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=)# 注意处处都是空格#-gt大于,-lt小于,-eq等于
- 能够使用 && || 结合多个条件
- if [ $a -gt 5 ] && [ $a -lt 10 ]; then#&&等于and是与的意思。
- if [ $b -gt 5 ] || [ $b -lt 3 ]; then#||等于or是或的意思。
[ -f file ]判断是不是普通文件,且存在 eg:if [ -f X.sh ] ; then echo 1 ; fi #若是是普通文件,则打印1shell
[ -d file ] 判断是不是目录,且存在bash
[ -e file ] 判断文件或目录是否存在spa
[ -r file ] 判断文件是否可读code
[ -w file ] 判断文件是否可写变量
- [ -x file ] 判断文件是否可执行
- root用户对文件的读写比较特殊,即便一个文件没有给root用户读或者写的权限,root用户照样能够读或者写
- if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
- if [ -n "$a" ] 表示当变量a的值不为空
- if grep -q '123' 1.txt; then;fi若是1.txt中含有'123'的行时会怎么样#grep -q 表示过滤出,不打印
[root@lyf shell]# if grep '3' 1.sh;then echo "you cuowu";fi a=3 you cuowu [root@lyf shell]# if grep -q '3' 1.sh;then echo "you cuowu";fi you cuowu [root@lyf shell]#[root@lyf shell]# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@lyf shell]# grep -q root /etc/passwd [root@lyf shell]#
- if [ ! -e file ]; then ..文件不存在时会怎么样 #!不存在,反面的意思
- if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… # [ $a -lt 1 ]有不少空格,而且要写表示大小的字母,前者双括号就能够写正常写而且不用空格
- [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
- echo $? 若打印非0,检测出有语法错误