Select usage in the shell

Source: Internet
Author: User

Select is also a kind of loop, it is more suitable for use in the case of user choice.
For example, we have one such requirement, after running the script, let the user go to select the number, select 1, run the W command, select the 2 run Top command, select 3 run the free command, select 4 to exit. The script implements this:

    1. #!/bin/bash

    2. echo "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



The results of the implementation are as follows:
SH select.sh
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, 2:42, 1 user, load average:0.01, 0.08, 0.08
USER TTY from [email protected] IDLE jcpu PCPU
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 found that select defaults to the command line for the sequence number, each time a number is entered, the corresponding command executes, and the command does not exit the script. It will continue to let us lose again as the serial number. The prompt before the sequence number, we can also modify, using the variable PS3 can, again modify the script as follows:
    1. #!/bin/bash

    2. ps3= "Please select a number:"

    3. echo "chose a number, 1:run W, 2:run top, 3:run free, 4:quit"

    4. Echo


    5. Select command in w top free quit

    6. Do

    7. Case $command in

    8. W

    9. W

    10. ;;

    11. Top

    12. Top

    13. ;;

    14. Free

    15. Free

    16. ;;

    17. Quit

    18. Exit

    19. ;;

    20. *)

    21. echo "Please input a number: (1-4)."

    22. Esac

    23. Done



If you want the script to exit automatically each time you enter a sequence number, you will need to change the script again as follows:
    1. #!/bin/bash

    2. ps3= "Please select a number:"

    3. echo "chose a number, 1:run W, 2:run top, 3:run free, 4:quit"

    4. Echo


    5. Select command in w top free quit

    6. Do

    7. Case $command in

    8. W

    9. W;exit

    10. ;;

    11. Top

    12. Top;exit

    13. ;;

    14. Free

    15. Free;exit

    16. ;;

    17. Quit

    18. Exit

    19. ;;

    20. *)

    21. echo "Please input a number: (1-4)."; Exit

    22. Esac

    23. Done


Select usage in the shell

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.