Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 8888
Next
In Process

askthedev.com Latest Questions

Asked: September 25, 20242024-09-25T21:25:10+05:30 2024-09-25T21:25:10+05:30In: Ubuntu

How can I implement a selection menu in a shell script on Ubuntu?

anonymous user

I’m diving into a little project on my Ubuntu machine, and I’ve hit a bit of a snag with my shell script. I’m trying to create a simple selection menu that lets users choose from a list of options when they run the script. You know, something that’s interactive and user-friendly, but I’m not sure how to implement it properly!

I want the menu to display a few options, and then based on the user’s input, it should perform a specific action. For example, I’m thinking of giving the options like “1. Check Disk Usage”, “2. List Installed Packages”, and “3. Exit”. When they select an option by pressing a corresponding number and hit enter, the script should then execute the relevant command. Sounds straightforward, right?

The thing is, my scripting skills are a bit rusty, and I’m worried my menu might end up being clunky or non-functional. I’ve read somewhere that using a case statement could be a good way to handle the user input, but I’m not entirely sure how to structure everything. I can imagine it could look something like:

“`bash
echo “Choose an option:”
echo “1. Check Disk Usage”
echo “2. List Installed Packages”
echo “3. Exit”
read -p “Enter your choice: ” choice

case $choice in
1)
# Command to check disk usage
;;
2)
# Command to list installed packages
;;
3)
echo “Goodbye!”
exit 0
;;
*)
echo “Invalid option, please try again.”
;;
esac
“`

But I have a feeling there’s more I could do to make it more polished. Also, how do I handle situations where the user inputs something unexpected?

I guess I’m really looking for any tips or examples from anyone who has tackled something similar. Have you implemented a menu like this before? What are the best practices for making it clean and user-friendly? Any code snippets or suggestions would be super helpful! Thanks in advance!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2024-09-25T21:25:12+05:30Added an answer on September 25, 2024 at 9:25 pm



      Interactive Shell Script Menu

      Creating an interactive selection menu in a shell script can greatly enhance user experience. The `echo` commands you already have will display options effectively, and using the `case` statement is a clean way to handle the user input. To structure your script, you can refine the commands within each case to make it more functional. For example, to check disk usage, you can use the `df -h` command, and to list installed packages, you might use `dpkg –get-selections`. Additionally, to improve user experience, consider wrapping your script in a loop that repeatedly displays the menu until the user chooses to exit. This allows users to perform multiple actions without restarting the script each time.

      To handle unexpected inputs gracefully, you already have the default case that prompts the user if they provide an invalid option. Consider adding a validation loop which continues to ask for input until a valid response is given. You might also want to incorporate a helpful message that explains the valid options before asking for input. Below is a refactored version of your script that includes these improvements:

          #!/bin/bash
      
          while true; do
              echo "Choose an option:"
              echo "1. Check Disk Usage"
              echo "2. List Installed Packages"
              echo "3. Exit"
              read -p "Enter your choice: " choice
      
              case $choice in
                  1)
                      echo "Checking disk usage..."
                      df -h
                      ;;
                  2)
                      echo "Listing installed packages..."
                      dpkg --get-selections
                      ;;
                  3)
                      echo "Goodbye!"
                      exit 0
                      ;;
                  *)
                      echo "Invalid option, please try again."
                      ;;
              esac
          done
          


        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2024-09-25T21:25:11+05:30Added an answer on September 25, 2024 at 9:25 pm


      Creating an Interactive Menu in a Shell Script

      It sounds like you’re on the right track with your shell script, and using a case statement is definitely a great way to handle user input in a clear way! Here’s how you could structure your script to make it interactive and user-friendly:

      
      #!/bin/bash
      
      while true; do
          echo "Choose an option:"
          echo "1. Check Disk Usage"
          echo "2. List Installed Packages"
          echo "3. Exit"
          read -p "Enter your choice: " choice
      
          case $choice in
              1)
                  echo "Checking disk usage..."
                  df -h
                  ;;
              2)
                  echo "Listing installed packages..."
                  dpkg --get-selections
                  ;;
              3)
                  echo "Goodbye!"
                  exit 0
                  ;;
              *)
                  echo "Invalid option, please try again."
                  ;;
          esac
      done
      
          

      A few tips to polish it up:

      • Use a while true loop so that the menu keeps showing until the user exits. This is handy if they want to run another command after one has finished.
      • Make sure to include some feedback after each action, like showing the disk usage or the installed packages.
      • Handling unexpected input is key! The * case you already have will manage that by asking the user to try again.

      Running commands like df -h for disk usage and dpkg --get-selections for installed packages will give users useful information while keeping everything inside your menu. The script can always be expanded with more options later, making it flexible!

      Give this a go, and you should have a nice, user-friendly menu up and running in no time!


        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Related Questions

    • I'm having trouble installing the NVIDIA Quadro M2000M driver on Ubuntu 24.04.1 LTS with the current kernel. Can anyone provide guidance or solutions to this issue?
    • What steps can I take to troubleshoot high usage of GNOME Shell in Ubuntu 24.04?
    • I recently performed a fresh installation of Ubuntu 24.04, and I've noticed that my RAM usage steadily increases over time until my system becomes unresponsive. Has anyone experienced this issue ...
    • How can I resolve the "unknown filesystem" error that leads me to the GRUB rescue prompt on my Ubuntu system?
    • I'm experiencing an issue with Ubuntu 24.04 where Nautilus fails to display the progress indicator when I'm copying large files or folders. Has anyone else encountered this problem, and what ...

    Sidebar

    Related Questions

    • I'm having trouble installing the NVIDIA Quadro M2000M driver on Ubuntu 24.04.1 LTS with the current kernel. Can anyone provide guidance or solutions to this ...

    • What steps can I take to troubleshoot high usage of GNOME Shell in Ubuntu 24.04?

    • I recently performed a fresh installation of Ubuntu 24.04, and I've noticed that my RAM usage steadily increases over time until my system becomes unresponsive. ...

    • How can I resolve the "unknown filesystem" error that leads me to the GRUB rescue prompt on my Ubuntu system?

    • I'm experiencing an issue with Ubuntu 24.04 where Nautilus fails to display the progress indicator when I'm copying large files or folders. Has anyone else ...

    • How can I configure a server running Ubuntu to bind specific IP addresses to two different network interfaces? I'm looking for guidance on how to ...

    • Is it possible to configure automatic login on Ubuntu MATE 24.04?

    • After upgrading from Ubuntu Studio 22.04 to 24.04.1, I lost all audio functionality. What steps can I take to diagnose and resolve this issue?

    • I am experiencing issues booting Ubuntu 22.04 LTS from a live USB. Despite following the usual procedures, the system fails to start. What steps can ...

    • I'm encountering a problem with my Expandrive key while trying to update my Ubuntu system. Has anyone else faced similar issues, and if so, what ...

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.