select usage of the shell
share it
is a select cycle, it is more suitable for use in the case of user selection.
For example, we have such a demand, after running the script, allowing users to select the number, select 1, will run w command, select 2 to run the top command, select 3 run free command, select 4 Exit. Script to achieve this:
- #!/bin/bash
- echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
- echo
- select command in w top free quit
- do
- case $command in
- w)
- w
- ;;
- top)
- top
- ;;
- free)
- free
- ;;
- quit)
- exit
- ;;
- *)
- echo "Please input a number:(1-4)."
- ;;
- esac
- done
执行结果如下:
sh select.sh
Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit
1) w
2) top
3) free
4) quit
#? 1
16:03:40 up 32 days, 2:42, 1 user, load average: 0.01, 0.08, 0.08
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 61.135.172.68 15:33 0.00s 0.02s 0.00s sh select.sh
#? 3
total used free shared buffers cached
Mem: 1020328 943736 76592 0 86840 263624
-/+ buffers/cache: 593272 427056
Swap: 2097144 44196 2052948
#?
We have found, select the default list numbers corresponding to command each time you enter a number, the corresponding command will be executed, the command has completed and will not exit the script. It will also continue to let us lose, such as the serial number again. In front of the serial number prompt, we also can be modified, you can use variables PS3, once again modify the script as follows:
- #!/bin/bash
- PS3="Please select a number: "
- echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
- echo
- select command in w top free quit
- do
- case $command in
- w)
- w
- ;;
- top)
- top
- ;;
- free)
- free
- ;;
- quit)
- exit
- ;;
- *)
- echo "Please input a number:(1-4)."
- esac
- done
If you want to script each time you enter a serial number after the automatic exit, you need to change the script again as follows:
- #!/bin/bash
- PS3="Please select a number: "
- echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
- echo
- select command in w top free quit
- do
- case $command in
- w)
- w;exit
- ;;
- top)
- top;exit
- ;;
- free)
- free;exit
- ;;
- quit)
- exit
- ;;
- *)
- echo "Please input a number:(1-4).";exit
- esac
- done
supplement:
case ... esac and other similar language in the switch ... case statement, choose a multi-branched structure.
case statement matches a value or a pattern, if the match is successful, the implementation phase matching command. case statement format is as follows: case value in
mode. 1)
Command1
Command2
Command3
;;
Mode 2)
Command1
Command2
Command3
;;
*)
Command1
Command2
Command3
;;
Esac
shown above case work. The value must be behind the keyword in, each model must end with a closing parenthesis. Value can be variable or constant. After a match is found value in line with a pattern, during which all commands begin until ;;. ;; similar to other languages break, meaning that the entire case statement last jump.
Each value of the detected pattern matching. Once the pattern matching , after executing the matching pattern corresponding to discontinue other modes command. If none of the matching pattern , to capture the asterisk * value, then execute subsequent commands.