本文目录导读:

这里提供几种常用的环境初始化脚本方案,覆盖不同场景:
通用Shell脚本 (Linux/macOS)
#!/bin/bash
# init_env.sh - 快速初始化开发环境
set -e # 出错时停止执行
echo "=== 开始初始化环境 ==="
# 1. 系统更新
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo ">>> 更新系统包..."
sudo apt-get update && sudo apt-get upgrade -y
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo ">>> 检查Homebrew..."
if ! command -v brew &> /dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
brew update && brew upgrade
fi
# 2. 安装基础工具
install_basic_tools() {
local tools=("git" "curl" "wget" "vim" "htop" "tree")
for tool in "${tools[@]}"; do
if ! command -v "$tool" &> /dev/null; then
echo ">>> 安装 $tool..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt-get install -y "$tool"
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew install "$tool"
fi
fi
done
}
install_basic_tools
# 3. 配置Git
setup_git() {
if [ ! -f ~/.gitconfig ]; then
echo ">>> 配置Git..."
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor vim
fi
}
setup_git
# 4. 创建项目目录结构
create_project_structure() {
echo ">>> 创建目录结构..."
mkdir -p ~/projects/{python,web,scripts,data}
mkdir -p ~/.local/bin
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
}
create_project_structure
echo "=== 环境初始化完成 ==="
Python开发环境初始化
#!/bin/bash
# setup_python_env.sh
echo "=== 设置Python开发环境 ==="
# 安装pyenv (Python版本管理)
if ! command -v pyenv &> /dev/null; then
curl https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
fi
# 安装最新Python
pyenv install 3.11.0
pyenv global 3.11.0
# 安装常用包
pip install --upgrade pip
pip install \
requests \
numpy \
pandas \
flask \
pytest \
black \
flake8 \
ipython \
jupyter
# 设置pip镜像
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
echo "Python环境设置完成!"
Node.js/前端环境初始化
#!/bin/bash
# setup_node_env.sh
echo "=== 设置Node.js环境 ==="
# 安装nvm (Node版本管理)
if [ ! -d "$HOME/.nvm" ]; then
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi
# 安装Node.js LTS版本
nvm install --lts
nvm use --lts
# 配置npm镜像
npm config set registry https://registry.npmmirror.com
# 安装全局工具
npm install -g yarn pnpm typescript eslint prettier nodemon
# 创建Vue/React项目模板
echo ">>> 创建项目模板..."
npx create-vite@latest my-vue-app --template vue
npx create-react-app my-react-app
echo "Node.js环境设置完成!"
Docker环境初始化
#!/bin/bash
# setup_docker_env.sh
echo "=== 设置Docker环境 ==="
# 安装Docker
if ! command -v docker &> /dev/null; then
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew install --cask docker
fi
fi
# 拉取常用镜像
docker pull nginx:alpine
docker pull postgres:15
docker pull python:3.11-slim
docker pull node:18-alpine
# 创建docker-compose配置模板
cat > docker-compose.yml << 'EOF'
version: '3.8'
services:
app:
image: python:3.11-slim
volumes:
- .:/app
working_dir: /app
command: bash -c "pip install -r requirements.txt && python app.py"
EOF
echo "Docker环境设置完成!请重新登录以应用用户组变更。"
快速使用脚本
# 使用方式 chmod +x init_env.sh ./init_env.sh # 或一键下载并执行 (谨慎使用) curl -fsSL https://your-server/init_env.sh | bash # 带参数执行 ./init_env.sh --python --node --docker
建议将脚本保存在~/scripts/目录,并添加到PATH中以便随时使用,根据具体需求修改脚本中的版本号、工具列表和配置。