- Format 1: if condition; then statement; fi command line writing 1:
[root@lijie-01 ~]# a=5
[root@lijie-01 ~]# if [ $a -gt 3 ] //这里的$a表示取a的值,gt是great than表示大于的意思,小于用lt表示,即less than
> then
> echo ok
> fi
ok
[root@lijie-01 ~]#
Command line writing 2:
[root@lijie-01 ~]# a=5
[root@lijie-01 ~]# if [ $a -gt 3 ]; then echo ok; fi
ok
[root@lijie-01 ~]#
Script writing method 3 (this writing method is commonly used in the future):
[root@lijie-01 ~]# cat ifi.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
[root@lijie-01 ~]# sh ifi.sh
ok
- Format 2: if condition; then statement; else statement; fi
[root@lijie-01 ~]# cat ifi2.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
[root@lijie-01 ~]# sh ifi2.sh
nook
- Format 3: if …; then … ;elif …; then …; else …; fi
We write the following script
[root@lijie-01 ~]# cat ifi2.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
[root@lijie-01 ~]#
View the execution process
- Logical judgment expressions: if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ] etc. -gt (>); -lt(<); -ge(>= ); -le(<=);-eq(==); -ne(!=) Notice that there are spaces everywhere
gt means greater than
lt means less than less than
eq means equal to
ne means not equal to no equal
Above we use The characters such as gt lt eq represent greater than, less than, equal to; in fact, we can also use > < = >= to represent, but we need to use two layers of parentheses, as follows:
[root@lijie-01 ~]# a=5
[root@lijie-01 ~]# if (($a>1));then echo ok; fi
ok
[root@lijie-01 ~]#
- Multiple conditions can be combined using && ||
- if [ $a -gt 5 ] && [ $a -lt 10 ]; then
- if [ $b -gt 5 ] || [ $b -lt 3 ]; then