跳转到内容
代码片段·免费

Bash 代码片段

20 个用于脚本的 Bash 代码片段:安全的脚本开头、参数检查、getopts、遍历文件和数组、用 trap 清理、重试以及几个 git 命令。在 .sh 文件中输入前缀即可。

免费安装。

如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。

预览

20 个中的 5 个

Safe script headerstrict

Shebang plus set -euo pipefail so errors stop the script

#!/usr/bin/env bash
set -euo pipefail

$0

Check a command existshascmd

Stop with a message when a required program is missing

if ! command -v ${1:git} >/dev/null 2>&1; then
  echo "${1:git} is not installed" >&2
  exit 1
fi

Git: new branch from maingnewbr

Update main and create a branch from it

git switch ${1:main}
git pull --ff-only
git switch -c ${2:feature/name}

Git: undo the last commitgundo

Remove the last commit and keep its changes staged

git reset --soft HEAD~${1:1}

Replace text in many filessedall

Find files that contain a string and replace it in place (GNU sed)

grep -rl "${1:old}" ${2:.} | xargs sed -i "s/${1:old}/${3:new}/g"

1.0.0 的变更

首个版本。

相关