refactor(task_manager): replace recursion with while loop

This commit is contained in:
Sofia
2026-01-10 17:25:32 +08:00
committed by GitHub
parent 10e50dc244
commit 0712905fbe

View File

@@ -211,19 +211,23 @@ task_type(){ #任务条件选择菜单
;;
esac
}
task_manager(){ #任务管理列表
# 任务管理列表
task_manager() {
while true; do
echo "-----------------------------------------------"
#抽取并生成临时列表
croncmd -l > "$TMPDIR"/task_cronlist
cat "$TMPDIR"/task_cronlist "$CRASHDIR"/task/running 2>/dev/null | sort -u | grep -oE "task/task.sh .*" | awk -F ' ' '{print $2" "$3}' > "$TMPDIR"/task_list
cat "$CRASHDIR"/task/bfstart "$CRASHDIR"/task/afstart "$CRASHDIR"/task/affirewall 2>/dev/null | awk -F ' ' '{print $2" "$3}' >> "$TMPDIR"/task_list
cat "$TMPDIR"/task_cronlist 2>/dev/null | sort -u | grep -oE " #.*" | grep -v "守护" | awk -F '#' '{print "0 旧版任务-"$2}' >> "$TMPDIR"/task_list
# 抽取并生成临时列表
croncmd -l >"$TMPDIR"/task_cronlist
cat "$TMPDIR"/task_cronlist "$CRASHDIR"/task/running 2>/dev/null | sort -u | grep -oE "task/task.sh .*" | awk -F ' ' '{print $2" "$3}' >"$TMPDIR"/task_list
cat "$CRASHDIR"/task/bfstart "$CRASHDIR"/task/afstart "$CRASHDIR"/task/affirewall 2>/dev/null | awk -F ' ' '{print $2" "$3}' >>"$TMPDIR"/task_list
cat "$TMPDIR"/task_cronlist 2>/dev/null | sort -u | grep -oE " #.*" | grep -v "守护" | awk -F '#' '{print "0 旧版任务-"$2}' >>"$TMPDIR"/task_list
sed -i '/^ *$/d' "$TMPDIR"/task_list
rm -rf "$TMPDIR"/task_cronlist
#判断为空则返回
if [ ! -s "$TMPDIR"/task_list ];then
# 判断为空则返回
if [ ! -s "$TMPDIR"/task_list ]; then
echo -e "\033[31m当前没有可供管理的任务\033[36m"
sleep 1
break
else
echo -e "\033[33m已添加的任务:\033[0m"
echo "-----------------------------------------------"
@@ -234,30 +238,33 @@ task_manager(){ #任务管理列表
echo -e " 0 返回上级菜单"
read -p "请输入对应数字 > " num
case "$num" in
0)
"" | 0)
break
;;
a)
task_del "#"
echo -e "\033[31m旧版任务已清空\033[36m"
sleep 1
break
;;
d)
task_del "task.sh"
echo -e "\033[31m全部任务已清空\033[36m"
sleep 1
break
;;
[1-9]|[1-9][0-9])
[1-9] | [1-9][0-9])
task_txt=$(sed -n "$num p" "$TMPDIR"/task_list)
task_id=$(echo $task_txt | awk '{print $1}')
if [ "$task_id" = 0 ];then
if [ "$task_id" = 0 ]; then
read -p "旧版任务不支持管理,是否移除?(1/0) > " res
[ "$res" = 1 ] && {
cronname=$(echo $task_txt | awk -F '-' '{print $2}')
croncmd -l > $TMPDIR/conf && sed -i "/$cronname/d" $TMPDIR/conf && croncmd $TMPDIR/conf
croncmd -l >$TMPDIR/conf && sed -i "/$cronname/d" $TMPDIR/conf && croncmd $TMPDIR/conf
sed -i "/$cronname/d" $clashdir/tools/cron 2>/dev/null
rm -f $TMPDIR/conf
}
break
else
task_des=$(echo $task_txt | awk '{print $2}')
task_name=$(cat "$CRASHDIR"/task/task.list "$CRASHDIR"/task/task.user 2>/dev/null | grep "$task_id" | awk -F '#' '{print $3}')
@@ -271,7 +278,8 @@ task_manager(){ #任务管理列表
echo -e " 0 返回上级菜单"
read -p "请选择需要执行的操作 > " num
case "$num" in
0)
"" | 0)
return 0
;;
1)
task_type && task_del $task_des
@@ -287,7 +295,7 @@ task_manager(){ #任务管理列表
;;
4)
echo "-----------------------------------------------"
if [ -n "$(cat "$TMPDIR"/ShellCrash.log | grep "$task_name")" ];then
if [ -n "$(cat "$TMPDIR"/ShellCrash.log | grep "$task_name")" ]; then
cat "$TMPDIR"/ShellCrash.log | grep "$task_name"
else
echo -e "\033[31m未找到相关执行记录\033[0m"
@@ -296,17 +304,21 @@ task_manager(){ #任务管理列表
;;
*)
errornum
sleep 1
;;
esac
fi
task_manager
;;
*)
errornum
sleep 1
break
;;
esac
fi
done
}
task_recom(){ #任务推荐
echo "-----------------------------------------------"
echo -e "\033[32m启用推荐的自动任务配置这包括\033[0m"