Skip to content

Instantly share code, notes, and snippets.

View enriched's full-sized avatar

Rich Adams enriched

  • Seattle, WA
View GitHub Profile
@enriched
enriched / awkeval.sh
Created May 2, 2016 19:26
Floating point math in bash without bc (using awk)
#!/bin/bash
# Uses awk to do the comparison so it can handle floating point numbers
awke()
{
echo "$1 $3" | awk '{ print ($1 '"$2"' $2) }'
}
awke '3.124' '*' '6'
@enriched
enriched / foreachdir.sh
Created May 2, 2016 19:25
Bash for each directory in current folder
# you can add this little gem to your .bash_profile to run a command inside of each directory in the current folder
# eg: foreachdir 'git status'
foreachdir() { /usr/bin/find . -type d -mindepth 1 -maxdepth 1 -exec sh -c "(cd {} && echo '\033[0;31m// '\$PWD'------>\033[0m' && $1)" ';' ; }
@enriched
enriched / ngreplocalport.sh
Created May 2, 2016 19:24
ngrep local loopback port .bash_profile function
ngreplocalport(){
sudo ngrep -q -W byline -d lo0 '' "port $1";
}
@enriched
enriched / completebasedonfile.sh
Created May 2, 2016 19:21
bash command completion based on lines in a file
_list_from_file()
{
local curr_arg;
curr_arg=${COMP_WORDS[COMP_CWORD]}
filelines=$(cat /some/file)
COMPREPLY=( $(compgen -W "$filelines" -- $curr_arg) );
}
@enriched
enriched / usefulbashping.sh
Last active May 2, 2016 19:20
Ping with packet loss % and millisec output
usefulbashping()
{
read loss rttavg <<< $(ping -q -c 100 -w 60 -A $1 | \
awk 'BEGIN {FS="[/ ]"}; /loss/ {print substr($6,0,length($6))}; /rtt/ {print $9*1000}')
}