refactor: 抽取函数, 统一放入common.sh
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# 安装 Image Builder 构建所需依赖
|
||||
install_dependencies() {
|
||||
sudo apt install -y build-essential libncurses-dev zlib1g-dev gawk git \
|
||||
gettext libssl-dev xsltproc rsync wget unzip python3 qemu-utils mkisofs zstd
|
||||
}
|
||||
source common.sh
|
||||
|
||||
# 下载并解压 x86-64 的 Image Builder
|
||||
# 参数 1: ImmortalWrt 版本号,例如 25.12.1
|
||||
@@ -229,60 +225,6 @@ build(){
|
||||
|
||||
}
|
||||
|
||||
# =============================================================
|
||||
# 交互输入函数
|
||||
# 参数1: 提示信息
|
||||
# 参数2: 默认值
|
||||
# 返回: 用户输入值,直接回车返回默认值
|
||||
# =============================================================
|
||||
ask_input() {
|
||||
local prompt="$1"
|
||||
local default="$2"
|
||||
|
||||
read -p "${prompt} [ 默认: ${default} ]: " value
|
||||
|
||||
if [ -z "$value" ]; then
|
||||
echo "$default"
|
||||
else
|
||||
echo "$value"
|
||||
fi
|
||||
}
|
||||
|
||||
# 获取lan口ip
|
||||
input_lan_ip() {
|
||||
local default_ip="$1"
|
||||
|
||||
while true; do
|
||||
local ip
|
||||
|
||||
ip=$(ask_input "请输入lan口ip" "$default_ip")
|
||||
|
||||
if valid_ipv4 "$ip"; then
|
||||
echo "$ip"
|
||||
return 0
|
||||
else
|
||||
echo "[错误] IP地址格式不正确,请重新输入" >&2
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
valid_ipv4() {
|
||||
local ip="$1"
|
||||
|
||||
# 第一层:检查格式,必须是4段数字
|
||||
echo "$ip" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || return 1
|
||||
|
||||
# 第二层:检查每段范围
|
||||
echo "$ip" | awk -F. '
|
||||
$1>=0 && $1<=255 &&
|
||||
$2>=0 && $2<=255 &&
|
||||
$3>=0 && $3<=255 &&
|
||||
$4>=0 && $4<=255
|
||||
' | grep -q . || return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
# 要构建的 ImmortalWrt 版本, 可以在 https://github.com/immortalwrt/immortalwrt 查询版本号
|
||||
default_version="25.12.1"
|
||||
|
||||
Reference in New Issue
Block a user