dotfiles/scripts/bin/nato

43 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Script Name: nato
# Description: Convert strings to NATO phonetic alphabet
# Source: Inspired by https://evanhahn.com/scripts-i-wrote-that-i-use-all-the-time/
# Usage: nato bar # Output: Bravo Alfa Romeo
# nato "hello 123"
if [[ $# -eq 0 ]]; then
echo "Usage: nato <string>" >&2
exit 1
fi
# NATO phonetic alphabet mapping
declare -A nato_map=(
[a]="Alfa" [b]="Bravo" [c]="Charlie" [d]="Delta" [e]="Echo"
[f]="Foxtrot" [g]="Golf" [h]="Hotel" [i]="India" [j]="Juliett"
[k]="Kilo" [l]="Lima" [m]="Mike" [n]="November" [o]="Oscar"
[p]="Papa" [q]="Quebec" [r]="Romeo" [s]="Sierra" [t]="Tango"
[u]="Uniform" [v]="Victor" [w]="Whiskey" [x]="Xray" [y]="Yankee"
[z]="Zulu"
[0]="Zero" [1]="One" [2]="Two" [3]="Three" [4]="Four"
[5]="Five" [6]="Six" [7]="Seven" [8]="Eight" [9]="Niner"
)
# Convert all arguments to lowercase and process each character
input="$(echo "$*" | tr '[:upper:]' '[:lower:]')"
result=()
while IFS= read -r -n1 char; do
# Skip empty characters
[[ -z "$char" ]] && continue
if [[ -n "${nato_map[$char]:-}" ]]; then
result+=("${nato_map[$char]}")
elif [[ "$char" == " " ]]; then
result+=("/") # Use / as space separator
fi
done <<< "$input"
# Output with spaces between words
echo "${result[*]}"