Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
#
# generate-github-references
#
[[ ! -d '.git' ]] && { echo 'not in a git directory' >&2; exit 1; }
term="$term"
name="$name"
username="$username"
organization="$organization"
@sgen
sgen / validate-semver
Created August 8, 2020 20:47
Bash Semantic Versioning Validation Regular Expression
#!/bin/bash
version="$1"
[[ -z "$version" ]] && { echo "no version provided" >&2; exit 1; }
semver_regexp='^[vV]?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-([0-9A-Za-z]*)(\.([0-9A-Za-z]*))*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-])*)?$'
[[ ! "$version" =~ $semver_regexp ]] && { echo "$version is not valid" >&2; exit 1; }
@sgen
sgen / recreate
Created November 15, 2018 01:56
Recreate the Gulp.js @babel/register issue
#!/bin/bash
working_dir='gulp-babel-register-namespace-issue-scratchpad';
mkdir -p "$working_dir";
cd "$working_dir";
cat << EOF >> package.json
{
"name": "gulp-babel-register-namespace-issue-scratchpad",
@sgen
sgen / write-heredoc-with-conditional.bash
Created August 28, 2018 19:33
Write a Heredoc with a conditional command afterwards
#!/bin/bash
# Group cat Heredoc with {}
{ cat << EOF > 'test.txt'
Testing 1 2 3
EOF
} || { # { must be on a new line.
# Do something else
}
@sgen
sgen / fork-bomb.bash
Created August 22, 2018 15:40
A fork bomb
#!/bin/bash
:( ){ :|:& }:;
@sgen
sgen / read_password.bash
Created August 17, 2018 21:21
Read a password discreetly in bash
#!/bin/bash
# Read secret string
read_secret() {
# Disable echo.
stty -echo
# Set up trap to ensure echo is enabled before exiting if the script
# is terminated while echo is disabled.
trap 'stty echo' EXIT
@sgen
sgen / find-project-root.bash
Last active August 21, 2018 17:01
Find the project root from a script
#!/bin/bash
# $(cd $(dirname "$0"); pwd -P) finds the scripts path;
#
# /../ Is the path of the script relative to the project root
#
# realpath normalizes the path
export project_root=$(realpath $(cd $(dirname "$0"); pwd -P)/../);
@sgen
sgen / gist:3ee61be3307a62ac9729a7b797ace443
Created August 13, 2018 17:25
Find path of a bash script
#!/bin/bash
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
echo "$SCRIPTPATH";
@sgen
sgen / flags.bash
Created August 9, 2018 18:11
Flags in bash
#!/bin/bash
flag_with_value='';
flag_without_value='';
while [[ "$#" -gt 0 ]];
do
arg="$1";
val="$2";
case "$arg" in
@sgen
sgen / gist:4add8e84836313a5bae23d55cc7aa35f
Created July 27, 2018 19:18
Very basic command line application error logging in go. Use of a properly configured log.Logger or another logging package is preferable.
package main
import (
"errors"
"fmt"
"os"
)
func handle(err error) {
if err == nil {