如何精简Bash终端样式选择脚本?(优化if/elif分支逻辑)
Optimized Bash Script: Replace
if/elif Chains with Arrays Great question! Those endless if/elif branches are a nightmare to maintain—especially when you have 18 options. Let's refactor your script to use arrays instead, making it cleaner, more scalable, and easier to update later.
Here's the Refactored Script
#!/bin/bash clear # Safe input handling with -r to avoid backslash escaping echo "Hello, who am I talking to?" read -rp 'String: ' string echo "$string" # Define arrays to map style options to their ANSI codes and names # Indexes 0-17 correspond to user choices 1-18 style_previews=( '\e[1;4;33m' # 1: Bold preview '\e[4;33m' # 2: Underlined preview '\e[30;47m' # 3: Black foreground preview '\e[31;47m' # 4: Red foreground preview '\e[32m' # 5: Green foreground preview '\e[33m' # 6: Yellow foreground preview '\e[34m' # 7: Blue foreground preview '\e[35m' # 8: Purple foreground preview '\e[36m' # 9: Cyan foreground preview '\e[0;37;40m' # 10: White foreground preview '\e[1;4;33m' # 11: Black background preview '\e[1;4;33m' # 12: Red background preview '\e[1;4;33m' # 13: Green background preview '\e[1;4;33m' # 14: Yellow background preview '\e[1;4;33m' # 15: Blue background preview '\e[1;4;33m' # 16: Purple background preview '\e[1;4;33m' # 17: Cyan background preview '\e[1;4;33m' # 18: White background preview ) style_names=( "Bold string" "Underlined string" "Black foreground" "Red foreground" "Green foreground" "Yellow foreground" "Blue foreground" "Purple foreground" "Cyan foreground" "White foreground" "Black background" "Red background" "Green background" "Yellow background" "Blue background" "Purple background" "Cyan background" "White background" ) style_codes=( '\e[1;37m' # 1: Bold '\e[4;37m' # 2: Underlined '\e[30m' # 3: Black foreground '\e[31m' # 4: Red foreground '\e[32m' # 5: Green foreground '\e[33m' # 6: Yellow foreground '\e[34m' # 7: Blue foreground '\e[35m' # 8: Purple foreground '\e[36m' # 9: Cyan foreground '\e[37m' # 10: White foreground '\e[40m' # 11: Black background '\e[41m' # 12: Red background '\e[42m' # 13: Green background '\e[43m' # 14: Yellow background '\e[44m' # 15: Blue background '\e[45m' # 16: Purple background '\e[46m' # 17: Cyan background '\e[47m' # 18: White background ) # Generate the menu dynamically with a loop echo "Please select a modifier from this list by typing the modifier's number." for i in "${!style_previews[@]}"; do choice=$((i + 1)) printf "${style_previews[$i]}[%d] %s\n\e[0m" "$choice" "${style_names[$i]}" done # Get user choice and validate read -rp 'Number: ' number if ! [[ "$number" =~ ^([1-9]|1[0-8])$ ]]; then echo 'Please only select a number from 1 to 18' exit 1 fi # Map user choice to array index (subtract 1 since arrays start at 0) index=$((number - 1)) selected_code="${style_codes[$index]}" # Output the styled string and its code printf "%s%s\e[0m\n" "$selected_code" "$string" echo "${selected_code}${string}\e[0m"
Key Improvements Explained
- No more
if/elifchains: By storing all style data in arrays, we can directly access the correct style code using the user's input (adjusted for 0-indexed arrays). This makes adding/removing styles as simple as updating the arrays—no need to edit a huge block of conditionals. - Dynamic menu generation: The
forloop builds the style menu automatically from the arrays. No more copying and pastingprintflines for each option. - Safer input handling: Added
-rtoreadto prevent unintended backslash escaping, and quoted variables ("$string","$number") to handle spaces in user input correctly. - Clean terminal reset: Added
\e[0mafter all styled output to reset the terminal to default style, so subsequent commands aren't affected by the selected style. - Fixed validation: Updated the regex to correctly match all numbers 1-18 (your original regex missed 11-17!).
内容的提问来源于stack exchange,提问作者GoDzM4TT3O
相关产品推荐
相关产品推荐

