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