Snippets·Grátis
Snippets de Bash
20 snippets de Bash para scripts: cabeçalho seguro, checagem de argumentos, getopts, loops sobre arquivos e arrays, limpeza com trap, novas tentativas e alguns comandos git. Digite o prefixo em um arquivo .sh.
Instalação grátis.
Se o PocketCode estiver instalado no seu Android, isto abre a página no app.
Prévia
5 de 20
Safe script headerstrict
Shebang plus set -euo pipefail so errors stop the script
#!/usr/bin/env bash
set -euo pipefail
$0Check 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
fiGit: 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"Mudanças na 1.0.0
Primeira versão.