-
-
Save jbenninghoff/a5e59bdd9d1af83e05ab to your computer and use it in GitHub Desktop.
Bash Idioms template
This file contains 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
#!/bin/bash | |
#jbenninghoff 2015-Dec-28 vi: set ai et sw=3 tabstop=3 retab: | |
: << '--BLOCK-COMMENT--' | |
Bash idioms template | |
Save as ~/.vim/templates/sh | |
Above requires vim templates plugin: https://github.com/ap/vim-templates | |
Useful site for lots of Bash info: http://wiki.bash-hackers.org/ | |
--BLOCK-COMMENT-- | |
# Check for root | |
[ $(id -u) -ne 0 ] && { echo This script must be run as root; exit 1; } | |
# Make temp file, delete when script ends | |
tmpfile=$(mktemp); trap 'rm $tmpfile' 0 1 2 3 15 | |
#Pause the script | |
read -s -r -p "Press enter to continue or ctrl-c to abort" | |
sep='=====================================================================' | |
# Pad $msg to fixed length using $sep | |
msg="Job Status"; printf "%s %s \n" "$msg" "${sep:${#msg}}" | |
# Handle script arguments | |
verbose=false | |
while getopts ":v" opt; do | |
case $opt in | |
v) verbose=true ;; | |
\?) echo "Invalid option: -$OPTARG" >&2; exit ;; | |
esac | |
done | |
# Assign file contents to shell variable | |
[ -f myIPlist ] && hosts=$(<myIPlist) | |
#Assign 6 args to single variable | |
[ $# -gt 0 ] && hosts="$1 $2 $3 $4 $5 $6" | |
# Various for loops | |
#for i in $hosts; do | |
#for ((i=8; i>0; i--)); do | |
#for i in {1..5}; do | |
for i in $(seq 1 $nproc); do | |
echo $i | |
done | |
# Locate next smallest power of 2 using bitwise AND | |
n=$1 | |
while ((n & (n - 1))); do | |
((n--)) | |
done | |
# Repeat command until string found | |
until (maprcli dump rereplicationinfo | grep 'No active rereplications'); do | |
echo -n 'Still Replicating '; sleep 120 | |
done | |
# Use a shell array variable; declare, assign, append | |
declare -A maclist | |
maclist[10.20.30.44]=00:21:9b:a1:17:63 | |
maclist+=( [10.20.30.40]=00:21:9b:a1:17:62 ) | |
maclist+=( [10.20.30.48]=00:21:9b:9e:ae:15 ) | |
# Loop over contents of shell array | |
for key in ${!maclist[@]}; do | |
echo ${key} ${maclist[${key}]} | |
done | |
echo $[10*1000*1000] | |
# filename=${path##*/} | |
# dirname=${path%/*} | |
# printf "%c" {a..z} $'\n' #Print the alphabet and newline using bash quoting $'...' | |
# IFS=- read -r x y z <<< "str=foo-bar-baz" #read doesn't work with pipe| | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment