Saltar al contenido
Snippets·Gratis

Snippets de Bash

20 snippets de Bash para scripts: cabecera segura, comprobación de argumentos, getopts, bucles sobre archivos y arrays, limpieza con trap, reintentos y unos cuantos comandos de git. Escribe el prefijo en un archivo .sh.

Se instala gratis.

Si tienes PocketCode instalado en tu Android, esto abre la página en la app.

Vista previa

5 de 20

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"

Cambios en la 1.0.0

Primera versión.

Relacionados