Linux shell Script programming Case Condition statement

Source: Internet
Author: User

1, determine whether a number is in the one-way.

#!/bin/Bashread-P"pls input a number:"N Case "$n" inch    1)        Echo "variable is 1"        ;; 2)        Echo "variable is 2"        ;; 3)        Echo "variable is 3"        ;; *)        Echo "pls input a number between 1 and 3"exit;Esac

2, multi-level if statement rewriting

#!/bin/Bashread-P"pls input a number:"Nif[$n-eq1]; Then    Echo "$n is a variable 1"elif[$n-eq2]; Then    Echo "$n is a variable 2"elif[$n-eq3]; Then    Echo "$n is a variable 3"Else    Echo "pls input a number between 1 and 3"fi

3,if. else nesting, implementing

#!/bin/Bashread-P"pls input a number:"Nif[$n-eq1]; Then    Echo 1Else    if[$n-eq2]; Then        Echo 2    elif[$n-eq3]; Then        Echo 3    Else        Echo "pls input a number [1-3]"    fifi

4, judging the score level

#!/bin/Bashread-P"pls input score to test level:"scoreif[$score-ge -]; Then    Echo "Excellent"elif[$score-ge the]; Then    Echo "Good"elif[$score-ge -]; Then    Echo "Medium"elif[$score-ge -]; Then    Echo "Pass"Else    Echo "inferior lattice"fi

5, add color to text

#!/bin/Bashred_color='\e[1;31m'Green_color='\e[1;32m'Yellow_color='\e[1;33m'Blue_color='\e[1;34m'Reset_color='\e[0m'Echo '    1, Goku2, eight commandments3, Tang's monk4, White Dragon Horse'Read-p"pls input a number:"N Case$ninch    1)        Echo-E"${red_color} Goku ${reset_color}"        ;; 2)        Echo-E"${green_color} Eight commandments ${reset_color}"        ;; 3)        Echo-E"${yellow_color} tang priest ${reset_color}"        ;; 4)        Echo-E"${blue_color} white Dragon horse ${reset_color}"        ;; *)        Echo "You need input a number in {1|2|3|4}"Esac

Another way to do this:

#!/bin/Bashred_color='\e[1;31m'Green_color='\e[1;32m'Yellow_color='\e[1;33m'Blue_color='\e[1;34m'Reset_color='\e[0m'functionmenu () {Cat<<END1, Goku2, eight commandments3, Tang's monk4, White Dragon Horse End}functionSelect_type () {Read-P"pls input a number:"N Case$ninch            1)                Echo-E"${red_color} Goku ${reset_color}"                ;; 2)                Echo-E"${green_color} Eight commandments ${reset_color}"                ;; 3)                Echo-E"${yellow_color} tang priest ${reset_color}"                ;; 4)                Echo-E"${blue_color} white Dragon horse ${reset_color}"                ;; *)                Echo "You need input a number in {1|2|3|4}"        Esac}functionMain () {menu Select_type}main

Read command line arguments, set color for content

#!/bin/Bashred_color='\e[1;31m'Green_color='\e[1;32m'Yellow_color='\e[1;33m'Blue_color='\e[1;34m'Reset_color='\e[0m'if[$#-ne2]; Then    Echo "Usage $ input {red|green|yellow|blue}"Exitfi Case$2 inchred)Echo-E"${red_color}$1${reset_color}"        ;; Green)Echo-E"${green_color}$1${reset_color}"        ;; Yellow)Echo-E"${yellow_color}$1${reset_color}"        ;; Blue)Echo-E"${blue_color}$1${reset_color}"        ;; *)            Echo "usage $ input {red|green|yellow|blue}"ExitEsac

Modify into function call mode

#!/bin/BashfunctionTocolor () {Red_color='\e[1;31m'Green_color='\e[1;32m'Yellow_color='\e[1;33m'Blue_color='\e[1;34m'Reset_color='\e[0m'        if[$#-ne2]; Then            Echo "Usage $ input {red|green|yellow|blue}"Exitfi         Case$2 inchred)Echo-E"${red_color}$1${reset_color}"                ;; Green)Echo-E"${green_color}$1${reset_color}"                ;; Yellow)Echo-E"${yellow_color}$1${reset_color}"                ;; Blue)Echo-E"${blue_color}$1${reset_color}"                ;; *)                    Echo "usage $ input {red|green|yellow|blue}"ExitEsac}functionMain () {Tocolor $1$2}main $*

Linux shell Script programming Case Condition statement

Related Article

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.