Summary of Shell script syntax in RHEL

Source: Internet
Author: User
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
**************************************** **************************/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.