fix: read interactive input from /dev/tty for curl|bash runs

This commit is contained in:
FlintyLemming
2026-03-03 23:23:51 +08:00
parent 72fefab22d
commit 5cd803b90c
2 changed files with 30 additions and 4 deletions

View File

@@ -7,6 +7,19 @@ BLUE='\033[0;34m'; CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
_NAMES=(); _DESCS=(); _URLS=() _NAMES=(); _DESCS=(); _URLS=()
add() { _NAMES+=("$1"); _DESCS+=("$2"); _URLS+=("$3"); } 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 — 在此处添加或修改脚本条目 # CONFIGURATION — 在此处添加或修改脚本条目
# 用法: add "<显示名称>" "<脚本说明>" "<脚本 URL>" # 用法: add "<显示名称>" "<脚本说明>" "<脚本 URL>"
@@ -67,7 +80,7 @@ main() {
while true; do while true; do
draw_menu draw_menu
read -rp "请选择要执行的脚本 [0-$total]: " choice prompt_read choice "请选择要执行的脚本 [0-$total]: "
if [[ "$choice" == "0" ]]; then if [[ "$choice" == "0" ]]; then
echo -e "\n${CYAN}[INFO]${NC} 已退出。\n" echo -e "\n${CYAN}[INFO]${NC} 已退出。\n"

View File

@@ -11,6 +11,19 @@ warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERR]${NC} $*" >&2; } error() { echo -e "${RED}[ERR]${NC} $*" >&2; }
step() { echo -e "\n${BOLD}${BLUE}══ $* ${NC}"; } 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 ───────────────────────────────────────────────────────────── # ─── OS Detection ─────────────────────────────────────────────────────────────
detect_os() { detect_os() {
if [ -f /etc/os-release ]; then if [ -f /etc/os-release ]; then
@@ -43,11 +56,11 @@ detect_os() {
setup_proxy() { setup_proxy() {
step "HTTP Proxy" step "HTTP Proxy"
echo -e "Do you want to configure an HTTP proxy for this session? ${YELLOW}(helps with Homebrew downloads)${NC}" 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 case "$ans" in
[Yy]*) [Yy]*)
while true; do 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 if [[ "$proxy_url" =~ ^https?://[^:]+:[0-9]+$ ]]; then
break break
fi fi
@@ -91,7 +104,7 @@ setup_ssh_key() {
local added=0 local added=0
while true; do while true; do
read -rp "Public key (or blank to finish): " pubkey prompt_read pubkey "Public key (or blank to finish): "
[[ -z "$pubkey" ]] && break [[ -z "$pubkey" ]] && break
if [[ "$pubkey" =~ ^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|sk-ssh-ed25519) ]]; then if [[ "$pubkey" =~ ^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|sk-ssh-ed25519) ]]; then
if grep -qF "$pubkey" ~/.ssh/authorized_keys 2>/dev/null; then if grep -qF "$pubkey" ~/.ssh/authorized_keys 2>/dev/null; then