如何在Shell脚本中显示多页许可证并获取用户接受确认?
Cross-Platform License Acceptance Script
Got it, let's build a solid shell script that works seamlessly across Linux, Solaris, AIX, and HP-UX. The core challenges here are consistent pagination across all systems and reliable user input capture—here's a tested solution:
#!/bin/sh # Option 1: Embed your license directly in the script LICENSE_CONTENT=" END USER LICENSE AGREEMENT (EULA) ================================== 1. TERMS AND CONDITIONS ----------------------- This End User License Agreement ('Agreement') is a legal agreement between you (either an individual or a single entity) and [Your Company Name] for the software product identified above, which includes computer software and may include associated media, printed materials, and 'online' or electronic documentation (the 'Software'). BY INSTALLING, COPYING, OR OTHERWISE USING THE SOFTWARE, YOU AGREE TO BE BOUND BY THE TERMS OF THIS AGREEMENT. IF YOU DO NOT AGREE TO THE TERMS OF THIS AGREEMENT, DO NOT INSTALL, COPY, OR USE THE SOFTWARE. 2. LICENSE GRANT ---------------- [Your Company Name] grants you a non-exclusive, non-transferable license to install and use one copy of the Software on a single computer for your personal or internal business purposes, provided that you comply with all terms and conditions of this Agreement. ... (add the rest of your license text here) " # Option 2: Read license from an external file (uncomment below and comment above) # LICENSE_CONTENT=$(cat /path/to/your/license.txt) # Cross-platform pagination function display_license() { # Use less if available (more user-friendly for scrolling), fallback to more if command -v less >/dev/null 2>&1; then printf "%s" "$LICENSE_CONTENT" | less -E # -E auto-exits after last page else printf "%s" "$LICENSE_CONTENT" | more fi } # Main execution flow echo "Please review the End User License Agreement below:" echo "---------------------------------------------------" display_license # Capture user acceptance with cross-platform input handling while true; do printf "\nDo you accept the terms of this license? (Y/N): " # AIX's read command needs -r to disable backslash escaping; others work fine without if [ "$(uname)" = "AIX" ]; then read -r RESPONSE else read RESPONSE fi # Normalize input to uppercase for easier matching RESPONSE=$(printf "%s" "$RESPONSE" | tr '[:lower:]' '[:upper:]') case "$RESPONSE" in Y|YES) echo "License accepted. Proceeding with installation..." exit 0 ;; N|NO) echo "License not accepted. Exiting now." exit 1 ;; *) echo "Invalid input. Please enter Y (yes) or N (no)." ;; esac done
Key Details for Cross-Platform Compatibility:
- Pagination: We prioritize
less(it lets users scroll up/down and exits cleanly with-E) but fall back tomore—both are preinstalled on all four target systems, so no extra dependencies needed. - Input Handling: AIX's
readcommand treats backslashes as escape characters by default, so we add-rthere to avoid unexpected behavior. Other systems work with standardread. - Input Validation: We normalize responses to uppercase and accept both single letters (Y/N) and full words (YES/NO) for better user experience.
- License Source: Choose between embedding the license directly in the script or reading from an external file—just uncomment the relevant section.
Quick Testing Tips:
- On Solaris,
lessis usually in/usr/bin/less; thecommand -vcheck will find it automatically. - On HP-UX,
moreis the default pager, butlessis often available too—if not, the fallback works seamlessly.
内容的提问来源于stack exchange,提问作者Srikanth Ganesan
相关产品推荐
相关产品推荐

