Skip to content

Commit 78a7b39

Browse files
authored
fix(script/v4.sh): handle non-interactive mode and add proxy settings when set autoupdate (#19)
* fix(script/v4.sh): 修复脚本在非交互模式下的处理逻辑 新增对非交互模式(如 cron 后台运行)的检测与处理,避免阻塞 同时优化了 GitHub 代理设置逻辑,在定时更新中支持可选的代理配置,并将更新日志输出到文件。 * 后台定时更新的日志文件存放在工作目录下
1 parent 30440d5 commit 78a7b39

1 file changed

Lines changed: 56 additions & 27 deletions

File tree

script/v4.sh

100644100755
Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,22 @@ check_disk_space() {
8888
echo -e "临时目录可用空间: $tmp_space"
8989
echo -e "安装目录可用空间: $install_space"
9090
echo -e "${YELLOW_COLOR}建议清理系统空间后再继续${RES}"
91-
read -p "是否继续?[y/N]: " continue_choice
92-
case "$continue_choice" in
93-
[yY])
94-
return 0
95-
;;
96-
*)
97-
exit 1
98-
;;
99-
esac
91+
# 如果是非交互(后台/cron)模式,直接返回失败以避免阻塞
92+
if [ ! -t 0 ]; then
93+
echo -e "${YELLOW_COLOR}非交互模式:检测到可用空间不足,自动退出以避免阻塞${RES}"
94+
return 1
95+
fi
96+
97+
# 交互模式才提示用户继续
98+
read -p "是否继续?[y/N]: " continue_choice
99+
case "$continue_choice" in
100+
[yY])
101+
return 0
102+
;;
103+
*)
104+
exit 1
105+
;;
106+
esac
100107
fi
101108
fi
102109
}
@@ -533,15 +540,27 @@ setup_auto_update() {
533540
echo -e "${YELLOW_COLOR}默认:每周日凌晨2点 (0 2 * * 0)${RES}"
534541
echo -e "${YELLOW_COLOR}示例:每天凌晨3点 (0 3 * * *)${RES}"
535542
read -p "请输入 cron 时间表达式 (默认: 0 2 * * 0): " cron_time
536-
537543
if [ -z "$cron_time" ]; then
538544
cron_time="0 2 * * 0"
539545
fi
540546

547+
# 询问是否使用 GitHub 代理(可选),并保存在 crontab 环境中
548+
echo -e "${GREEN_COLOR}是否为定时更新设置 GitHub 代理?(可选,直接按 Enter 跳过)${RES}"
549+
echo -e "${GREEN_COLOR}示例:https://ghproxy.com/ 或 https://ghproxy.net/ ${RES}"
550+
read -p "请输入代理地址或直接按 Enter: " cron_gh_proxy
551+
541552
SCRIPT_PATH=$(readlink -f "$0")
542553

543-
# 添加到 crontab
544-
(crontab -l 2>/dev/null | grep -v "openlist.*update"; echo "$cron_time $SCRIPT_PATH update >/dev/null 2>&1") | crontab -
554+
# 构造要写入 crontab 的命令行,保留 GH_PROXY 环境变量(如果用户设置)
555+
if [ -n "$cron_gh_proxy" ]; then
556+
# 将 GH_PROXY 导出到 crontab 环境并把输出写入日志文件
557+
cron_cmd="GH_PROXY=$cron_gh_proxy $SCRIPT_PATH update >> openlist_update.log 2>&1"
558+
else
559+
cron_cmd="$SCRIPT_PATH update >> openlist_update.log 2>&1"
560+
fi
561+
562+
# 添加到 crontab(先移除旧条目)
563+
(crontab -l 2>/dev/null | grep -v "openlist.*update"; echo "$cron_time $cron_cmd") | crontab -
545564

546565
echo -e "${GREEN_COLOR}定时更新已启用${RES}"
547566
echo -e "${GREEN_COLOR}更新时间:$cron_time${RES}"
@@ -812,22 +831,32 @@ UPDATE() {
812831

813832
echo -e "${GREEN_COLOR}开始更新 OpenList ...${RES}"
814833

815-
# 询问是否使用代理
816-
echo -e "${GREEN_COLOR}是否使用 GitHub 代理?(默认无代理)${RES}"
817-
echo -e "${GREEN_COLOR}代理地址必须为 https 开头,斜杠 / 结尾 ${RES}"
818-
echo -e "${GREEN_COLOR}例如:https://ghproxy.com/ ${RES}"
819-
read -p "请输入代理地址或直接按 Enter 继续: " proxy_input
820-
821-
# 如果用户输入了代理地址,则使用代理拼接下载链接
822-
if [ -n "$proxy_input" ]; then
823-
GH_PROXY="$proxy_input"
824-
GH_DOWNLOAD_URL="${GH_PROXY}https://github.com/OpenListTeam/OpenList/releases/download"
825-
echo -e "${GREEN_COLOR}已使用代理地址: $GH_PROXY${RES}"
834+
# 询问是否使用代理(仅在交互式终端时询问)
835+
if [ -t 0 ]; then
836+
echo -e "${GREEN_COLOR}是否使用 GitHub 代理?(默认无代理)${RES}"
837+
echo -e "${GREEN_COLOR}代理地址必须为 https 开头,斜杠 / 结尾 ${RES}"
838+
echo -e "${GREEN_COLOR}例如:https://ghproxy.com/ ${RES}"
839+
read -p "请输入代理地址或直接按 Enter 继续: " proxy_input
840+
841+
# 如果用户输入了代理地址,则使用代理拼接下载链接
842+
if [ -n "$proxy_input" ]; then
843+
GH_PROXY="$proxy_input"
844+
GH_DOWNLOAD_URL="${GH_PROXY}https://github.com/OpenListTeam/OpenList/releases/download"
845+
echo -e "${GREEN_COLOR}已使用代理地址: $GH_PROXY${RES}"
846+
else
847+
# 如果不需要代理,直接使用默认链接
848+
GH_PROXY=""
849+
GH_DOWNLOAD_URL="https://github.com/OpenListTeam/OpenList/releases/download"
850+
echo -e "${GREEN_COLOR}使用默认 GitHub 地址进行下载${RES}"
851+
fi
826852
else
827-
# 如果不需要代理,直接使用默认链接
828-
GH_PROXY=""
829-
GH_DOWNLOAD_URL="https://github.com/OpenListTeam/OpenList/releases/download"
830-
echo -e "${GREEN_COLOR}使用默认 GitHub 地址进行下载${RES}"
853+
# 非交互模式(例如后台或 cron)使用环境变量 GH_PROXY(如果设置)或默认下载地址
854+
if [ -n "$GH_PROXY" ]; then
855+
GH_DOWNLOAD_URL="${GH_PROXY}https://github.com/OpenListTeam/OpenList/releases/download"
856+
else
857+
GH_PROXY=""
858+
GH_DOWNLOAD_URL="https://github.com/OpenListTeam/OpenList/releases/download"
859+
fi
831860
fi
832861

833862
# 获取真实版本信息

0 commit comments

Comments
 (0)