From e696b879930d7a2b9d996039f8cf1f3635db2ca5 Mon Sep 17 00:00:00 2001 From: lsy Date: Tue, 19 Nov 2024 16:47:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E7=AE=A1=E7=90=86bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Config/Automation/manage.sh | 75 +++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/Config/Automation/manage.sh b/Config/Automation/manage.sh index e252b9b..bd3e8b2 100644 --- a/Config/Automation/manage.sh +++ b/Config/Automation/manage.sh @@ -1,59 +1,70 @@ #!/bin/bash + echo "1. 查看已安装的脚本" echo "2. 删除脚本" -declare user_choice read -p "请输入您的选择:" user_choice -declare script_directory="/var/script" -echo "请输入脚本安装目录,默认是 ${script_directory}" -read -p "请输入:" script_directory +script_directory="/var/script" +read -p "请输入脚本安装目录,默认是 ${script_directory}:" input_directory -if [[ -z $script_directory ]]; then - script_directory="/var/script" -elif ! [[ -d $script_directory ]]; then +if [[ -n $input_directory ]]; then + script_directory="$input_directory" +fi + +if [[ ! -d $script_directory ]]; then echo "该目录不存在" - exit + exit 1 fi function list_scripts() { - declare -a installed_scripts - declare script_name - declare script_count=0 + local installed_scripts=() + local script_count=0 + for script in "$script_directory"/*; do - if [[ $script == "${script_directory}/*" ]]; then + if [[ ! -e $script ]]; then echo "该目录没有脚本" - exit + return fi - script_name=$(awk -F '.' '{print $1}' <<<"$(basename $script)") + local script_name=$(basename "$script" | awk -F '.' '{print $1}') if [[ $script_name == "linuxtool" ]]; then continue fi script_count=$((script_count + 1)) echo "${script_count}.${script_name}" - installed_scripts[$script_count]=$script_name + installed_scripts+=("$script_name") done - if [ ${#installed_scripts[@]} == 0 ]; then + + if [[ ${#installed_scripts[@]} -eq 0 ]]; then echo "该目录没有脚本" - exit + return fi + echo "${installed_scripts[@]}" } case $user_choice in -'1') - list_scripts - ;; -'2') - installed_scripts=($(list_scripts)) - read -p "请输入要删除的序号(多个用空格隔开):" script_name - for i in $script_name; do - if [[ $i =~ [1-${#installed_scripts[@]}] ]]; then - echo "开始删除 ${installed_scripts[$i]}" - (crontab -l 2>/dev/null | grep -v "${installed_scripts[$i]}") | crontab - && echo "已删除脚本的自动任务" - rm -rf "$script_directory/${installed_scripts[$i]}" &>/dev/null - echo "删除完成" - fi - done - ;; + '1') + list_scripts + ;; + '2') + installed_scripts=($(list_scripts)) + read -p "请输入要删除的序号(多个用空格隔开):" script_indices + + for index in $script_indices; do + if [[ $index =~ ^[1-9][0-9]*$ ]] && [ $index -le ${#installed_scripts[@]} ]; then + local script_to_delete=${installed_scripts[$((index - 1))]} + echo "开始删除 ${script_to_delete}" + (crontab -l 2>/dev/null | grep -v "$script_to_delete") | crontab - && echo "已删除脚本的自动任务" + rm -rf "$script_directory/$script_to_delete" &>/dev/null + echo "删除完成" + else + echo "无效的序号: $index" + fi + done + ;; + *) + echo "无效的选择" + exit 1 + ;; esac