Skip to main content

脚本片段

判断命令执行情况

# 检查指定网络是否存在
docker network inspect "$NETWORK_NAME" &>/dev/null

# 如果返回非零状态,表示网络不存在
if [ $? -ne 0 ]; then
echo "网络 $NETWORK_NAME 不存在,正在创建网络..."
docker network create --subnet=173.25.0.0/24 "$NETWORK_NAME"
if [ $? -eq 0 ]; then
echo "网络 $NETWORK_NAME 创建成功"
else
echo "创建网络 $NETWORK_NAME 失败,脚本退出"
exit 1
fi
else
echo "网络 $NETWORK_NAME 已经存在"
fi

判断目录是否存在

check_and_create_dir() {
local dir_path="$1"

if [ ! -d "$dir_path" ]; then
echo "路径 $dir_path 不存在,正在创建..."
mkdir -p "$dir_path"
if [ $? -eq 0 ]; then
echo "路径 $dir_path 创建成功"
else
echo "创建路径 $dir_path 失败,脚本退出"
exit 1
fi
else
echo "路径 $dir_path 已存在"
fi
}
check_and_create_dir "$APP_PATH"

判断文件是否存在

# 检查文件是否存在的函数
check_file_exists() {
local file_path="$1"

if [ ! -f "$file_path" ]; then
echo "文件 $file_path 不存在"
return 1
else
echo "文件 $file_path 已存在"
return 0
fi
}
check_file_exists "$ENV_FILE"

复制文件

# 复制文件的函数
copy_file() {
local src_file="$1"
local dest_file="$2"

if [ -f "$src_file" ]; then
cp "$src_file" "$dest_file"
if [ $? -eq 0 ]; then
echo "文件 $src_file 已成功复制到 $dest_file"
else
echo "复制文件 $src_file$dest_file 失败"
exit 1
fi
else
echo "源文件 $src_file 不存在,无法复制"
exit 1
fi
}
check_file_exists "$ENV_FILE"

删除文件

# 删除文件的函数
delete_file() {
local file_path="$1"

if [ -f "$file_path" ]; then
rm -f "$file_path"
if [ $? -eq 0 ]; then
echo "文件 $file_path 删除成功"
else
echo "删除文件 $file_path 失败"
exit 1
fi
else
echo "文件 $file_path 不存在,无法删除"
fi
}
delete_file "$TEMP_SCRIPT"