2024-07-25 21:56:21 +08:00
|
|
|
|
#!/bin/bash
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare local_path=$1
|
|
|
|
|
declare user_choice
|
|
|
|
|
declare selected_file
|
2024-09-26 09:18:37 +08:00
|
|
|
|
echo "========$(basename $0 .sh)========"
|
2024-11-18 23:12:52 +08:00
|
|
|
|
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
|
2024-11-18 23:12:52 +08:00
|
|
|
|
file_count=$((file_count + 1))
|
|
|
|
|
file_array[$file_count]=$selected_file
|
|
|
|
|
echo "${file_count}.${selected_file}"
|
2024-09-23 02:44:39 +08:00
|
|
|
|
done
|
2024-11-18 23:12:52 +08:00
|
|
|
|
echo "输入其他任意值返回主页"
|
|
|
|
|
read -p "请输入:" user_choice
|
2024-07-25 21:56:21 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ ! ${user_choice} =~ [1-$file_count] ]];then
|
2024-07-25 21:56:21 +08:00
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
read -p "请输入脚本存放路径(默认:/var/script):" script_path
|
2024-07-25 21:56:21 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ -z $script_path ]];then
|
|
|
|
|
script_path='/var/script'
|
2024-07-25 21:56:21 +08:00
|
|
|
|
fi
|
2024-11-18 23:12:52 +08:00
|
|
|
|
mkdir -p "$script_path"
|
2024-07-25 21:56:21 +08:00
|
|
|
|
|
2024-07-26 18:45:04 +08:00
|
|
|
|
echo "执行日期"
|
|
|
|
|
echo "星号(*):表示匹配任意值"
|
|
|
|
|
echo "逗号(,):用于分隔多个值"
|
|
|
|
|
echo "斜线(/):用于指定间隔值"
|
|
|
|
|
echo "连字符(-):用于指定范围"
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
declare cron_expression
|
|
|
|
|
declare -a cron_fields=("分钟 0–59" "小时 0–23" "天数 1–31" "月份 1–12" "星期 0–7" )
|
|
|
|
|
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
|
2024-11-18 23:12:52 +08:00
|
|
|
|
cron_expression+="${tmp_time} "
|
2024-07-26 18:45:04 +08:00
|
|
|
|
elif [[ -z ${tmp_time} ]];then
|
2024-11-18 23:12:52 +08:00
|
|
|
|
cron_expression+='* '
|
2024-07-26 18:45:04 +08:00
|
|
|
|
else
|
|
|
|
|
echo "输入错误"
|
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
done
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ "$cron_expression" == '* * * * * ' ]];then
|
|
|
|
|
read -p "该脚本会无时无刻执行,请重新输入"
|
2024-07-26 18:45:04 +08:00
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
if [[ -f "${local_path}/${file_array[user_choice]}.sh" ]];then
|
2024-09-23 16:47:48 +08:00
|
|
|
|
echo "该路径文件已经存在"
|
|
|
|
|
fi
|
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
bash "${local_path}/${file_array[user_choice]}.sh" "$script_path"
|
2024-07-25 21:56:21 +08:00
|
|
|
|
|
2024-11-18 23:12:52 +08:00
|
|
|
|
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
|
|
|
|
|
2024-07-25 21:56:21 +08:00
|
|
|
|
echo "配置完成"
|