20.5 logical judgments in shell scripts
格式1:if 条件 ; then 语句; 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(!=) 注意到处都是空格可以使用 && || 结合多个条件if [ $a -gt 5 ] && [ $a -lt 10 ]; thenif [ $b -gt 5 ] || [ $b -lt 3 ]; then
Operation Process
[Email protected] shell]# vim if1.sh
#!/bin/bash
A=5
If [$a-GT 3]
Then
Echo OK
Fi
[[email protected] shell]# sh if1.sh ok
[Email protected] shell]# vim if2.sh
#!/bin/bash
A=5
If [$a-GT 3]
Then
Echo OK
Else
Echo Nook
Fi
[[email protected] shell]# sh if2.sh ok
[Email protected] shell]# vim if2-2.sh
#!/bin/bash
a=2
If [$a-GT 3]
Then
Echo OK
Else
Echo Nook
Fi
[[email protected] shell]# sh if2-2.sh nook
[Email protected] shell]# vim if3.sh
#!/bin/bash
A=5
If [$a-GT 6]
Then
echo ">1"
elif [$a-lt 6]
Then
echo "<6 && >1"
Else
Echo Nook
Fi
[[email protected] shell]# sh if3.sh <6 && >1
20.6 File Directory property judgment
[ -f file ]判断是否是普通文件,且存在[ -d file ] 判断是否是目录,且存在[ -e file ] 判断文件或目录是否存在[ -r file ] 判断文件是否可读[ -w file ] 判断文件是否可写[ -x file ] 判断文件是否可执行
Operation Process
[Email protected] shell]# vim file1.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-F $f]
Then
echo $f exist.
Else
Touch $f
Fi
[[email protected] shell]# sh -x file1.sh + f=/tmp/aminglinux+ ‘[‘ -f /tmp/aminglinux ‘]‘+ touch /tmp/aminglinux[[email protected] shell]# sh -x file1.sh + f=/tmp/aminglinux+ ‘[‘ -f /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux exist./tmp/aminglinux exist.
[Email protected] shell]# vim file2.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-D $f]
Then
echo $f exist.
Else
mkdir $f
Fi
[[email protected] shell]# sh -x file2.sh + f=/tmp/aminglinux+ ‘[‘ -d /tmp/aminglinux ‘]‘+ mkdir /tmp/aminglinux[[email protected] shell]# sh -x file2.sh + f=/tmp/aminglinux+ ‘[‘ -d /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux exist./tmp/aminglinux exist.
[Email protected] shell]# vim file3.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-e $f]
Then
echo $f exist.
Else
mkdir $f
Fi
[[email protected] shell]# sh -x file3.sh + f=/tmp/aminglinux+ ‘[‘ -e /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux exist./tmp/aminglinux exist.
[Email protected] shell]# vim file4.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-R $f]
Then
echo $f readable.
Fi
[[email protected] shell]# sh -x file4.sh + f=/tmp/aminglinux+ ‘[‘ -r /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux readable./tmp/aminglinux readable.
[Email protected] shell]# vim file5.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [w $f]
Then
Echo $f writeable.
Fi
[[email protected] shell]# sh -x file5.sh + f=/tmp/aminglinux+ ‘[‘ -w /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux writeable./tmp/aminglinux writeable.
[Email protected] shell]# vim file6.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-X $f]
Then
Echo $f exeable.
Fi
[[email protected] shell]# sh -x file6.sh + f=/tmp/aminglinux+ ‘[‘ -x /tmp/aminglinux ‘]‘+ echo /tmp/aminglinux exeable./tmp/aminglinux exeable.
20.7 If special usage
if [ -z "$a" ]??这个表示当变量a的值为空时会怎么样if [ -n "$a" ] 表示当变量a的值不为空if grep -q ‘123‘ 1.txt; then??表示如果1.txt中含有‘123‘的行时会怎么样if [ ! -e file ]; then 表示文件不存在时会怎么样if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
Operation Process
[Email protected] shell]# vim file4.sh
#!/bin/bash
if [!-f/tmp/lalala]
Then
echo "/tmp/lalala not exist."
Exit
Fi
n= ' Wc-l/tmp/lalala '
If [-Z "$n"]
Then
echo Error
Exit
elif [$n-GT 100]
Then
Echo Aljalala
Fi
[[email protected] shell]# sh -x file4.sh + ‘[‘ ‘!‘ -f /tmp/lalala ‘]‘+ echo ‘/tmp/lalala not exist.‘/tmp/lalala not exist.+ exit[[email protected] shell]# if [ -n if1.sh ]; then echo ok; fiok[[email protected] shell]# if [ -n "$b" ]; then ehco $b; else echo "b is null"; fib is null[[email protected] shell]# if grep -wq ‘user1‘ /etc/passwd; then echo "user1 exist";fiuser1 exist
20.8-9 Case Judgment
Format case?? Variable name in?
value1)
??? Command
???;;
value2)
??? Command
??;;
*)
? commond
;;
Esac
In a case program, you can use a |, meaning, or means in a condition, such as
2|3)
Command
;;
Shell Script case
#!/bin/bash
Read-p "Please input a number:" N
If [-Z "$n"]
Then
echo "Please input a number."
Exit 1
Fi
n1=echo $n|sed ‘s/[0-9]//g‘
If [-N "$n 1"]
Then
echo "Please input a number."
Exit 1
Fi
If [$n-lt] && [$n-ge 0]
Then
Tag=1
elif [$n-ge] && [$n-LT 80]
Then
tag=2
elif [$n-ge] && [$n-LT 90]
Then
Tag=3
elif [$n-ge] && [$n-le 100]
Then
Tag=4
Else
Tag=0
Fi
Case $tag in
1)
echo "Not OK"
;;
2)
echo "OK"
;;
3)
echo "Ook"
;;
4)
echo "Oook"
;;
*)
echo "The number range is 0-100."
;;
Esac
Operation Process
[Email protected] shell]# vim case.sh
Enter the sample above
[[email protected] shell]# sh case.sh Please input a number: 50not ok[[email protected] shell]# sh case.sh Please input a number: 120The number range is 0-100.
2018-05-30 Linux Learning