User-Selectable Timezone in Bash Script #1

Open
opened 2024-10-14 21:18:56 -04:00 by nick · 0 comments
Owner

Summary:
Enhance the existing Bash script that sets the timezone based on a predefined variable by allowing users to select their preferred timezone from a list of options.

Motivation:
Currently, the script requires modification to change the timezone, which can be inconvenient for users who may not be familiar with editing scripts. By providing a user-friendly selection mechanism, we can improve usability and reduce the likelihood of errors.

Proposed Changes:

  1. Add a List of Timezone Options:

    • Create a menu displaying available timezones (e.g., UTC, PST, EST, CST, etc.).
    • Allow the user to choose their desired timezone from the list.
  2. Input Handling:

    • Capture user input and validate it to ensure a valid timezone is selected.
    • Provide feedback in case of invalid selection.
  3. Update Timezone Setting Logic:

    • Modify the script to use the selected timezone instead of a hard-coded variable.

Example Implementation:

#!/bin/bash

echo "Select your timezone:"
echo "1) UTC"
echo "2) PST"
echo "3) EST"
echo "4) CST"
read -p "Enter the number corresponding to your choice: " choice

case $choice in
    1) timezone="UTC";;
    2) timezone="America/Los_Angeles";;
    3) timezone="America/New_York";;
    4) timezone="America/Chicago";;
    *) echo "Invalid selection"; exit 1;;
esac

# Set the timezone
timedatectl set-timezone "$timezone"
echo "Timezone set to $timezone"

Benefits:

  • Simplifies the user experience by eliminating the need to edit the script directly.
  • Reduces errors associated with manual input of timezone strings.
  • Makes the script more versatile and accessible for a broader audience.

Additional Notes:
Consider integrating a complete list of timezones that can be fetched dynamically to ensure all users have access to the most current options.


Feel free to modify any parts to better fit your project's style or requirements!

**Summary:** Enhance the existing Bash script that sets the timezone based on a predefined variable by allowing users to select their preferred timezone from a list of options. **Motivation:** Currently, the script requires modification to change the timezone, which can be inconvenient for users who may not be familiar with editing scripts. By providing a user-friendly selection mechanism, we can improve usability and reduce the likelihood of errors. **Proposed Changes:** 1. **Add a List of Timezone Options:** - Create a menu displaying available timezones (e.g., UTC, PST, EST, CST, etc.). - Allow the user to choose their desired timezone from the list. 2. **Input Handling:** - Capture user input and validate it to ensure a valid timezone is selected. - Provide feedback in case of invalid selection. 3. **Update Timezone Setting Logic:** - Modify the script to use the selected timezone instead of a hard-coded variable. **Example Implementation:** ```bash #!/bin/bash echo "Select your timezone:" echo "1) UTC" echo "2) PST" echo "3) EST" echo "4) CST" read -p "Enter the number corresponding to your choice: " choice case $choice in 1) timezone="UTC";; 2) timezone="America/Los_Angeles";; 3) timezone="America/New_York";; 4) timezone="America/Chicago";; *) echo "Invalid selection"; exit 1;; esac # Set the timezone timedatectl set-timezone "$timezone" echo "Timezone set to $timezone" ``` **Benefits:** - Simplifies the user experience by eliminating the need to edit the script directly. - Reduces errors associated with manual input of timezone strings. - Makes the script more versatile and accessible for a broader audience. **Additional Notes:** Consider integrating a complete list of timezones that can be fetched dynamically to ensure all users have access to the most current options. --- Feel free to modify any parts to better fit your project's style or requirements!
nick added the
enhancement
label 2024-10-14 21:18:56 -04:00
nick self-assigned this 2024-10-14 21:18:56 -04:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: nick/RHELSecureSetup#1
No description provided.