- if [ -z "$a" ] This indicates what happens when the value of variable a is empty.
Let's assume the following scenario, determine the number of lines in a file, and return a value when the number of lines is greater than 100
#!/bin/bash
n=`wc -l /tmp/wrsfsf`
if [ $n -gt 100 ]
then
echo deo
fi
The premise of the above scenario is that the file exists, but if the file is deleted by mistake in the production environment, that is, when the file does not exist, the following prompt will appear:
Therefore, we need to nest an if statement to judge this first. Whether the file exists, and pay attention to adding double quotation marks, only double quotation marks will take effect, as follows
#!/bin/bash
n=`wc -l /tmp/wrsfsf`
if [ -z "$n" ]
then
echo error
else
if [ $n -gt 100 ]
then
echo deo
fi
fi
The above code block can also be improved to the following code block, where we do not have nested if statements
#!/bin/bash
n=`wc -l /tmp/wrsfsf`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo deo
fi
Let’s modify it again, modify it to first determine whether the file exists, if not, return a result and exit the script
#!/bin/bash
if [ ! -f /tmp/wrsfsf ]
then
echo /tmp/wrsfsf not exist
exit
fi
n=`wc -l /tmp/wrsfsf`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo deo
fi
- if [ -n "$a" ] means that when the value of variable a is not empty,
-n is the opposite of the previous -z, which can be used to determine whether the variable exists and whether the file is not empty. The variable needs to be enclosed in quotation marks , if -n is followed by a file, no quotation marks are
required
[root@lijie-01 ~]# ls
11.txt 1.txt~ anaconda-ks.cfg bb.txt file1.sh ifi2.sh lijie.txt ??????.pdf sim.pid
123 1.txz~ a.txt b.txt file2.sh ifi3.sh log sed temp
1.txt 3.txt awk fi2.sh grep ifi.sh logs shell
[root@lijie-01 ~]# if [ -n file1.sh ];then echo ok;fi
ok
[root@lijie-01 ~]#
Then judge when the value of the variable is not empty,
[root@lijie-01 ~]# echo $c
[root@lijie-01 ~]# if [ -n "$c" ];then echo $c;else echo "c is null";fi
c is null
[root@lijie-01 ~]#
- if grep -q '123' 1.txt; then means what happens if there is a line with '123' in 1.txt
Here we use the execution result of a statement as the judgment condition of the if statement, the -q in the above command Indicates not to display the content filtered by grep, such as judging whether a file contains a certain string, such as judging whether there is a user zabbix among the system users, generally we think of the following methods, where -w means that the word zabbix is filtered
[root@lijie-01 ~]# grep -w 'zabbix' /etc/passwd
zabbix:x:997:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
[root@lijie-01 ~]#
Next, we can judge based on this result and output the desired result
[root@lijie-01 ~]# if grep -w 'zabbix' /etc/passwd;then echo zabbix exist;fi
zabbix:x:997:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
zabbix exist
[root@lijie-01 ~]#
If you don't want to output the filtered content, you can add -q
[root@lijie-01 ~]# if grep -wq 'zabbix' /etc/passwd;then echo zabbix exist;fi
zabbix exist
[root@lijie-01 ~]#
- if [ ! -e file ]; then what happens when the file does not exist
- if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
- [ ] cannot use symbols such as <,>,==,!=,>=,<=