Compare commits

..

No commits in common. "9126ea03125bae0ec94d132d40cfde7d202dc46c" and "2ddc1b51298f4cee7fa7812732a577061cbddfb4" have entirely different histories.

View File

@ -1,5 +1,4 @@
#!/bin/bash #!/bin/bash
echo "1. 查看已安装的脚本" echo "1. 查看已安装的脚本"
echo "2. 删除脚本" echo "2. 删除脚本"
@ -7,26 +6,26 @@ declare user_choice
read -p "请输入您的选择:" user_choice read -p "请输入您的选择:" user_choice
declare script_directory="/var/script" declare script_directory="/var/script"
echo "请输入脚本安装目录,默认是 ${script_directory}" echo "请输入脚本安装目录,默认是 ${script_directory}"
read -p "请输入:" input_directory read -p "请输入:" script_directory
if [[ -n $input_directory ]]; then if [[ -z $script_directory ]]; then
script_directory="$input_directory" script_directory="/var/script"
fi elif ! [[ -d $script_directory ]]; then
if [[ ! -d $script_directory ]]; then
echo "该目录不存在" echo "该目录不存在"
exit 1 exit
fi fi
function list_scripts() { function list_scripts() {
local script_count=0 declare -a installed_scripts
declare script_name
declare script_count=0
for script in "$script_directory"/*; do for script in "$script_directory"/*; do
if [[ ! -e $script ]]; then if [[ $script == "${script_directory}/*" ]]; then
echo "该目录没有脚本" echo "该目录没有脚本"
return exit
fi fi
local script_name=$(basename "$script" | awk -F '.' '{print $1}') script_name=$(awk -F '.' '{print $1}' <<<"$(basename $script)")
if [[ $script_name == "linuxtool" ]]; then if [[ $script_name == "linuxtool" ]]; then
continue continue
fi fi
@ -34,36 +33,27 @@ function list_scripts() {
echo "${script_count}.${script_name}" echo "${script_count}.${script_name}"
installed_scripts[$script_count]=$script_name installed_scripts[$script_count]=$script_name
done done
if [ ${#installed_scripts[@]} == 0 ]; then
if [[ ${#installed_scripts[@]} -eq 0 ]]; then
echo "该目录没有脚本" echo "该目录没有脚本"
return exit
fi fi
echo "${installed_scripts[@]}"
} }
case $user_choice in case $user_choice in
'1') '1')
list_scripts list_scripts
;; ;;
'2') '2')
declare -a installed_scripts installed_scripts=($(list_scripts))
list_scripts read -p "请输入要删除的序号(多个用空格隔开):" script_name
read -p "请输入要删除的序号(多个用空格隔开):" script_indices for i in $script_name; do
if [[ $i =~ [1-${#installed_scripts[@]}] ]]; then
for index in $script_indices; do echo "开始删除 ${installed_scripts[$i]}"
if [[ $index =~ ^[1-9][0-9]*$ ]] && [ $index -le ${#installed_scripts[@]} ]; then (crontab -l 2>/dev/null | grep -v "${installed_scripts[$i]}") | crontab - && echo "已删除脚本的自动任务"
local script_to_delete=${installed_scripts[$((index - 1))]} rm -rf "$script_directory/${installed_scripts[$i]}" &>/dev/null
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 "删除完成" echo "删除完成"
else
echo "无效的序号: $index"
fi fi
done done
;; ;;
*)
echo "无效的选择"
exit 1
;;
esac esac