简化版.bashrc

# ~/.bashrc - 极简版

# 1. 交互式检查 (保留,防止 scp/rsync 等报错)
case $- in
    *i*) ;;
      *) return;;
esac

# 2. 基础别名 (你要求的 ll,以及防止误删的 rm 提示)
alias ll='ls -alF'
# alias rm='rm -i' # 如果需要删除确认可以取消注释

# 3. Docker 快捷别名 (你要求的 d, dc,以及保留之前的 up/down/stop)
alias d='docker'
alias dc='docker compose'
alias docker-compose='docker compose' # 兼容旧习惯
alias down='docker compose down'
alias up='docker compose up -d'
alias start='docker compose restart'
alias stop='docker compose stop'
alias reload='docker exec nginx nginx -s reload'

# 4. 你自定义的实用工具 (mem 和 men)
# 注意:确保 ~/mem.sh 有执行权限 (chmod +x ~/mem.sh)
mem() { MEM_PROG=mem "$HOME/mem.sh" "$@"; }
men() { MEM_PROG=men "$HOME/mem.sh" "$@"; }

# 5. 历史记录优化 (保留默认的,避免终端卡顿)
HISTSIZE=1000
HISTFILESIZE=2000
HISTCONTROL=ignoreboth

# 6. 彩色终端提示符 (直接把之前折腾的代码浓缩成一行)
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

# 7. CUDA 12.6 环境变量
export PATH=/usr/local/cuda-12.6/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.6/lib64:$LD_LIBRARY_PATH
export PATH="$HOME/.local/bin:$PATH"

dsave() {
  if [[ $# -eq 0 ]]; then
    cat <<'EOF'
Usage: dsave <image|image-id> [image|image-id ...]

Export one or more Docker images to gzip-compressed tar files,
named automatically from the image name/tag.

Arguments:
  <image|image-id>   Image name with tag (e.g. nginx:1.31.1-alpine),
                     short/full image ID (e.g. 1847037475ce),
                     or an untagged image name (defaults to :latest).

Behavior:
  - Filename is derived from the first RepoTag of the image,
    replacing ':' and '/' with '_'.
      nginx:1.31.1-alpine  -> nginx_1.31.1-alpine.tar.gz
      reg.io/team/app:v1   -> reg.io_team_app_v1.tar.gz
  - Inputs are de-duplicated by image ID, so passing both
    a name and its short ID exports only once.
  - If 'pv' is installed, transfer progress is shown.
  - On 'docker save' failure, the partial .tar.gz is removed.

Examples:
  dsave nginx:1.31.1-alpine
  dsave 1847037475ce
  dsave nginx:1.31.1-alpine mariadb:11.4 redis:7-alpine
  dsave 1847037475ce 1847037475ce nginx:1.31.1-alpine

EOF
    return 0
  fi

  local -A seen_ids=()
  local img name out repo_tag img_id rc ok=0 fail=0 skip=0

  for img in "$@"; do
    img_id=$(docker inspect --format '{{.Id}}' "$img" 2>/dev/null)

    if [[ -z "$img_id" ]]; then
      echo ">>> ERROR: image not found: ${img}" >&2
      fail=$((fail+1))
      continue
    fi

    if [[ -n "${seen_ids[$img_id]:-}" ]]; then
      echo ">>> skip duplicate: ${img} (same image ID as '${seen_ids[$img_id]}')"
      skip=$((skip+1))
      continue
    fi
    seen_ids[$img_id]="$img"

    repo_tag=$(docker inspect --format '{{if .RepoTags}}{{index .RepoTags 0}}{{end}}' "$img" 2>/dev/null)

    if [[ -n "$repo_tag" && "$repo_tag" != "<none>:<none>" ]]; then
      name="${repo_tag//[:\/]/_}"
      [[ "$img" != "$repo_tag" ]] && echo ">>> resolved ${img} -> ${repo_tag}"
    else
      name="${img//[:\/]/_}"
      echo ">>> no RepoTags for ${img}, using input as filename"
    fi

    out="${name}.tar.gz"
    echo ">>> saving ${img} -> ${out}"

    if command -v pv >/dev/null 2>&1; then
      docker save "$img" | pv | gzip > "$out"
    else
      docker save "$img" | gzip > "$out"
    fi
    rc=${PIPESTATUS[0]}

    if [[ $rc -ne 0 ]]; then
      echo ">>> ERROR: docker save failed for ${img}" >&2
      rm -f "$out"
      fail=$((fail+1))
      continue
    fi

    echo "<<< done: ${out} ($(du -h "$out" | cut -f1))"
    ok=$((ok+1))
  done

  echo "=== summary: ${ok} saved, ${skip} skipped, ${fail} failed ==="
  [[ $fail -eq 0 ]]
}

# 8. 自动加载 bash_completion (自动补全,比如 docker 按 tab)
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi