LogNotes

2025-09-14 10:37:40
$ cat menu.sh3 # apt install dialog
#!/bin/bash

# Check if dialog is installed
if ! command -v dialog &> /dev/null; then
    echo "This script requires 'dialog' to be installed. Please install it using your package manager."
    exit 1
fi

# File to store custom commands
COMMANDS_FILE="$HOME/.menu_commands"

# Initialize default commands if file doesn't exist
if [ ! -f "$COMMANDS_FILE" ]; then
    echo -e "date\nip a\nip r" > "$COMMANDS_FILE"
fi

# Load commands from file
mapfile -t commands < "$COMMANDS_FILE"

# Temporary file to store dialog output and command output
tempfile=$(mktemp)
cmd_output=$(mktemp)

# Cleanup on exit
trap 'rm -f $tempfile $cmd_output' EXIT

# Function to display and handle the menu
show_menu() {
    while true; do
        # Build menu options dynamically
        menu_options=()
        for i in "${!commands[@]}"; do
            menu_options+=("$((i+1))" "${commands[i]}")
        done
#        menu_options+=("$(( ${#commands[@]} + 1 ))" "Add new command")
        menu_options+=("$(( ${#commands[@]} + 2 ))" "Quit")

        # Create the dialog menu
        dialog --clear --backtitle "System Commands Menu" \
               --title "Select a Command" \
               --menu "Use arrow keys to navigate, Enter to select:" 15 50 7 \
               "${menu_options[@]}" \
               2> "$tempfile"

        # Get the user's choice
        choice=$(cat "$tempfile")

        # Handle the selection
        if [ "$choice" -eq "$(( ${#commands[@]} + 2 ))" ]; then
            clear
            exit 0
        elif [ "$choice" -eq "$(( ${#commands[@]} + 1 ))" ]; then
            # Add new command
            dialog --title "Add New Command" --inputbox "Enter the command to add:" 8 40 2> "$tempfile"
            new_command=$(cat "$tempfile")
            if [ -n "$new_command" ]; then
                # Validate command existence (check the first word of the command)
                if command -v "${new_command%% *}" &> /dev/null; then
                    echo "$new_command" >> "$COMMANDS_FILE"
                    commands+=("$new_command")
                    dialog --title "Success" --msgbox "Command '$new_command' added successfully!" 6 40
                else
                    dialog --title "Error" --msgbox "Command '$new_command' not found!" 6 40
                fi
            fi
        elif [ "$choice" -ge 1 ] && [ "$choice" -le "${#commands[@]}" ]; then
            # Execute selected command
            selected_command="${commands[$((choice-1))]}"
            # Run the command and capture output
            if $selected_command > "$cmd_output" 2>&1; then
                if [ "$selected_command" = "date" ]; then
                    dialog --title "$selected_command Output" --msgbox "$(cat "$cmd_output")" 10 50
                else
                    dialog --title "$selected_command Output" --textbox "$cmd_output" 20 70
                fi
            else
                dialog --title "Error" --msgbox "Failed to execute '$selected_command'. Check the command syntax." 6 50
            fi
        fi
    done
}

# Run the menu
show_menu
← Previous Next →
Back to list