Last active
September 20, 2019 11:28
-
-
Save martin-juul/c8cb8ea0178299f6f7e37069dc59d087 to your computer and use it in GitHub Desktop.
Common bash helpers These snippets are meant for large deployments, so if something is marked as *standard* - do not change those values Before you start I know it tempting to use csh, fish, zsh etc.. - but the fact of the matter is, if youre lu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Battle tested for macOs deploy scripts | |
# disable builtin echo so we can use '-en' | |
enable -n echo | |
# Get the real path to the location of the script | |
shopt -s nullglob | |
DIR=$( | |
cd "$(dirname "$0")" || echo "Couldn't find script source directory" && exit 1 | |
pwd | |
) | |
######### | |
# Interative/Noninteractive scripts | |
######### | |
# Determine if we're interactive or not | |
# is_interactive() { test -t 0; } | |
use_ansi() { test -t 1; } | |
# only output colors if our output is to terminal | |
# i recommend not changing any of these, as they're pretty much standard. | |
# If the colors look weird in our terminal ,change your theme. | |
# *standard* | |
if use_ansi ; then | |
GREEN="\033[0;32m" | |
RED="\033[0;31m" | |
YELLOW="\033[0;33m" | |
BOLD_WHITE="\033[1;37m" | |
CLEAR="\033[0m" | |
else | |
GREEN="" | |
RED="" | |
YELLOW="" | |
BOLD_WHITE="" | |
CLEAR="" | |
fi | |
# ANSI color output helpers | |
green() { echo -en "${GREEN}"$@"${CLEAR}"; } | |
red() { echo -en "${RED}"$@"${CLEAR}"; } | |
yellow() { echo -en "${YELLOW}"$@"${CLEAR}"; } | |
hl() { echo -en "${BOLD_WHITE}"$@"${CLEAR}"; } | |
# output stuff to terminal | |
log() { echo -en "$@"; } | |
logfmt() { printf "$@"; } | |
Extended RegExp matching on arguments | |
# $1 = regex pattern | |
# $@... = remaining args as string to match against | |
matches() { | |
local pat=$1; shift | |
echo "$@" | grep -qEi "$pat" >/dev/null 2>&1 | |
} | |
pass() { | |
local check="✔" | |
green $check | |
} | |
fail() { | |
local x="✘" | |
red $x | |
} | |
# trim leading/trailing whitespace in string | |
trim() { | |
echo "$@" | awk '{ gsub(/^ +| +$/,"") }{ print $0 }' | |
} | |
# repeat character $1 for $2 times | |
repeat() { | |
echo $(printf "%${2}s" |tr " " "$1") | |
} | |
# is_available COMMAND [TEST] | |
# Check if 'command' is available and if passed | |
# execute TEST as well | |
# returns command line status of both | |
is_available() { | |
local cmd=$1; shift | |
local res=0 | |
hash $cmd >/dev/null 2>&1 || { local res=1; } | |
if [ $# -ge 1 ]; then | |
$@ >/dev/null 2>&1 || { local res=2; } | |
fi | |
# echo -n $res | |
return $res | |
} | |
filesize() { | |
local filename=$1; shift | |
echo $(du -k "$filename" | cut -f1) | |
} | |
truncate() { | |
local len=$1; shift; | |
echo "$*" | awk -v len=$len '{ if (length($0) > len) print substr($0, 1, len-3) "..."; else print; }' | |
} | |
escape() { | |
echo "$1" | sed 's/\([\.\$\*]\)/\\\1/g' | |
} | |
escapeSlashes() { | |
echo "$@" | sed 's/\\/\\\\/g' | |
} | |
# Convert UCS-2/UTF-16 with BOM file encoding to UTF-8 w/o BOM | |
# ucs2utf8 DIR FILE_PATTERN | |
# where | |
# - DIR is the root directory to find files for conversion | |
# - FILE_PATTERN is `find` pattern to match file names | |
ucs2utf8() { | |
local count=0 | |
local converted=0 | |
local DIR=$1 | |
local FILE_PAT=$2 | |
# Ensure we can handle filenames w/ spaces in them | |
local oldIFS=$IFS | |
IFS=$(echo -en "\n\b") | |
for file in $(find $DIR -name $FILE_PAT 2>/dev/null); do | |
count=$((count + 1)) | |
# If it has a BOM and first character after is nul (00), convert it | |
if od -x "$file" | head -1 | cut -d' ' -f 2,3 | grep "feff 00" >/dev/null 2>&1; then | |
log "Converting $(hl ${file}) to UTF-8........" | |
converted=$((converted + 1)) | |
local tmp="${file}.tmp" | |
mv "${file}" "${tmp}" | |
iconv -c -f UCS-2 -t UTF-8 "${tmp}" > "${file}" | |
rm "${tmp}" | |
log "$(pass)\n" | |
fi | |
done | |
IFS=$oldIFS | |
echo "$converted/$count" | |
} | |
while IFS= read -r line | |
do | |
if matches "^if\s+" $line; then | |
log $(hl "found: ") $(green $line) "\n" | |
log $(yellow "A warning line...")"\n" | |
log $(red "An error line...")"\n" | |
fi | |
done < t.sh | |
logfmt "%20.20b %20.20b" $(green "test") $(yellow "again") |
Checks if command succeeded (poor mans try...catch)
function didSucceed() {
if [[ $? > 0 ]]
then
echo "The command failed, exiting."
exit 1
else
echo "The command ran successfully, continuing"
fi
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check if directory exists, otherwise created it