linuxtool/Config/Automation/Installs/menu.sh

68 lines
2.1 KiB
Bash
Raw Normal View History

#!/bin/bash
declare local_path=$1
declare user_choice
declare selected_file
2024-09-26 09:18:37 +08:00
echo "========$(basename $0 .sh)========"
declare file_count=0
declare -a file_array
for file in "${local_path}"/*;do
selected_file=$(awk -F '.' '{print $1}' <<< "$(basename $file)")
if [[ $selected_file == "test" || $selected_file == "menu" ]]; then
2024-09-26 09:18:37 +08:00
continue
fi
file_count=$((file_count + 1))
file_array[$file_count]=$selected_file
echo "${file_count}.${selected_file}"
done
echo "输入其他任意值返回主页"
read -p "请输入:" user_choice
if [[ ! ${user_choice} =~ [1-$file_count] ]];then
exit
fi
read -p "请输入脚本存放路径(默认:/var/script" script_path
if [[ -z $script_path ]];then
script_path='/var/script'
fi
mkdir -p "$script_path"
2024-07-26 18:45:04 +08:00
echo "执行日期"
echo "星号(*):表示匹配任意值"
echo "逗号(,):用于分隔多个值"
echo "斜线(/):用于指定间隔值"
echo "连字符(-):用于指定范围"
declare cron_expression
declare -a cron_fields=("分钟 059" "小时 023" "天数 131" "月份 112" "星期 07" )
for field in "${cron_fields[@]}";do
read -p "${field},默认为 * " tmp_time
2024-07-26 18:45:04 +08:00
if [[ $tmp_time =~ ^[0-9]+$ || $tmp_time == '*' ]];then
cron_expression+="${tmp_time} "
2024-07-26 18:45:04 +08:00
elif [[ -z ${tmp_time} ]];then
cron_expression+='* '
2024-07-26 18:45:04 +08:00
else
echo "输入错误"
exit
fi
done
if [[ "$cron_expression" == '* * * * * ' ]];then
read -p "该脚本会无时无刻执行,请重新输入"
2024-07-26 18:45:04 +08:00
exit
fi
if [[ -f "${local_path}/${file_array[user_choice]}.sh" ]];then
echo "该路径文件已经存在"
fi
bash "${local_path}/${file_array[user_choice]}.sh" "$script_path"
chmod +x "${script_path}/${file_array[user_choice]}.sh" && echo "脚本执行权限添加成功" || echo "脚本执行权限添加失败"
declare cron_job="${cron_expression} ${script_path}/${file_array[user_choice]}.sh"
(crontab -l 2>/dev/null | grep -v "${file_array[user_choice]}.sh") | crontab -
2024-09-23 17:59:28 +08:00
(crontab -l 2>/dev/null; echo "$cron_job") | crontab -
2024-07-26 18:39:43 +08:00
2024-09-26 19:46:58 +08:00
systemctl restart cron 2>> /dev/null && echo "自动任务配置成功" || echo "自动任务重启失败"
2024-07-26 18:39:43 +08:00
echo "配置完成"