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
# tar zxf big_file.tar.gz > /dev/null 2>&1 & | |
# printf "%s" "[+] tar is running ..." && _animcui $(pidof tar) | |
showanim() | |
{ | |
[ -z "$1" ] && { printf "%5s\n" ""; return 1; } | |
printf "%s\\n" "$1" | grep -v "[^0-9]" >/dev/null || { printf "%5s\n" ""; return 1; } | |
_animcui_var_pid="$1" | |
_animcui_var_animation_state=1 |
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
# Usage: | |
# printf "%s" "[+] tar" && _animrotate $(pidof tar) | |
# | |
animrotate() | |
{ #shows an animation as long as a process is in memory | |
[ -z "$1" ] && return 1 | |
printf "%s\\n" "$1" | grep -v "[^0-9]" >/dev/null || { printf "%5s\n" ""; return 1; } | |
_animrotate_var_pid=$1 |
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
# sends email using sendmail | |
# usage: echo "my email message" | email "foo@bar.com" "new event happened" "serverX" "noreply@serverx.com" | |
email(){ | |
content="$(cat - )"; email="$1"; subject="$2"; fromname="$3"; from="$4" | |
{ | |
echo "Subject: $subject" | |
echo "From: $fromname <$from>"; | |
echo "To: $email"; | |
echo "$content" |
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
# prompts user to enter password | |
promptpasswd() | |
{ | |
if [ -n "$1" ]; then | |
_getpasswd_var_option=$(printf "%s\\n" "$1" | tr '[:upper:]' '[:lower:]') | |
printf "%s" "Enter your $_getpasswd_var_option password: " | |
stty -echo | |
read ${_getpasswd_var_option}passwd | |
stty echo |
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
# look for a value in an array, returns 0 on success, 1 otherwise | |
# Usage: in_array "$needle" "${haystack[@]}" | |
inarray() | |
{ | |
[ -z "$1" ] && return 1 | |
if [ -n "$BASH" ]; then | |
_inarray_var_needle="$1" | |
shift |
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
has_internet() | |
{ #check for internet connection, returns 0 on success, 1 otherwise | |
wget --tries=3 --timeout=5 http://www.google.com -O /tmp/index.google > /dev/null 2>&1 | |
if [ -s /tmp/index.google ]; then | |
rm /tmp/index.google | |
return 0 | |
else | |
rm /tmp/index.google | |
return 1 |
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
# remove non-alphanumeric characters in string | |
# @param string | |
alphanumeric() | |
{ | |
str="$(cat -)" | |
echo ${str//[^[:alpha:].-]/} | |
} |
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
# detect if arg is an empty file, returns 0 on sucess, 1 otherwise | |
# @param string- string of a file | |
# @dependancy wc | |
is_empty() | |
{ | |
[ -z "$1" ] && return 1 | |
if [ -f "$1" ] && [ X"$(wc -c "$1" | cut -d" " -f1)" = X"0" ]; then | |
return 0 | |
else |
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
# smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.) | |
# @link http://github.com/coderofsalvation/bashdown | |
# @dependancies: sed cat | |
# @example: echo 'hi $NAME it is $(date)' > /tmp/mytemplate.bashdown | |
# @example: cat /tmp/mytemplate.bashdown | bashdown | |
# fetches a document and interprets bashsyntax in string (bashdown) templates | |
# @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ") | |
bashdown(){ | |
IFS=''; cat - | sed 's/\\/\\\\\\\\/g' | while read -r line; do |
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
# Starts bash cmds but limits to maximum of N running instances (handy for cronjobs) | |
# usage: ./limitprocess <max parallel processes> ..yourcommands.. | |
# @dependancies: ps grep wc | |
# @param int maximum number of parallel processes | |
function limitprocess(){ | |
MAXINSTANCES="$1"; shift; | |
processes=$(ps aux | grep "$*" | grep -v grep | wc -l ) | |
((processes < MAXINSTANCES)) && "$@" || echo "already $MAXINSTANCES processes running" | |
} |