Skip to content

Instantly share code, notes, and snippets.

@malcolmgreaves
Created June 16, 2019 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save malcolmgreaves/83b57bc2cd0291d880f819ee2a1185cb to your computer and use it in GitHub Desktop.
Save malcolmgreaves/83b57bc2cd0291d880f819ee2a1185cb to your computer and use it in GitHub Desktop.
General bash functions
# Exectuable scripts
PROGRAMS="$HOME/programs/py:$HOME/programs/sh"
export PATH="${PATH}:${PROGRAMS}"
# Programs
port-pid() {
PORT="$1"
if [[ -z "$PORT" ]]
then
echo "First argument must be port number"
return 1
fi
echo "Finding programs listening on port: $PORT"
echo "(Requires su to find non-user owned programs)"
echo "--"
sudo lsof -i -n -P | grep $PORT
}
find-ext() {
EXTENSION="$1"
LOCATION="${2:-.}"
find "${LOCATION}" -type f -name "*.${EXTENSION}"
}
sbt-version-project() {
sbt version | tail -n 1 | cut -d' ' -f2-
}
post-json() {
ENDPOINT="$1"
if [[ -z "${ENDPOINT}" ]]; then
echo "First argument must be URL to service!"
return 1
fi
JSON_DATA="$2"
if [[ -z "${JSON_DATA}" ]]; then
echo "Second argument must be JSON data to POST."
return 1
fi
echo "POST to endpoint: $ENDPOINT"
curl \
--request POST \
--header "Content-Type: application/json" \
--data "${JSON_DATA}" \
"${ENDPOINT}"
}
post-json-dev() {
JSON_DATA_FILE="$1"
if [[ -z "${JSON_DATA}_FILE" ]]; then
echo "First argument must be local filepath to JSON data to POST."
return 1
fi
PORT="${2:-8080}"
ENDPOINT="localhost:$PORT"
echo "JSON file: $JSON_DATA_FILE"
post-json $ENDPOINT "\'$(cat $JSON_DATA_FILE)\'"
}
tar-c() {
DIR="$1"
tar -zvcf "${DIR}".tar.gz "${DIR}"
}
tar-d() {
ARCHIVE="$1"
tar -zvxf "${ARCHIVE}"
}
git-head-reset() {
git reset --hard HEAD
}
git-tag-most-recent() {
git describe --abbrev=0 --tags
}
git-tag-delete() {
TAG="$1"
if [[ "master" = "$TAG" ]];
then
echo "Not removing master"
return 1
fi
set -x
git push --delete origin $TAG
git tag --delete $TAG
set +x
}
git-branch-name() {
git branch --list | grep \* | cut -d ' ' -f2
}
process_named() {
PNAME="$1"
ps -ef | head -n 1
ps -ef | grep -v "grep" | grep -i "${PNAME}"
}
alias dockerls='docker images'
alias encrypt='openssl enc -aes-256-cbc -a'
alias decrypt='openssl enc -aes-256-cbc -d -a'
kube_fwd_8080_namespace() {
NAMESPACE="$1"
if [ -z "${NAMESPACE}" ]; then
echo "ERROR: No namespace provided!"
return 1
fi
echo "Port-forwarding 8080 from Kubernetes namespace: ${NAMESPACE}"
kubectl get pods --namespace "${NAMESPACE}" | grep webserver | awk '{ printf "kubectl port-forward --namespace ${NAMESPACE} %s 8080:8080 &", $1 ;}' | source /dev/stdin
}
download() {
local URL="$1"
local FI="$2"
if [[ -z "${FI}" ]];
then
FI="${URL}"
FI="${FI##*/}"
fi
echo "Saving: ${URL}"
echo "As file: ${FI}"
wget --show-progress --continue --output-document="${FI}" "${URL}"
}
trim-whitespace() {
echo "$@" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
git-branch-current() {
trim-whitespace `git branch 2> /dev/null | grep -e "^*" | cut -d' ' -f 2`
}
# Given a directory, adds each unique filepath from within the directory to git-lfs tracking.
track-folder-git-lfs() {
DIR="$1"
for f in `find "${DIR}" -type f`; do
if [[ "$f" = *"README"* ]]; then
echo "Not tracking README file: $f"
else
git lfs track "$f"
fi
done
}
dedupe-and-sort-fi() {
FI="$1"
cat "${FI}" | sort -u "$1"
}
# Outputs the nth line of a file
line() {
N="$1"
if [ -z "$N" ];
then
echo "ERROR: Need to supply line number as first argument (1-based indexing) !!"
return 1
fi
FI="$2"
if [ -z "$FI" ];
then
# no file, assume input is from STDIN
head -n "$N" | tail -n 1
else
head -n "$N" "$FI" | tail -n 1
fi
}
add-line-between() {
FI="$1"
if [ -z "$FI" ];
then
# no file, assume input is from STDIN
OLDIFS="$IFS"
IFS=$'\n'
while read line; do
echo $line
echo ""
done
IFS="$OLDIFS"
else
# process from file
cat $FI | add-line-between
fi
}
add-header-from-to() {
FROM=$1
TO=$2
TEMP=`mktemp`
head -n 1 $FROM > $TEMP
cat $TO >> $TEMP
mv $TEMP $TO
}
split-line-count() {
FI=$1
K=$2
N=`wc -l $FI | cut -f 1 -d" "`
N_MINUS_K=`expr $N - $K`
if [ -n $3 ] ; then
NAME_1=$3-$FI
else
NAME_1="head_${K}-$FI"
fi
if [ -n $4 ]; then
NAME_2=$4-$FI
else
NAME_2="tail_${N_MINUS_K}-$FI"
fi
head -n $K $FI > $NAME_1
tail -n $N_MINUS_K $FI > $NAME_2
}
split-prop() {
FI=$1
PROPORTION=$2
N=`wc -l $FI | cut -f 1 -d" "`
A=`expr $N '*' $PROPORTION`
FIRST_N=`expr $A / 100`
LAST_N=`expr $N - $FIRST_N`
if [ -n $3 ] ; then
NAME_1=$3-$FI
else
NAME_1="head_${K}-$FI"
fi
if [ -n $4 ]; then
NAME_2=$4-$FI
else
NAME_2="tail_${N_MINUS_K}-$FI"
fi
head -n $FIRST_N $FI > $NAME_1
tail -n $LAST_N $FI > $NAME_2
}
no-header-cat() {
FI=$1
HEADER_HEIGHT=1
N=`wc -l $FI | cut -f 1 -d" "`
N_MINUS_1=`expr $N - 1`
tail -n $N_MINUS_1 $FI
}
n-cols() {
SEP="$1"
FI="$2"
python -c "
import sys; c = sys.argv[1]; fi = sys.argv[2].strip();
for l in open(fi):
print len(l.strip().split(c))
break
" $SEP $FI
}
alias tab-n-cols="n-cols \t"
alias comma-n-cols="n-cols ,"
alias space-n-cols='n-cols " "'
concat_spaces() {
# build up arguments into space-sep. string
string=""
for a in "$@"
do
string+="$a "
done
# trim leading and trailing whitespace
echo $string | xargs echo
}
rm_pyc_onward() {
find . -type f -name "*.pyc" | xargs rm -f
}
rm_so_onward() {
find . -type f -name "*.so" | xargs rm -f
}
########################################################################
tt() {
rm_pyc_onward
nosetests -s
}
pt() {
rm_pyc_onward
pytest
}
ct() {
rm_pyc_onward
rm_so_onward
rm -rf build/
python setup.py build_ext --inplace
}
pb() {
ARGS="$@"
rm_pyc_onward
rm_so_onward
rm -rf build/
python setup.py build $ARGS
}
pt() {
ARGS="$@"
rm_pyc_onward
python setup.py test $ARGS
}
pi() {
ARGS="$Q"
rm_pyc_onward
rm_so_onward
python setup.py install $ARGS
}
pc() {
ARGS="$@"
if [ -z "$ARGS" ]
then
# no arguments supplied: assume to run on current directory
ARGS='.'
fi
rm_pyc_onward
coverage run --source $ARGS -m py.test
coverage report
coverage html
}
pd() {
ARGS="$@"
rm_pyc_onward
rm_so_onward
python setup.py develop
}
########################################################################
calc() {
local IFS=' '
local c="${*//p/+}"
c="${c//x/*}"
bc -l <<<"scale=10;$c"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment