Day 3: Select Conditions
Outline
Application Instance Analysis
Conditional Test
If... Else... Fi
Case... In... Esac
Implementation menu:
After the script is executed
Press 1 to display the current time
Press 2 to display the CPU load
Press 3 to display the remaining memory
Press 0 to exit the script.
Press other characters to exit after the selected range is exceeded.
Analysis Steps.
# Date + % T
Uptime awk Interception
Free-m
Conditional test format
# Test-option obj
# [-Option obj]
Returned results
- The expression content test result is true.
- The expression content test result is false.
Test object category
Execution result (successful or failed)
File (whether the file exists)
Text (consistency)
Number (numerical comparison)
Option for conditional test
Option |
Function |
-D |
Directory |
-E |
Exist? |
-F |
Whether it is a common file |
-S |
Whether the file size is equal to 0 |
-R |
Readable or not |
-W |
Writable or not |
-X |
Executable or not |
Logical operation symbol
Option |
Function |
- |
And operations |
-O |
Or operation |
! |
Non-operation |
Instance:
# Test-e/etc/passwd-a-e/etc/shadow is in the middle of a and operation, then 0 is required
# If test-e/etc/passwd-o-e/etc/groups is an o or operation in the middle, the true result is 0.
String Operators
= Two strings are equal
! = Two strings are not equal
-Z empty string
-N non-null string
Instance:
# Test-z $ LOGNAME
# Echo $ LOGNAME
# Echo $?
Numeric comparison operator
Symbol |
Description |
-Eq |
Equal |
-Ne |
Not equal |
-Gt |
Greater |
-Lt |
Less |
-Ge |
Greater than or equal |
-Le |
Less than or equal |
If... Else... Fi Condition Selection
The basic format of the if control structure:
If condition # It can be a command or a test statement.
Then # execute if the condition is true and the negative value is true.
Command 1 # Run Command 1
Else # execute if the condition is false anti-value false 1
Command 2 # Run Command 2
Fi # End of judgment
Instance (if... Else... Fi) 1
Inputtest. sh
#! /Bin/bash
# Input test
Echo-n "Enter your name :"
Read name
# Did the user just hit return
If ["$ {name}" = ""]
Then
Echo "You did not enter any information"
Else
Echo "Your name: $ {name }"
Fi
Instance (if... Else... Fi) 2
Filecopy. sh
#! /Bin/bash
# File copy
If cp/etc/passwd. bak 2>/dev/null 2>/dev/null discard error message
Then
Echo "Good Copy !"
Else
Echo "'basename $ 0': error, cocould not copy"
Fi
If... Else... Nested fi (two-layer nesting)
If condition 1; then
If condition 2; then
Command 1
Else
Command 2
Else
If Condition 3; then
Command 3
Else
Command 4
Fi
Case... In... Esac Condition Selection (more flexible)
Case statements are mostly used for judgment of multiple branches.
Case format: (multiple modes match only the same pattern as variable)
Case variable in
Mode 1) command 1 ...;;
Mode 2) command 2 ...;;
Esac
Matching Mode
* Match any character
? Match any single character
[] Matching character range
Case... In. esac instance 1
#! /Bin/bash
# Case select
Echo-n "enter a number from 1 to 5 :"
Read NUM
Case $ NUM in
1) echo "you select 1 ″;;
2) echo "you select 2 ″;;
3) echo "you select 3 ″;;
4) echo "you select 4 ″;;
5) echo "you select 5 ″;;
*) Echo "basename $ This is not between 1 and 5 ″
Esac
Case... In. esac instance 2
The question is: the student's score is 0-. If the score is above 85, you must be prompted that you are the best !, Show you get a good mark in 70-84! , Show come on at 60-74 !, You must study hard is displayed below 60 points!
#! /Bin/bash
Echo-n "please input your mark :"
Read mark
Case $ mark in
100 | 9 [0-9] | 8 [5-9]) echo "you are the best !";; 100, 90-99, 85-89
8 [0-4] | 7 [0-9]) echo "you get a good mark !";; 80-84, 70-79
7 [0-4] | 6 [0-9]) echo "come on !";; 70-74, 60-69
[0-5] [0-9]) echo "You must study hard !";; 00-59
Esac
Solve today's problems
Use if... Else... Fi is used to determine input variables.
Execute the corresponding statement on each judgment branch.
Menu. sh
#! /Bin/bash
Clear
Echo "------- menu ------"
Echo "1) Show Time"
Echo "2) CPU load"
Echo "3) Memory free"
Echo "0) Exit"
Echo "---------------"
Echo-n "Enter you chose [0-3]:"
Read NUM
If [$ {NUM}-lt 0-o $ {NUM}-gt 3]
Then
Echo "This is not between 0-3 ."
Else
If ["$ {NUM}" = "1"]
Then
Echo "'date + % t '"
Else
If ["$ {NUM}" = "2"]
Then
Echo "'uptime | awk-F' [,:]'' {print $7 }''"
Else
If ["$ {NUM}" = "3"]
Then
Echo "'free-m | awk' $1 =" Mem: "{print $4 }''"
Else
Exit
Fi
Fi
Fi
Fi
Review this lesson:
Type of the condition test
File Test
Text Test
Numerical Test
Logic Test
If... Else... Fi Condition Selection Structure
Case... In... Esac
Test after class
1. Modify menu. sh to use a non-menu style. The parameter transmission mode is used for selection. For example, #./menu. sh 1 Output Time
2. Use case to select the menu
Sudu answer: (it is inevitable that there will be errors, but it can be implemented successfully)
1. After menu. sh is modified
#! /Bin/bash
If [$1-lt 0-o $1-gt 3]
Then
Echo "This is not between 0-3 ."
Else
If ["$1" = "1"]
Then
Echo "'date + % t '"
Else
If ["$1" = "2"]
Then
Echo "'uptime | awk-F' [,:]'' {print $7 }''"
Else
If ["$1" = "3"]
Then
Echo "'free-m | awk' $1 =" Mem: "{print $4 }''"
Else
Exit
Fi
Fi
Fi
Fi
2 ,#! /Bin/bash
Clear
Echo "------- menu ------"
Echo "1) Show Time"
Echo "2) CPU load"
Echo "3) Memory free"
Echo "0) Exit"
Echo "---------------"
Echo-n "Enter you chose [0-3]:"
Read NUM
Case $ NUM in
1) date + % T ;;
2) uptime | awk-F' [,:] ''{print $7 }';;
3) free-m | awk '$1 = "Mem:" {print $4 }';;
0) exit ;;
*) Echo "This is not between 0-3 .";;
Esac
There are still many gains today. The course lasted for nearly three hours.
Although if... Else... Fi is easier to understand, but it is much easier to use case.
It seems that it is enough to read a tutorial every day. It will also be dizzy if you look too much. Haha. Continue Learning ~