Skip to content

Instantly share code, notes, and snippets.

View coderofsalvation's full-sized avatar

Coder of Salvation / Leon van Kammen coderofsalvation

View GitHub Profile
@coderofsalvation
coderofsalvation / showanim
Created January 5, 2014 16:33
bash animation function
# 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
@coderofsalvation
coderofsalvation / gist:8270372
Last active January 2, 2016 07:29
rotating animation for bash
# 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
@coderofsalvation
coderofsalvation / email.bash
Last active May 9, 2019 04:03
simple bash email function using sendmail
# 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"
@coderofsalvation
coderofsalvation / password.bash
Last active January 2, 2016 07:29
get password bashfunction
# 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
@coderofsalvation
coderofsalvation / inarray.bash
Last active January 2, 2016 07:29
inarray() and isarray() functions for bash (to check whether value exist)
# 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
@coderofsalvation
coderofsalvation / hasinternet.bash
Last active January 2, 2016 07:29
bash function to check if internet is available
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
@coderofsalvation
coderofsalvation / alphanumeric.bash
Last active September 24, 2019 14:30
remove non-alphanumeric characters in string
# remove non-alphanumeric characters in string
# @param string
alphanumeric()
{
str="$(cat -)"
echo ${str//[^[:alpha:].-]/}
}
@coderofsalvation
coderofsalvation / isempty.bash
Last active January 2, 2016 07:29
isempty() function for bash
# 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
@coderofsalvation
coderofsalvation / bashdown.bash
Last active January 2, 2016 09:08
bashdown template function (extracted from the bashdown github project for bangfu usage)
# 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
@coderofsalvation
coderofsalvation / limitprocess.bash
Created January 8, 2014 15:35
limitprocess is a bashfunction which starts bash cmds but limits to maximum of N running instances (handy for cronjobs)
# 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"
}