From 5cd803b90cf2eeef251787ff85c501da0c98a5d8 Mon Sep 17 00:00:00 2001 From: FlintyLemming Date: Tue, 3 Mar 2026 23:23:51 +0800 Subject: [PATCH] fix: read interactive input from /dev/tty for curl|bash runs --- linux-managements/menu.sh | 15 ++++++++++++++- linux-managements/setup.sh | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/linux-managements/menu.sh b/linux-managements/menu.sh index a53d042..27cb096 100644 --- a/linux-managements/menu.sh +++ b/linux-managements/menu.sh @@ -7,6 +7,19 @@ BLUE='\033[0;34m'; CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m' _NAMES=(); _DESCS=(); _URLS=() add() { _NAMES+=("$1"); _DESCS+=("$2"); _URLS+=("$3"); } +# Read from terminal even when script is run via `curl ... | bash`. +prompt_read() { + local __var_name="$1" + local __prompt="$2" + local __input="" + if [ -r /dev/tty ]; then + read -r -p "$__prompt" __input < /dev/tty + else + read -r -p "$__prompt" __input + fi + printf -v "$__var_name" '%s' "$__input" +} + # ════════════════════════════════════════════════════════════════════════════════ # CONFIGURATION — 在此处添加或修改脚本条目 # 用法: add "<显示名称>" "<脚本说明>" "<脚本 URL>" @@ -67,7 +80,7 @@ main() { while true; do draw_menu - read -rp "请选择要执行的脚本 [0-$total]: " choice + prompt_read choice "请选择要执行的脚本 [0-$total]: " if [[ "$choice" == "0" ]]; then echo -e "\n${CYAN}[INFO]${NC} 已退出。\n" diff --git a/linux-managements/setup.sh b/linux-managements/setup.sh index 97bbdc2..5cdb8c4 100755 --- a/linux-managements/setup.sh +++ b/linux-managements/setup.sh @@ -11,6 +11,19 @@ warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } error() { echo -e "${RED}[ERR]${NC} $*" >&2; } step() { echo -e "\n${BOLD}${BLUE}══ $* ${NC}"; } +# Read from terminal even when script is run via `curl ... | bash`. +prompt_read() { + local __var_name="$1" + local __prompt="$2" + local __input="" + if [ -r /dev/tty ]; then + read -r -p "$__prompt" __input < /dev/tty + else + read -r -p "$__prompt" __input + fi + printf -v "$__var_name" '%s' "$__input" +} + # ─── OS Detection ───────────────────────────────────────────────────────────── detect_os() { if [ -f /etc/os-release ]; then @@ -43,11 +56,11 @@ detect_os() { setup_proxy() { step "HTTP Proxy" echo -e "Do you want to configure an HTTP proxy for this session? ${YELLOW}(helps with Homebrew downloads)${NC}" - read -rp "Configure proxy? [y/N] " ans + prompt_read ans "Configure proxy? [y/N] " case "$ans" in [Yy]*) while true; do - read -rp "Enter proxy URL (e.g. http://192.168.1.1:7890): " proxy_url + prompt_read proxy_url "Enter proxy URL (e.g. http://192.168.1.1:7890): " if [[ "$proxy_url" =~ ^https?://[^:]+:[0-9]+$ ]]; then break fi @@ -91,7 +104,7 @@ setup_ssh_key() { local added=0 while true; do - read -rp "Public key (or blank to finish): " pubkey + prompt_read pubkey "Public key (or blank to finish): " [[ -z "$pubkey" ]] && break if [[ "$pubkey" =~ ^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|sk-ssh-ed25519) ]]; then if grep -qF "$pubkey" ~/.ssh/authorized_keys 2>/dev/null; then