Introduction to the case command:
This command is used for conditional judgment, select structure.
1 . Command format:
Case switch in-----switch variable value
value1)
Statement-----Statement Statement
...
;;
value2)
Statement
...
;;
*)
Statement
....
;;
Esac
2 . Command function:
By selecting the structure, the results of the final output are compared for all possible conditional statements.
3 . Command parameters:
/
4, the command instance:
1, through the keyboard input characters, if the capital letter is a capital letter, lowercase is a lowercase letter.
#!/bin/bash
#
Case $ in
[0-9])
echo "It is digit"
;;
[A-z])
echo "It is lower"
;;
[A-z])
echo "It is Upper"
;;
*)
echo "It is Unknown"
;;
Esac
2, only accept the parameter start, Stop,restart,shutdown.
#!/bin/bash
#
#program
Case $ in
' Start ')
echo "Start server ...";;
' Restart ')
echo "Restart server ...";;
' Stop ')
echo "Stop server ...";;
' Status ')
echo "Running ...";;
*)
echo "' basename $ ' {start|stop|restart|status}";;
Esac
3, write a script, you can accept the option parameters, and then get each option, and the parameters of the options,
And can be based on options and parameters to make a specific operation.
#!/bin/bash
#
Declare-i shownum=0
Declare-i showusers=0
For I in ' seq 1 $# '; Do
If [$#-gt 0]; Then
Case $ in
-H|--HELP)
echo "Usage: ' basename$0 '-h|--help-c|--count-v|--verbose"
Exit 0;;
-v|--verbose)
Let Showusers=1
shift;
-c|--count)
Let Shownum=1
shift;
*)
echo "Usage: ' basename$0 '-h|--help-c|--count-v|--verbose"
Exit 8;;
Esac
Fi
Done
If [$SHOWNUM-eq 1]; Then
echo "Logged users: ' Who |wc-l '."
If [$SHOWUSERS-eq 1]; Then
echo "They is:"
W.H.O.
Fi
Fi
This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1866118
Linux command: Case selection Structure statement