refactor: 抽取函数, 统一放入common.sh

This commit is contained in:
Olia Lisa
2026-07-24 11:10:56 +08:00
parent 2b5026d76c
commit a9b7f09e06
5 changed files with 66 additions and 226 deletions
+62
View File
@@ -0,0 +1,62 @@
#!/bin/bash
# 安装 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
}
# =============================================================
# 交互输入函数
# 参数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
}