shell in the select usage

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:

  1. #!/bin/bash
  2. echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
  3. echo
  4. select command in w top free quit
  5. do
  6.     case $command in
  7.     w)
  8.         w
  9.         ;;
  10.     top)
  11.         top
  12.         ;;
  13.     free)
  14.         free
  15.         ;;
  16.     quit)
  17.         exit
  18.         ;;
  19.     *)
  20.         echo "Please input a number:(1-4)."
  21.         ;;
  22.     esac
  23. 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:

  1. #!/bin/bash
  2. PS3="Please select a number: "
  3. echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
  4. echo
  5.  
  6. select command in w top free quit
  7. do
  8.     case $command in
  9.     w)
  10.         w
  11.         ;;
  12.     top)
  13.         top
  14.         ;;
  15.     free)
  16.         free
  17.         ;;
  18.     quit)
  19.         exit
  20.         ;;
  21.     *)
  22.         echo "Please input a number:(1-4)."
  23.     esac
  24. 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:

  1. #!/bin/bash
  2. PS3="Please select a number: "
  3. echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
  4. echo
  5.  
  6. select command in w top free quit
  7. do
  8.     case $command in
  9.     w)
  10.         w;exit
  11.         ;;
  12.     top)
  13.         top;exit
  14.         ;;
  15.     free)
  16.         free;exit
  17.         ;;
  18.     quit)
  19.         exit
  20.         ;;
  21.     *)
  22.         echo "Please input a number:(1-4).";exit
  23.     esac
  24. 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.

 

Guess you like

Origin blog.csdn.net/weixin_40876986/article/details/92385029