Shell programming file test, conditional statement and circular statement

Condition test

test command

Test whether the expression is true. If it is true, return 0; otherwise, return other values
Format 1: test conditional expression
Format 2: [conditional expression]

File test

[operator file or directory]
Common test operators

  • -d: Test whether it is a directory
  • -e: Test whether the directory or file exists (Exist)
    • f: Test whether it is a file
  • -r: Test whether the current user has permission to read. (Read)
  • -w: Test whether the current user has permission to write
  • -x: Test whether the current user has permission to execute (eXcute)
[root@localhost ~]#  [ -d /media/cdrom ]
[root@localhost ~]# echo $?
1						#A return value of 1 indicates that the condition is not valid
[root@localhost ~]#  [ -d /root ]
[root@localhost ~]# echo $?
0						#A return value of 0 indicates that the condition is true
[root@localhost ~]#  [ -d /root ] && echo "yes"		#Logic and the meaning of "and"
yes

Integer value comparison

[integer 1 operator integer 2]
Common test operators:

  • -eq: equal
  • -ne: not equal
  • -gt: greater than
  • -lt: less than (Lesser Than)
  • -le: less than or equal
  • -ge: greater or equal
[root@localhost ~]# who | wc -l
7
[root@localhost ~]# [ $(who | wc -l) -gt 5 ] && echo "Too many."	#Is the number of users > 5
Too many.
[root@localhost ~]# [$(who [ wc -l) -ge 10 ] && echo "> = 10."	#Is the number of users > = 10
[root@localhost ~]# FreeCC=$(free -m | grep "Mem: " | agk "{fprint $6}")
[root@localhost ~]# [$FreeCC -lt 1024 ] && echo ${FreeCC}MB	#Is the free memory < 1024MB
275MB

String test

1. String comparison
Format 1
[string 1 = string 2]
[string 1! = string 2]
Format 2
[- z string]
2. Common test operators

  • =: same string content
  • !=: Different string contents,! The sign means the opposite
  • -z: The string content is empty
[root@localhost ~]# echo $LANG
zh_CN.UTF-8
[root@localhost ~]# [ $LANG != "en.US"]&& echo "Not en.Us
"Not en.Uus
[root@localhost ~]# read -p "overwrite existing file (yesino)?" ACK
 Overwrite existing file( yes/no)? yes
[root@localhost~]# [ $ACK = "yes" ] && echo"Overwrite	#Test whether the read string is yes
 cover

Logic test

Format 1: [expression1] operator [expression2]
Format 2: Command 1 operator command 2
Common test operators

  • -a or & &: logic and meaning of "and"
  • -o or |: logical or meaning of "or"
    !: Logical no
[root@localhost~]# [ -d /etc ] && [ -r /etc ] && echo "You can op
[root@localhost ~]# [ -d letc ] ll [ -d /home ] && echo "ok"

Structure of if statement

Single branch structure

if  Conditional test operation
	then  Command sequence
fi

Double branch structure

if Conditional test operation 
	then  Command sequence 1
	else  Command sequence 2
fi

Multi branch structure

if  Condition test operation 1
	  then  Command sequence 1
elif  Condition test operation 2
	  then Command sequence 2
else
 Command sequence 3
fi

case multi branch statement

[root@localhost-]# cat hitkey.sh
#!/bin/bash
read -p "Please enter a character,And press Enter Key confirmation:" KEY
case "$KEY" in
	[a-z][[A-Z])
		echo "You entered a letter." 
		;;
	[0-9])
		echo "You entered a number."
		;;
	*)
		echo "You have entered spaces, function keys, or other control characters."
esac

Structure of for statement

Read different variable values to execute the same set of commands one by one

for Variable name in Value list
do
	Command sequence
done

Structure of while statement

Test a condition repeatedly, and execute it repeatedly as long as the condition is true

while  Conditional test operation
do
	Command sequence
done

The for loop statement is very suitable for situations where the list object is irregular and the source of the list has been fixed (such as a list file). However, when it is required to control the number of cycles, number the operation objects in numerical order, and perform repeated operations according to specific conditions, it is more suitable to use another cycle - while statement.

Structure of until statement

Test a condition repeatedly, and execute it repeatedly as long as the condition is not tenable

until  Conditional test operation
do
	Command sequence
done

Hard disk excess alarm

yum -y install mailx
vi /etc/mail.rc
set from=e-mail address			#For example: xxxxxx@qq.com
set smtp=smtp.Mailbox suffix		#For example: STMP qq. com
set smtp-auth-user=e-mail address	
set smtp-auth-password=Email authorization code
set smtp-auth=login
hard_margin=$( df -Th | grep "/$" | awk -F ' [%]+' ' {print $6}')
[root@server1~]# echo $hard_margin
12
[ $hard_margin -gt 10 ] && $(echo""Excess hard disk usage" │ mail -s "Email address of "hard disk alarm")

Tags: shell

Posted by pklover on Thu, 05 May 2022 22:11:00 +0300