Skip to content

Instantly share code, notes, and snippets.

@double-z
Last active August 1, 2017 23:59
Show Gist options
  • Save double-z/f8596e280a278ad70f7bee0269dbf079 to your computer and use it in GitHub Desktop.
Save double-z/f8596e280a278ad70f7bee0269dbf079 to your computer and use it in GitHub Desktop.
sub_var_text() {
local variable=$1
read -r -d '' VAR <<-EOT
text also with $variable inside\n
EOT
printf "$VAR"
}
# function change_ifs()
# change the default field seperator
# usage: change_ifs ":"
change_ifs(){
new_ifs=$1
OLDIFS="${IFS}"
export OLDIFS
IFS=$new_ifs
return
}
# function revert_ifs()
# revert the default field seperator
# Automatically reverts to latest change.
# As such use with caution.
# usage: revert_ifs
revert_ifs(){
IFS=$OLDIFS
return
}
# If it is a function name it is then executed with all the values passed with
execute_function() {
local func="$1"
shift
if [[ ${func:-} ]] && declare -F | cut -d' ' -f3 | fgrep -qx -- "${func:-}"; then
func "${@}"
rc=$?
return $rc
else
echo "Not a function ${@}"
fi
}
# cmd: for_each "applies an operation to set of arguments one by one"
# ex. $(for_each 'grep mesos' `ls *.sh`)
for_each() {
op=$1
shift
for arg in "$@"; do
eval "$op" \"$arg\"
done
}
cmd duplicates 'finds duplicate files. To follow symbolic links run duplicate -L $DIR'
duplicates()
{
# Features:
# Fast because checks sizes first
# and filters same linked files by checking inode (%i)
# - Sorts files by size to help you to delete biggest files first
# Troubleshooting:
# on out of memory define TMPDIR
find "$@" -type f -not -regex '.*/\.svn/.*' -printf "%10i\t%10s\t%p\n" \
| sort -n \
| uniq --unique -w10 \
| cut -f 2,3 | sort -n \
| uniq --all-repeated -w 10 \
| cut -f 2 \
| perl -pe "s/\n/\0/g" \
| xargs -0 -i{} sha1sum "{}" | sort \
| uniq --all-repeated=separate -w32 | cut -d ' ' -f 3
}
# cmd: system_status_long "shows long system status and statistics by running various system utilities"
system_status_long() {
grep "" /etc/issue /etc/*release
top -bn1 | head --lines 20
echo
free -m
echo
iostat -m
echo
mpstat
echo
vmstat -S M -s
echo
df --all --human-readable
echo
lsblk
echo
lscpu
echo
lsusb
echo
lspci
echo
lshw -short
}
# cmd: system_status_short "shows short summary of system resources (RAM,CPU) usage"
system_status_short() {
echo "Available RAM: "$((`mem_avail_kb`/1024))"M" \
$((100*`mem_avail_kb`/`head -1</proc/meminfo | sed "s/ \+/ /g" | cut -d' ' -f 2`))"%"
paste <(mpstat|grep %usr|sed "s/ \+/\n/g") <(mpstat|grep all|sed "s/ \+/\n/g") | \grep -e idle -e iowait -e sys
ps-cpu
ps-mem
}
# cmd: trap_err "traps command failures, print reuturn value and returns, better than set -o errexit"
trap_err() {
trap 'echo -e $"\e[2;31mFAIL \e[0;39m ret=$? ${BASH_SOURCE[0]}:${LINENO}" > /dev/stderr;return 2> /dev/null' ERR
}
# cmd: repeat_cmd Repeat n times command.
repeat_cmd() {
local ii max
max=$1; shift;
for ((ii=1; i <= max ; ii++)); do
eval "$@";
done
}
# cmd: ff Find a file with a pattern in name:
ff() { find . -type f -iname '*'$*'*' -ls ; }
# cmd: fe \*.txt cat
# Find a file with pattern $1 in name and Execute $2 on it:
fe() { find . -type f -iname '*'${1:-}'*' -exec ${2:-file} {} \; ; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment