linuxtool/Config/Automation/Installs/menu.sh

68 lines
1.8 KiB
Bash
Raw Normal View History

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