1. Conditional Select if statement
Select Execute:
Single Branch
If judgment condition: Then
Branch code with true condition
Fi
Dual Branch
if judgment condition; Then
Branch code with true condition
Else
The condition is a false branch code
Fi
Multi-Branch
if CONDITION1; Then
If-true
Elif CONDITION2; Then
If-ture
Elif CONDITION3; Then
If-ture
...
Else
All-false
Fi
Judge by condition, when the first encounter is a "true" condition, execute its branch, and then end the entire if statement
If instance
Execute commands According to the exit status of the command
If ping-c1-w2 station1 &>/dev/null; Then
Echo ' Station1 is up '
elif grep "Station1" ~/maintenance.txt &>/dev/null
Then
Echo ' Station1 is undergoing maintenance '
Else
Echo ' Station1 is unexpectedly down! '
Exit 1
2. Conditional Judgment: Case statement
Case variable reference in
PAT1)
Branch 1
;;
PAT2)
Branch 2
;;
...
*)
Default Branch
;;
Esac
3. Practice:
1, write a script/root/bin/createuser.sh, to achieve the following functions:
Using a user name as a parameter, if the user who specified the parameter exists, the
otherwise added; Displays information such as the ID number of the user being added
Read "Input your username:" Input_user
ID $input _user
If [$?-eq 0]; then
echo "User exist"
Else
Useradd $input _user
Chk_id= ' getent passwd $input _user | Cut-d:-F 3 '
Echo $chk _id
Fi
? 2, write a script/root/bin/yesorno.sh, prompting the user to enter Yes or
No, and determine whether the user has entered Yes or no, or other information
#!/bin/bash
#
Read-p "Please input yes or no:" Input_info
[-Z "$input _info"] && (echo "error"; exit) | | Uperr_input_info= ' echo ' $input _info | tr [A-z] [a-z] '
Case $uperr _input_info in
y| yes|no| N
echo "Right"
;;
*)
echo "Other Info"
;;
Esac
3, write a script/root/bin/filetype.sh, determine the user input file path
File type (normal, directory, link, other file type)
#!/bin/bash
#
Read-p "Please input the path:" Path_file
If [-z] $path _file "];then
echo "You need to input info"; exit
Else
Type_file= ' Ls-ld $path _file | Cut-c1 '
echo "The type of the file is: $type _file"
Fi
4, write a script/root/bin/checkint.sh, determine the user input parameters
is a positive integer
#!/bin/bash
#
Read "Please input int:" Int_put
if [["$int _put" =~ ' ^[1-9]+$ ']]; then
echo "It is a int"
elif ["$int _put"-le 0]; then
Ehco "It is fu int or zero"
elif ["$int _put"-eq 0];then
echo "It is zero"
Else
echo "It is not a int"
Fi
#也可以使用 expr A + 0 to determine the type of a
Shell--if, Case Grammar detailed