Skip to content

Shell 脚本

基本结构

bash
#!/bin/bash
set -euo pipefail

NAME="YC"
echo "Hello, $NAME"

条件判断

bash
if [ -f "/etc/config" ]; then
    echo "配置文件存在"
elif [ -d "/etc/conf.d" ]; then
    echo "配置目录存在"
else
    echo "未找到配置"
fi

循环

bash
# 遍历文件
for file in *.log; do
    gzip "$file"
done

# while 循环
while read -r line; do
    echo "$line"
done < input.txt

函数

bash
log() {
    local level="$1"
    local msg="$2"
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $msg"
}

log "INFO" "服务启动"

基于 VitePress 构建