You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 to more—both are preinstalled on all four target systems, so no extra dependencies needed.
  • Input Handling: AIX's read command treats backslashes as escape characters by default, so we add -r there to avoid unexpected behavior. Other systems work with standard read.
  • 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, less is usually in /usr/bin/less; the command -v check will find it automatically.
  • On HP-UX, more is the default pager, but less is often available too—if not, the fallback works seamlessly.

内容的提问来源于stack exchange,提问作者Srikanth Ganesan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:16:00