优化文件路径结构

This commit is contained in:
lsy 2025-05-19 22:41:57 +08:00
parent 607b6a723d
commit 551f728eb2
9 changed files with 122 additions and 108 deletions

View File

@ -0,0 +1,57 @@
#!/bin/bash
# 接收参数
local_path=$1
selected_script=$2
# 配置存储路径
declare storage_path
read -p "请输入软件存储位置,默认 /var/www/${selected_script} " storage_path
if [[ -z ${storage_path} ]]; then
storage_path="/var/www/${selected_script}"
fi
# 检查并创建目录
if [[ ! -d "$storage_path" ]]; then
mkdir -p "$storage_path" || {
echo "目录创建失败"
exit 1
}
elif [[ ! -z "$(find "$storage_path" -mindepth 1 -print -quit)" ]]; then
echo "该目录存有文件"
read -p "是否继续?(y/n): " continue_choice
if [[ ! $continue_choice =~ [Yy] ]]; then
echo "安装已取消"
exit
fi
fi
# 生成随机端口
declare random_port=$(($RANDOM % 9000 + 1000))
while ss -tuln | grep $random_port &>/dev/null; do
random_port=$(($RANDOM % 9000 + 1000))
done
# 配置访问端口
declare access_port
read -p "请输入访问端口,默认 $random_port " access_port
if [[ -z $access_port ]]; then
access_port=$random_port
fi
# 检查端口是否被占用
if ss -tuln | grep $access_port &>/dev/null; then
echo "端口已被占用"
read -p "是否使用其他端口?(y/n): " port_choice
if [[ $port_choice =~ [Yy] ]]; then
read -p "请输入新的端口: " access_port
else
echo "安装已取消"
exit
fi
fi
# 执行原始安装脚本
bash "${local_path}/${selected_script}.sh" "$storage_path" "$access_port"
echo "${selected_script} 安装完成,访问端口 ${access_port}"

View File

@ -1,59 +0,0 @@
#!/bin/bash
declare local_directory=$1
declare user_choice
declare selected_file_name
echo "========$(basename $0 .sh)========"
declare file_count=0
declare -a file_array
for file in "${local_directory}"/*; do
selected_file_name=$(awk -F '.' '{print $1}' <<<"$(basename $file)")
if [[ $selected_file_name == "test" || $selected_file_name == "menu" ]]; then
continue
fi
file_count=$((file_count + 1))
file_array[$file_count]=$selected_file_name
echo "${file_count}.${selected_file_name}"
done
echo "输入其他字符返回主页"
read -p "请输入:" user_choice
if [[ ! "$user_choice" =~ ^[0-9]+$ ]] || [ "$user_choice" -lt 1 ] || [ "$user_choice" -gt "$file_count" ]; then
exit
fi
declare storage_path
read -p "请输入软件存储位置,默认 /var/www/${file_array[${user_choice}]} " storage_path
if [[ -z ${storage_path} ]]; then
storage_path="/var/www/${file_array[${user_choice}]}"
fi
if [[ ! -d "$storage_path" ]]; then
mkdir -p "$storage_path" || {
echo "目录创建失败"
exit 1
}
elif [[ ! -z "$(find "$storage_path" -mindepth 1 -print -quit)" ]]; then
echo "该目录存有文件"
exit
fi
declare random_port=$(($RANDOM % 9000 + 1000))
while ss -tuln | grep $random_port &>/dev/null; do
random_port=$(($RANDOM % 9000 + 1000))
done
declare access_port
read -p "请输入访问端口,默认 $random_port " access_port
if [[ -z $access_port ]]; then
access_port=$random_port
fi
if ss -tuln | grep $access_port &>/dev/null; then
echo "端口已被占用"
exit
fi
bash "${local_directory}/${file_array[user_choice]}.sh" "$storage_path" "$access_port"
echo "${file_array[${user_choice}]} 安装完成,访问端口 ${access_port}"

View File

@ -1,27 +1,9 @@
#!/bin/bash
declare local_path=$1
declare user_choice
declare selected_file
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
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" =~ ^[0-9]+$ ]] || [ "$user_choice" -lt 1 ] || [ "$user_choice" -gt "$file_count" ]; then
exit
fi
# 接收参数
local_path=$1
selected_script=$2
# 脚本路径配置
read -p "请输入脚本存放路径(默认:/var/script" script_path
if [[ -z $script_path ]];then
@ -29,12 +11,14 @@ if [[ -z $script_path ]];then
fi
mkdir -p "$script_path"
# 定时任务配置说明
echo "执行日期"
echo "星号(*):表示匹配任意值"
echo "逗号(,):用于分隔多个值"
echo "斜线(/):用于指定间隔值"
echo "连字符(-):用于指定范围"
# 配置cron表达式
declare cron_expression
declare -a cron_fields=("分钟 059" "小时 023" "天数 131" "月份 112" "星期 07" )
for field in "${cron_fields[@]}";do
@ -48,22 +32,35 @@ for field in "${cron_fields[@]}";do
exit
fi
done
# 验证cron表达式
if [[ "$cron_expression" == '* * * * * ' ]];then
read -p "该脚本会无时无刻执行,请重新输入"
exit
fi
if [[ -f "${local_path}/${file_array[user_choice]}.sh" ]];then
# 检查脚本是否存在
if [[ -f "${script_path}/${selected_script}.sh" ]];then
echo "该路径文件已经存在"
read -p "是否覆盖?(y/n): " overwrite
if [[ ! $overwrite =~ [Yy] ]]; then
echo "已取消操作"
exit
fi
fi
bash "${local_path}/${file_array[user_choice]}.sh" "$script_path"
# 执行原始脚本
bash "${local_path}/${selected_script}.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 -
# 设置脚本权限
chmod +x "${script_path}/${selected_script}.sh" && echo "脚本执行权限添加成功" || echo "脚本执行权限添加失败"
# 配置定时任务
declare cron_job="${cron_expression} ${script_path}/${selected_script}.sh"
(crontab -l 2>/dev/null | grep -v "${selected_script}.sh") | crontab -
(crontab -l 2>/dev/null; echo "$cron_job") | crontab -
# 重启cron服务
systemctl restart cron 2>> /dev/null && echo "自动任务配置成功" || echo "自动任务重启失败"
echo "配置完成"

View File

@ -53,8 +53,9 @@
### 特殊文件作用
* **test.sh**:如果当前目录存在此文件,会在进入目录时自动执行,用于环境依赖检查
* **menu.sh**:如果当前目录存在此文件,会替代默认的遍历行为,提供自定义菜单
* **_init.sh**:目录初始化脚本,在进入目录时自动执行,用于环境依赖检查
* **_menu.sh**:完全替代默认菜单,提供自定义菜单界面
* **_action.sh**:半替代菜单,接收用户选择的脚本名称和当前路径,决定如何执行
### 自定义扩展方法
@ -64,16 +65,13 @@
2. **添加新模块**
* 在 Config 目录下创建新的文件夹
* 可以在文件夹中添加 test.sh 用于依赖检查
* 可以添加 menu.sh 提供自定义的选项界面
* 可以在文件夹中添加 _init.sh 用于依赖检查
* 可以添加 _menu.sh 提供自定义的选项界面
* 可以添加 _action.sh 处理用户选择
3. **修改菜单行为**
* 编辑对应目录下的 menu.sh 文件
* 通过自定义 menu.sh 可以实现更复杂的交互和选项管理
### 例子menu.sh 工作原理
通过观察现有的 menu.sh 文件(如 Docker/Installs/menu.sh 或 Task/Installs/menu.sh
* 编辑对应目录下的 _menu.sh 文件实现完全自定义菜单
* 编辑 _action.sh 实现半自定义菜单,保留默认菜单但自定义执行逻辑
## 📚 功能模块说明

39
run.sh
View File

@ -14,24 +14,32 @@ declare script_name
while true
do
if [[ -e "${local_path}/test.sh" ]]; then
bash "${local_path}/test.sh"
# 1. 目录初始化脚本 _init.sh
if [[ -e "${local_path}/_init.sh" ]]; then
bash "${local_path}/_init.sh"
if [[ $? -eq 1 ]]; then
local_path=$script_path
continue
fi
fi
if [[ -e "${local_path}/menu.sh" ]]; then
# 2. 完全替代菜单 _menu.sh
if [[ -e "${local_path}/_menu.sh" ]]; then
clear
bash "${local_path}/menu.sh" "$local_path"
bash "${local_path}/_menu.sh" "$local_path"
local_path=$script_path
continue
fi
# 默认菜单逻辑
selected_function=0
function_array=()
echo "======$(basename $local_path .sh)======"
for i in "${local_path}"/*
do
script_name=$(awk -F '.' '{print $1}' <<< "$(basename $i)")
if [[ $script_name == "test" ]]; then
# 忽略特殊文件
if [[ $script_name =~ ^_ ]]; then
continue
fi
selected_function=$((selected_function + 1))
@ -46,12 +54,25 @@ fi
read -p "请输入要使用的功能:" user_choice
if [[ "$user_choice" =~ ^[0-9]+$ ]] && [ "$user_choice" -ge 1 ] && [ "$user_choice" -le "${#function_array[*]}" ]; then
clear
if [[ -d "${local_path}/${function_array[$user_choice]}" ]]; then
local_path="${local_path}/${function_array[$user_choice]}"
elif [[ -e "${local_path}/${function_array[$user_choice]}.sh" ]]; then
bash "${local_path}/${function_array[$user_choice]}.sh"
selected_script="${function_array[$user_choice]}"
if [[ -d "${local_path}/${selected_script}" ]]; then
# 进入子目录
local_path="${local_path}/${selected_script}"
elif [[ -e "${local_path}/${selected_script}.sh" ]]; then
# 3. 检查是否存在 _action.sh半替代菜单
if [[ -e "${local_path}/_action.sh" ]]; then
# 将用户选择的脚本名称和当前路径传递给 _action.sh
bash "${local_path}/_action.sh" "$local_path" "${selected_script}"
else
# 直接执行用户选择的脚本
bash "${local_path}/${selected_script}.sh"
fi
local_path=$script_path
fi
if [[ $? -eq 1 ]]; then
echo "${selected_script}可能运行失败了"
fi
else
if [[ $local_path == $script_path ]]; then
exit