Syntax Summary of shell scripts in rhel: if condition judgment control statement: if condition then action elif condition & n
Summary of shell script syntax in rhel
If
Conditional judgment control statement:
If condition
Then action
Elif Condition
Then action
Else
Action
Fi
Note:
• If, when the condition is true, the action after then is executed. Elif is judged to be false, and else is executed when both if and elif are false.
• Example:
#! /Bin/sh
Read-p "Enter a password" pwd_entered
If ["$ pwd_entered" = "password"]
Then
Echo Password is correct
Else
Echo Password is incorrect
Fi
Case
Conditional judgment control statement:
Case expression in
Matching value 1)
Action ;;
Matching value 2)
Action ;;
*)
Action ;;
Esac
• Case: searches for matching items to execute the action based on the expression value (the expression usually contains variables. It can be used to replace a group of if Statements.
• Example:
#! /Bin/sh
Read-p "Enter a password" pwd_entered
Case $ pwd_entered in
Password1)
Echo You entered password1 ;;
Password2)
Echo You entered password2 ;;
Esac
Select
Loop Control statement
Select variable in array
Do
Action
Done
Note:
• Select, infinite loop, exit the loop until the exit command (exit, break) or ctrl + c sent by the keyboard is encountered. Each loop requires you to select a value for the variable from the array.
For
Loop Control statement
For variable in array
Do
Action
Done
Note:
• For, variables automatically go to the values in the array and execute a loop every time the values are obtained.
• Example:
#! /Bin/sh
Read-p "Enter several words" words_entered
Clear
Echo The words you entered are:
For word in $ word_entered
Do
Echo $ word
Done
While
Loop Control statement
While Condition
Do
Action
Done
Note:
• While, when the condition is true, execute the loop under it.
• Example:
#! /Bin/sh
Read-p "Enter a password" pwd_entered
Clear
While ["$ pwd_entered"! = "Opensesame"]
Do
Echo Sorry, try again
Read-p "Enter a password" pwd_entered
Done
Echo Correct password entered
Until
Loop Control statement
Until Condition
Do
Action
Done
Note:
Until. When the condition is true, the subsequent loop ends. Otherwise, keep repeating.
/*************************************** ***************************
Author: edwin)
Date: 2010.7.27
Note: Reprinted please keep this information
For more information, visit my blog: blog.csdn.net/windxfxx8
**************************************** **************************/