修复自动化管理问题

This commit is contained in:
lsy 2024-11-19 00:06:43 +08:00
parent af47b8f1b5
commit 66ba7dc088
2 changed files with 39 additions and 58 deletions

View File

@ -1,78 +1,59 @@
#!/bin/bash #!/bin/bash
echo "1. 查看已安装的脚本"
echo "2. 删除脚本"
# 显示菜单 declare user_choice
function show_menu() { read -p "请输入您的选择:" user_choice
echo "1. 查看已安装的脚本"
echo "2. 删除脚本"
}
# 获取用户选择 declare script_directory="/var/script"
function get_user_choice() { echo "请输入脚本安装目录,默认是 ${script_directory}"
read -p "请输入您的选择:" user_choice read -p "请输入:" script_directory
echo $user_choice
}
# 获取脚本目录 if [[ -z $script_directory ]]; then
function get_script_directory() { script_directory="/var/script"
local default_directory="/var/script" elif ! [[ -d $script_directory ]]; then
read -p "请输入脚本安装目录(默认是 ${default_directory}" script_directory echo "该目录不存在"
[[ -z $script_directory ]] && script_directory=$default_directory exit
echo $script_directory fi
}
# 列出已安装的脚本 function list_scripts() {
function list_installed_scripts() { declare -a installed_scripts
local directory=$1 declare script_name
declare -a scripts declare script_count=0
local count=0 for script in "$script_directory"/* ; do
if [[ $script == "${script_directory}/*" ]];then
for script in "$directory"/* ; do
if [[ $script == "${directory}/*" ]]; then
echo "该目录没有脚本" echo "该目录没有脚本"
return exit
fi fi
local script_name=$(awk -F '.' '{print $1}' <<< "$(basename $script)") script_name=$(awk -F '.' '{print $1}' <<< "$(basename $script)")
if [[ $script_name == "linuxtool" ]]; then if [[ $script_name == "linuxtool" ]]; then
continue continue
fi fi
count=$(( count + 1 )) script_count=$(( script_count+1 ))
echo "${count}.${script_name}" echo "${script_count}.${script_name}"
scripts[$count]=$script_name installed_scripts[$script_count]=$script_name
done done
if [ ${#installed_scripts[@]} == 0 ]; then
if [ ${#scripts[@]} == 0 ]; then
echo "该目录没有脚本" echo "该目录没有脚本"
return exit
fi fi
echo "${installed_scripts[@]}"
echo "${scripts[@]}"
} }
# 删除脚本
function delete_scripts() {
local scripts=("$@")
read -p "请输入要删除的序号(多个用空格隔开):" indices
for i in $indices ; do
if [[ $i =~ ^[1-9][0-9]*$ ]] && [ $i -le ${#scripts[@]} ]; then
echo "开始删除 ${scripts[$i]}"
(crontab -l 2>/dev/null | grep -v "${scripts[$i]}") | crontab - && echo "已删除脚本的自动任务"
rm -rf "$script_directory/${scripts[$i]}" &> /dev/null
echo "删除完成"
fi
done
}
# 主程序
show_menu
user_choice=$(get_user_choice)
script_directory=$(get_script_directory)
case $user_choice in case $user_choice in
'1') '1')
installed_scripts=($(list_installed_scripts "$script_directory")) list_scripts
;; ;;
'2') '2')
installed_scripts=($(list_installed_scripts "$script_directory")) installed_scripts=($(list_scripts))
delete_scripts "${installed_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
;; ;;
esac esac

View File

@ -47,6 +47,6 @@ case $user_choice in
echo "删除完成" echo "删除完成"
fi fi
done done
echo "删除完成" echo "全部删除完成"
;; ;;
esac esac