LogNotes

2025-09-14 10:36:27
$ cat menu.sh0 
#!/bin/bash

# Array of menu items and their corresponding commands
menu_items=("Show Date" "Show IP Address" "Show Routing Table" "Show Block Devices" "Test" "Exit")
commands=("date" "ip a" "ip r" "lsblk" "echo Test" "exit 0")

# Current selected item
current=0

# Function to display the menu
display_menu() {
  clear
  echo "=== System Information Menu ==="
  for i in "${!menu_items[@]}"; do
    if [ $i -eq $current ]; then
      echo "> ${menu_items[$i]}"
    else
      echo "  ${menu_items[$i]}"
    fi
  done
  echo "=============================="
  echo "Use arrow keys to navigate, Enter to select, Ctrl+C to quit"
}

# Function to handle key input
read_arrow() {
  read -s -n 1 key
  if [[ $key == $'\x1b' ]]; then
    read -s -n 2 key
    case $key in
      '[A') # Up arrow
        ((current--))
        if [ $current -lt 0 ]; then
          current=$((${#menu_items[@]} - 1))
        fi
        ;;
      '[B') # Down arrow
        ((current++))
        if [ $current -ge ${#menu_items[@]} ]; then
          current=0
        fi
        ;;
    esac
  elif [[ $key == "" ]]; then # Enter key
    execute_command
  fi
}

# Function to execute the selected command
execute_command() {
  clear
  echo "Executing: ${menu_items[$current]}"
  echo "-----------------------------"
  eval "${commands[$current]}"
  echo "-----------------------------"
  echo "Press any key to return to menu..."
  read -s -n 1
}

# Main loop
while true; do
  display_menu
  read_arrow
done
← Previous Next →
Back to list