Fork me on GitHub

Bashlive function reference

Bashlive features a basic repository of functions which can be used in your bash apps. We like to encourage you to leave feedback, or by just forking the repo on github here, add your snippets, and do a pull request push updates.

Last updated: Mon Jun 23 13:23:36 CEST 2014

                                                                                        
      
    

Date


rss()

returns a rss date format

                                                                                        
        # returns a rss date format
# usage: echo "$(rssdate)"
 
rssdate(){
   date -R
}
      
/ /bash/function/date/rss  # copy/paste this line into your terminal


timestamp()

returns the current unix timestamp

                                                                                        
        # returns the current unix timestamp
# usage: echo "$(timestamp)"

timestamp(){
  date +%s
}
      
/ /bash/function/date/timestamp  # copy/paste this line into your terminal


Debug


stacktrace()

print a stacktrace with a msg, exits with 1

                                                                                        
        # print a stacktrace with a msg, exits with 1

function stacktrace()
{  
    if [ -n "$BASH" ]; then
        _die_var_frame=0
        while caller $_die_var_frame; do
            _die_var_frame=$(expr $_die_var_frame + 1);
        done
    fi

    printf "%s\\n" "$*"
    exit
}
      
/ /bash/function/debug/stacktrace  # copy/paste this line into your terminal


File


csvget()

easily extract field from a csv file

                                                                                        
        # extracts fields from a csv like: "234","foo bar","foo"
# usage: cat my.csv | csvget 2     (outputs: "foo bar")
# @dependency: gawk

csvget(){
  field="$1"; ((field+=3));
  cat - | gawk -vFS='^"|","|"$' '{ print $'$field' }'
}
      
/ /bash/function/file/csvget  # copy/paste this line into your terminal


is_empty()

checks if file is empty

                                                                                        
        # 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
        return 1
    fi
}
      
/ /bash/function/file/is_empty  # copy/paste this line into your terminal


is_newer()

compare the dates of two files, return true (0) if file1 has been modified before file2

                                                                                        
        # compare the dates of two files, return true (0) if file1 has
# been modified more recently otherwise 1
# @param string file1
# @param string file2

function is_newer()
{   
    [ $# -ne 2 ] && return 1
    if [ ! -f $1 ] || [ ! -f $2 ]; then
        return 1       # No
    fi
    if [ -n "$(find $1 -newer $2 -print)" ]; then
        return 0       # Yes
    else
        return 1       # No
    fi
}
      
/ /bash/function/file/is_newer  # copy/paste this line into your terminal


General


eventhandler()

simple event & exception handling for bash

                                                                                        
        # simple event / exception handling in bash
#
# Usage:
#
#   onFoo(){
#     echo "onFoo() called width arg $1!"
#   }
#
#   onExit(){
#     echo "onExit called!"
#   }
#
#   foo(){
#     throw EVENT_FOO_OCCURED xyz
#   }
#
#   addListener EVENT_FOO_OCCURED onFoo
#   addListener EXIT onExit
#   foo
#
# OUTPUT:
#
#   onFoo() called width arg xyz!
#   onExit called!
#

declare -A LISTENERS

throw(){
  EVENT=$1; shift; for listener in "${LISTENERS[$EVENT]}"; do eval "$listener $@"; done
}

addListener(){
  if ! test "${LISTENERS['$1']+isset}"; then LISTENERS["$1"]=""; fi
  LISTENERS["$1"]+="$2 " # we can get away with this since functionnames never contain spaces
}

# convert exitcodes to events
trap "throw EXIT"    EXIT
trap "throw SIGINT"  SIGINT
trap "throw SIGTERM" SIGTERM
      
/ /bash/function/general/eventhandler  # copy/paste this line into your terminal


in_array()

check if argument exist as value in given array

                                                                                        
        # 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
        for element
        do
            [ X"$element" != X"$_inarray_var_needle" ] || return 0
        done
    fi
    return 1
}
      
/ /bash/function/general/in_array  # copy/paste this line into your terminal


is_array()

check if variable is array, returns 0 on success, 1 otherwise

                                                                                        
        # check if variable is array, returns 0 on success, 1 otherwise
# @param mixed 

is_array()
{   #detect if arg is an array, returns 0 on sucess, 1 otherwise
    [ -z "$1" ] && return 1
    if [ -n "$BASH" ]; then
        declare -p ${1} 2> /dev/null | grep 'declare \-a' >/dev/null && return 0
    fi
    return 1
}
      
/ /bash/function/general/is_array  # copy/paste this line into your terminal


is_dir()

checks if given parameter is a directory

                                                                                        
        # checks if given parameter is a directory
# @param string input
# usage: is_dir /my/path || { echo "directory does not exist"; exit 1; }

is_dir(){
 [[ -d "$1" ]] && return 0 || return 1;
}
      
/ /bash/function/general/is_dir  # copy/paste this line into your terminal


is_empty()

checks whether variable is empty

                                                                                        
        # 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
        return 1
    fi
}
      
/ /bash/function/general/is_empty  # copy/paste this line into your terminal


is_file()

checks if given parameter is a file

                                                                                        
        # checks if given parameter is a file
# @param string filename
# usage: is_file my/path/foo.txt || echo "not a file"

is_file(){
  [[ -f "$1" ]] && return 0 || return 1
}
      
/ /bash/function/general/is_file  # copy/paste this line into your terminal


is_float()

determines if variable is float

                                                                                        
        # determines if variable is float
# @param string input
# usage: is_float "12.0" && echo "yes"

is_float(){
  echo "$1" | grep \\. | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' &>/dev/null && return 0 || return 1
}
      
/ /bash/function/general/is_float  # copy/paste this line into your terminal


is_function()

This function checks if an defined function or command exist.

                                                                                        
        # This function checks if an defined function or command exist.
#
#  @param string $1 function name (required)
#  @return bool
#  @example:    is_function "rc_myfunc" || exit 1
 
is_function() {
  type "$1" &>/dev/null && return 0 || return 1
} 
      
/ /bash/function/general/is_function  # copy/paste this line into your terminal


is_integer()

determines if parameter is an int

                                                                                        
        # determines if parameter is an int
# @param string input

is_integer(){
  number=$( echo "$1" | sed 's/^[-+]*[0-9]*//g' ); [[ -z "$number" ]] && return 0 || return 1
}
      
/ /bash/function/general/is_integer  # copy/paste this line into your terminal


is_pipeline()

determines whether receiving from pipeline (echo foo | mycommand) or from terminal/tty ( mycommand foo )

                                                                                        
        # determines whether receiving from pipeline (return 0) or from terminal/tty
# usage: is_pipeline && cat -

is_pipeline(){
  [[ -t 0 ]] && return 1 || return 0 
}
      
/ /bash/function/general/is_pipeline  # copy/paste this line into your terminal


is_url()

checks if argument is an url

                                                                                        
        # checks if argument is an url
# usage: is_url "http://foo.com" && echo "yes im an url"
# @param string

is_url(){
  [[ "$1" =~ ^http ]] && return 0 || return 1
}
      
/ /bash/function/general/is_url  # copy/paste this line into your terminal


is_writable()

checks if given file/path is writable

                                                                                        
        # checks if given file/path is writable
# @param string filename
# usage: is_writable my/path/foo.txt || echo "not writable"
 
is_writable(){
  [[ -w "$1" ]] && return 0 || return 1
}
      
/ /bash/function/general/is_writable  # copy/paste this line into your terminal


Input


getopts.tpl()

getopts-snippet to easily pass commandline option/arguments to you shellscript

                                                                                        
        # snippet for getopts to easily support options and arguments in your shellscript
# usage: copy/paste it and run ./yourcommand -h foofoo -n barbar one two

# initialize variables
OPTION1="foo"
OPTION2="bar"
OPTIONS=0
# get rid of appname and (parse) options
shift 
while getopts ":h:n:" opt
do
    case ${opt} in
        h) OPTION1=${OPTARG};((OPTIONS+=2));;  
        n) OPTION2=${OPTARG};((OPTIONS+=2));;    
    esac
done
for((i=0;i<OPTIONS;i++)); do shift; done
# initialize arguments
ARG1="$1"
ARG2="$2"

echo "-h=$OPTION1 -n=$OPTION2 ARG1=$ARG1 ARG2=$ARG2"
      
/ /bash/function/input/getopts.tpl  # copy/paste this line into your terminal


is_interactive()

checks whether bash/script is executed by a human (and not a pipe, or scp e.g.)

                                                                                        
        # checks whether bash/script is executed by a human (and not a pipe, or scp e.g.)
# usage: is_interactive && echo "hello human" && read -p line "please enter your name> "
is_interactive(){
  [[ $- =~ "i" ]] && [[ -t 0 ]] && return 0
  return 1
}
      
/ /bash/function/input/is_interactive  # copy/paste this line into your terminal


promptpassword()

prompts user to type password (discrete)

                                                                                        
        # 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
        printf "\\n"
    else
        printf "%s" "Enter your password: "
        stty -echo
        read passwd
        stty echo
        printf "\\n"
    fi
}
      
/ /bash/function/input/promptpassword  # copy/paste this line into your terminal


subconsole()

starts a simple subconsole shell with arrowkey (history) support

                                                                                        
        # starts a simple subconsole shell with arrowkey (history) support
# @param string functionname of inputhandler 
# @param string consolename
# usage:  process(){ echo "$1 was typed!"; }
#         subconsole process myconsole
subconsole(){
  echo -e "type 'exit' to quit\n"
  while IFS="" read -r -e -d $'\n' -p "$2> " line; do
    "$1" "$line"
    history -s "$line"
  done
}
      
/ /bash/function/input/subconsole  # copy/paste this line into your terminal


Math


calculate()

simple calculation function for integer *and* floating point math

                                                                                        
        # simple calculation function for integer *and* floating point math
#
# @param string to be calculated
# @param precision (default = 0)
# usage: calculate "(1+1)/2/10"     <- outputs 0 
#        calculate "(1+1)/2/10" 10  <- outputs 0.1
#        echo $(calculate "1+1")    <- outputs 2

calculate(){
  if [[ -n "$2" ]]; then
    which bc &>/dev/null && { echo "scale=$2;$1" | bc; } || echo "bc is not installed"; 
  else echo $(($1)); fi
}
      
/ /bash/function/math/calculate  # copy/paste this line into your terminal


float2integer()

removes decimals from number(strings), usage: echo 1.2 | float2integer (output: 1)

                                                                                        
        # removes decimals from number(strings)
# usage: echo $(float2integer 1.02)  (output: 1)
# @param integer (piped)

float2integer(){
  printf "${1/\.*/}"
}
      
/ /bash/function/math/float2integer  # copy/paste this line into your terminal


Network


args2http()

converts commandline arguments 'one --flag "123 4"' e.g. to a http get-string '?1=one&flag=123%204'

                                                                                        
        # converts commandline arguments to a http get-string
# usage: args2http one --flag 123 two --somekey somevalue  <-- outputs: ?1=one&flag=123&2=two&somekey=somevalue

/ /bash/function/string/urlencode!

args2http(){
  httpstr="?"; pos=1; lastarg=""
  for arg in "$@"; do
    if [[ "${arg:0:1}" == "-" ]]; then
      key="${arg//-/}"; httpstr="$httpstr&$key="; lastarg="option"
    else
      [[ "$lastarg" == "option" ]] && httpstr="$httpstr$(urlencode "$arg")" || {
        httpstr="$httpstr&$pos=$(urlencode "$arg")"; ((pos++));
      };
      lastarg="positional"
    fi
  done
  echo "${httpstr/?&/?}"
}
      
/ /bash/function/network/args2http  # copy/paste this line into your terminal


debug_url()

shows statistics for an url like requesttime e.g.

                                                                                        
        # shows statistics for an url like requesttime e.g.
# usage: debug_http "http://foo.com/bar" 
# @dependancy: curl

debug_url () {
    curl -s $@ -o /dev/null \
    -w "http_code %{http_code} time_appconnect %{time_appconnect} time_namelookup %{time_namelookup} time_connect %{time_connect} time_pretransfer %{time_pretransfer} time_starttransfer %{time_starttransfer} time_total %{time_total} speed_download %{speed_download}\n"
}
      
/ /bash/function/network/debug_url  # copy/paste this line into your terminal


email()

sends email (using sendemail)

                                                                                        
        # 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"; from="$3"; fromname="$4"
  {
    echo "Subject: $subject"
    echo "From: $fromname <$from>";
    echo "To: $email";
    echo "$content"
  } | $(which sendmail) -F "$from" "$email"
}
      
/ /bash/function/network/email  # copy/paste this line into your terminal


get_url_and_extract_links()

retrieves given urls and extracts links and prints them line by line

                                                                                        
        # retrieves given urls and extracts links and prints them line by line
# usage: curl "2webapp.com/bashlive/" | htmldompath | htmlgreplinks
# @dependency sed awk curl

/ /bash/function/string/htmldompath!
/ /bash/function/string/htmlgreplinks!

get_url_and_extract_links(){
  curl -s "$1" | htmldompath 2>/dev/null | htmlgreplinks 2>/dev/null 
}
      
/ /bash/function/network/get_url_and_extract_links  # copy/paste this line into your terminal


has_internet()

checks whether bash has access to internet using ping

                                                                                        
        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
    fi
}
      
/ /bash/function/network/has_internet  # copy/paste this line into your terminal


iptools()

ip functions (check whether ip is in given range e.g.)

                                                                                        
        # ip functions (check whether ip is in given range e.g.)
# collected from https://github.com/caquino/bash-functions/blob/master/network-lib


iscidr () {
    [[ ${1} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/?[0-9]{1,2}?$ ]] && echo $1
}

inet_ntoa () {
    [[ ${1} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || return 255
    IFS=".";set - ${1};unset IFS
    local OCT1 OCT2 OCT3 OCT4
    OCT1=$(($1<<24));OCT2=$(($2<<16));OCT3=$(($3<<8));OCT4=$4
    echo $(($OCT1+$OCT2+$OCT3+$OCT4))
}

inet_aton () {
    [[ ${1} =~ ^[0-9]+$ ]] || return 255
    ADDRESS=${1}
    OCT4=$((${ADDRESS} % 256));ADDRESS=$((${ADDRESS}-${OCT4}));ADDRESS=$((${ADDRESS}/256))
    OCT3=$((${ADDRESS} % 256));ADDRESS=$((${ADDRESS}-${OCT3}));ADDRESS=$((${ADDRESS}/256))
    OCT2=$((${ADDRESS} % 256));ADDRESS=$((${ADDRESS}-${OCT2}));ADDRESS=$((${ADDRESS}/256))
    OCT1=$((${ADDRESS} % 256))
    echo "${OCT1}.${OCT2}.${OCT3}.${OCT4}"
}

isipinrange () {
    [[ ${1} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || return 255
    [[ ${2} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/?[0-9]{1,2}?$ ]] || return 255
    NETWORK=$(inet_ntoa ${2%%/*})
    BITMASK=${2##*/}
    BITMASK=$((~((1<<(32 - $BITMASK))-1)))
    IPDEC=$(inet_ntoa ${1})
    IPNET=$((${IPDEC}&${BITMASK}))
    [[ ${IPNET} == ${NETWORK} ]] && return 0
    return 1
}
      
/ /bash/function/network/iptools  # copy/paste this line into your terminal


listenport()

simple server listening on port using nc (netcat)

                                                                                        
        # simple server listening on port
# for extra (http) functions see: https://github.com/coderofsalvation/bashwapp/blob/master/app
# @dependancy nc
# usage: POST=8000 listenport
#        telnet localhost 8000

[[ ! -n $PORT ]] && PORT=8000 # allow port to be passed on commandline

listenport(){
  TMPFILE="/tmp/.myapp.$(whoami)"; CLIENT="$TMPFILE.fifo.$PORT.$(whoami)"
  [[ ! -p $CLIENT ]] && mkfifo $CLIENT 
  while [[ -p $CLIENT ]]; do cat $CLIENT | nc -v -l $PORT 2>&1 | onRequest "$CLIENT"; done
  rm $TMPFILE.*
}

onRequest(){
  request="$(cat - )"
  echo "OK\r\n" > "$1" 
  echo "incoming = $request"
}
      
/ /bash/function/network/listenport  # copy/paste this line into your terminal


Process


animrotate()

shows fancy rotating anim while process is not finished

                                                                                        
        # 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
    _animrotate_var_animation_state=1

    if [ ! "$(ps -p $_animrotate_var_pid -o comm=)" ]; then
        printf "%5s\n" ""
        return 1
    fi

    printf "%s" "    "

    while [ "$(ps -p $_animrotate_var_pid -o comm=)" ]; do
        # rotating star
        printf "%b" "\b\b\b"
        case $_animrotate_var_animation_state in
            1)
                printf "%s" "["
                printf "%b" "\033[1m|\033[0m"
                printf "%s" "]"
                _animrotate_var_animation_state=2
                ;;
            2)
                printf "%s" "["
                printf "%b" "\033[1m/\033[0m"
                printf "%s" "]"
                _animrotate_var_animation_state=3
                ;;
            3)
                printf "%s" "["
                printf "%b" "\033[1m-\033[0m"
                printf "%s" "]"
                _animrotate_var_animation_state=4
                ;;
            4)
                printf "%s" "["
                printf "%b" "\033[1m"
                printf "%s" "\\"
                printf "%b" "\033[0m"
                printf "%s" "]"
                _animrotate_var_animation_state=1
                ;;
        esac
        sleep 0.2
    done
    printf "%b" "\b\b\b\b\b\b\b       "
}
      
/ /bash/function/process/animrotate  # copy/paste this line into your terminal


animwiggle()

shows fancy wiggling animation while process is not finished

                                                                                        
        # 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

    if [ ! "$(ps -p $_animcui_var_pid -o comm=)" ]; then
        printf "%5s\n" ""
        return 1
    fi

    printf "%s" "      "

    while [ "$(ps -p $_animcui_var_pid -o comm=)" ]; do
        printf "%b" "\b\b\b\b\b"
        case $_animcui_var_animation_state in
            1)
                printf "%s" '\o@o\'
                _animcui_var_animation_state=2
                ;;
            2)
                printf "%s" '|o@o|'
                _animcui_var_animation_state=3
                ;;
            3)
                printf "%s" '/o@o/'
                _animcui_var_animation_state=4
                ;;
            4)
                printf "%s" '|o@o|'
                _animcui_var_animation_state=1
                ;;
        esac
        sleep 1
    done
    printf "%b" "\b\b\b\b\b" && printf "%5s\n" ""
}
      
/ /bash/function/process/animwiggle  # copy/paste this line into your terminal


findandkill()

find and kill by brett terpstra (interactive, preventing repetitive ps aux + kill -9 )

                                                                                        
        # find and kill by brett terpstra (saving repetitive ps aux + kill -9 )
fp () { #find and list processes matching a case-insensitive partial-match string
  ps Ao pid,comm|awk '{match($0,/[^\/]+$/); print substr($0,RSTART,RLENGTH)": "$1}'|grep -i $1|grep -v grep
}

fk () { # build menu to kill process
  IFS=$'\n'
  PS3='Kill which process? '
  select OPT in $(fp $1) "Cancel"; do
    if [ $OPT != "Cancel" ]; then
      kill $(echo $OPT|awk '{print $NF}')
    fi
  break
  done
  unset IFS
}
      
/ /bash/function/process/findandkill  # copy/paste this line into your terminal


limit()

limits shellscript to X simultanious processes

                                                                                        
        # 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"
}
      
/ /bash/function/process/limit  # copy/paste this line into your terminal


single()

limits script to one process (singleton)

                                                                                        
        # forces/limits shellscript to only one 1 process, to prevent running twice, aka bash singleton
# this function exits when no parameter is passed, and another instance of the script is detected
# @dependancies ps grep tr sed
# @param string (pass --kill to kill previous process)
# usage: change 'foo' and 'bar' to the strings which occur in your process name

function singleprocess(){
  PID=$$;                            # current process
  if [[ "$1" == "--kill" ]]; then 
    PID="$(ps aux | grep foo | grep bar | tr -s [:space:] | cut -d' ' -f2 )"
    [[ $? == 0 ]] && [[ ${#PID} != 0 ]] && echo "killing previous pid $PID" && kill -9 $PID 
  else
    ps aux | grep "$(basename $0)" | grep -v grep | grep -v $PID &>/dev/null && exit 0; # dont run twice
  fi
}
      
/ /bash/function/process/single  # copy/paste this line into your terminal


throttle()

throttle calls to a certain commands to prevent flooding (handy for notifications/email/chatmessages etc)

                                                                                        
        #!/bin/bash 
# Usage: ./throttle sendtoIRC "foo bar crashed"
shift
BASENAME="$(basename "$0")"
CACHEFILE="/tmp/$(whoami).$BASENAME.cache"
TIME=$(date +%H%M)  # lets restrict to one call per minute

callAllowed(){
  if [[ ! -f "$CACHEFILE" ]]; then
    echo "$TIME" > "$CACHEFILE"
  else
    lasttime=$(cat "$CACHEFILE")
    [[ $lasttime != $TIME ]] && echo "$TIME" > "$CACHEFILE" && return 0
    [[ $lasttime != $TIME ]] || return 1
  fi
}

if callAllowed; then
  "$@"
else
  echo "too many calls, please try again later"
fi
      
/ /bash/function/process/throttle  # copy/paste this line into your terminal


String


alphanumeric()

checks if string contains only alphanumeric chars

                                                                                        
        # remove non-alphanumeric characters in string
# @param string
alphanumeric()
{ 
  str="$(cat -)"
  echo ${str//[^[:alpha:].-]/}
}
      
/ /bash/function/string/alphanumeric  # copy/paste this line into your terminal


base64()

base64 encoding/decoding of strings

                                                                                        
        encode64()
{
    [ -z "$1" ] && return 1
    printf "%s\\n" "$1" | openssl enc -base64
}

decode64()
{
    [ -z "$1" ] && return 1
    printf "%s\\n" "$1" | openssl enc -base64 -d
}
      
/ /bash/function/string/base64  # copy/paste this line into your terminal


color()

terminal color escape codes for fancy colourfull output

                                                                                        
        # bash colors snippet (Usage: printf "$C_GREEN this is green $C_NORMAL this is normal")
C_NORMAL="\\033[0;0m"
C_BOLD="\E[1;37;40m"
C_BRIGHT="\E[1;33;40m"
C_BLACK="\\033[0;30m"
C_RED="\\033[0;31m"
C_GREEN="\\033[0;32m"
C_BROWN="\\033[0;33m"
C_BLUE="\\033[0;34m"
C_PURPLE="\\033[0;35m"
C_CYAN="\\033[0;36m"
C_LIGHT_GREY="\\033[0;37m"
C_DARK_GREY="\\033[1;30m"
C_LIGHT_RED="\\033[1;31m"
C_LIGHT_GREEN="\\033[1;32m"
C_YELLOW="\\033[1;33m"
C_LIGHT_BLUE="\\033[1;34m"
C_LIGHT_PURPLE="\\033[1;35m"
C_LIGHT_CYAN="\\033[1;36m"
C_WHITE="\\033[1;37m"
C_DEFAULT="\\033[0m"
      
/ /bash/function/string/color  # copy/paste this line into your terminal


explode()

explodes string to array based on given delimiter

                                                                                        
        # explodes string to array based on given delimiter
# @param string delimitor
# @param string varname
# @param string input
# usage: explode "," myarray "foo,bar"
#        echo ${myarray[@]}

explode(){
  IFS="$1" read -a $2 <<< "$3"
}
      
/ /bash/function/string/explode  # copy/paste this line into your terminal


getline()

gets the nth line ( e.g. cat file.txt | getline 4 )

                                                                                        
        # gets the nth line ( e.g. cat file.txt | getline 4 )
# @param int linenumber

getline(){
  cat - | sed -n "$1p"
}
      
/ /bash/function/string/getline  # copy/paste this line into your terminal


highlight()

highlights keywords in bashcode string (usage: echo "foo bar" | highlight "foo")

                                                                                        
        # highlights keywords in bashcode script
# usage: echo "foo normal bar" | highlight "foo|bar"

highlight(){
  cat - | grep -E --color "$1"
}
      
/ /bash/function/string/highlight  # copy/paste this line into your terminal


htmldompath()

adds htmldompaths to htmlcontent for easier line-by-line parsing ( fingers crossed! :D )

                                                                                        
        # adds htmldompaths to htmlcontent ( fingers crossed! :D ) 
# @param htmlstring (pipe)
# usage: curl "http://foo.com/bar.html" | htmldompath
# output: body:                                   <body>
#         body>a:                                 <a href="#foo">
#         body>a>h1:                              <h1>Some title</h1>

htmldompath(){
  allowedtags="$1"; TMPFILE="/tmp/.htmldompath.$(whoami)"; path=();
  # strip enters, put each tag on a line, remove whitespaces, and store into file
  cat - | sed ':a;N;$!ba;s/\n/ /g' | sed 's/</\n&/g;s/  / /g' | sed '/^\s*$/d' > $TMPFILE;
  while read line; do
    [[ "$line" =~ "<body"  ]] && bodyfound=1
    [[ "$line" =~ "</body" ]] && break;                      # we are done
    [[ ! -n $bodyfound     ]] && continue;                   # only care for data after <body>-tag
    tag="${line/</}"; tag="${tag/>*/}"; tag="${tag// */}"; tag="$(echo "$tag" | tr '[:upper:]' '[:lower:]' )"
    [[ "${tag:0:1}" == '!'  ]] && continue                   # skip nondom tags
    [[ "${tag:0:2}" == 'br' ]] && continue                   # skip noninteresting tags
    lasttag=${path[${#path[@]} - 1]}
    if [[ "${tag:0:1}" == '/' ]]; then 
      [[ "${tag/*\//}" == "$lasttag" ]] && unset path[${#path[@]}-1] # && echo "-pathsize=${#path[@]}"
    else 
      [[ ! "$line" =~ "/$tag" ]] && [[ ! "$line" =~ '/>' ]] && path+=("${tag/\//}") # && echo "+pathsize=${#path[@]}"
    fi
    pathstring="${path[*]}"; pathstring="${pathstring// />}"; printf "%-40s %s\n" "$pathstring:" "$line"
  done < $TMPFILE
  rm $TMPFILE
}
      
/ /bash/function/string/htmldompath  # copy/paste this line into your terminal


htmlentities()

converts special htmlcharacters into htmlentities

                                                                                        
        # converts special htmlcharacters into htmlentities
# usage: cat file.txt | htmlentities

htmlentities(){
  cat - | sed -e 's/&/\&amp;/g'	\
      -e 's/</\&lt;/g'	\
      -e 's/>/\&gt;/g'	\
      -e 's/ä/\&auml;/g'	\
      -e 's/ö/\&ouml;/g'	\
      -e 's/ü/\&uuml;/g'	\
      -e 's/ß/\&szlig;/g'	\
      -e 's/Ä/\&Auml;/g'	\
      -e 's/Ö/\&Ouml;/g'	\
      -e 's/Ü/\&Uuml;/g'	
}
      
/ /bash/function/string/htmlentities  # copy/paste this line into your terminal


htmlgreplinks()

tries to extract links from htmlcontent (hint: use with htmldompath)

                                                                                        
        # tries to extract links from htmlcontent (hint: use with htmldompath)
# @param htmlinput (piped)
# usage: curl "http://foo.com" | htmlgreplinks | sort | uniq

htmlgreplinks(){
  cat - | grep -E "(href|src|action)=[\"']*[\"']" | \
  sed "s/.*href=['\"]//g"   | \
  sed "s/.*action=['\"]//g" | \
  sed "s/.*src=['\"]//g"   | \
  sed "s/['\"].*//g"        | while read line; do [[ ! "$line" == "#" ]] && echo "$line"; done
}
      
/ /bash/function/string/htmlgreplinks  # copy/paste this line into your terminal


implode()

collapses arguments / list to single string

                                                                                        
        # collapses arguments / list to single string
# @param array/arguments
# usage: implode ${myarray[@]}
#        implode one two three

function implode(){
  echo "$*"
}
      
/ /bash/function/string/implode  # copy/paste this line into your terminal


lines_to_csv()

merges x number of lines to one commaseperated line`

                                                                                        
        # merges x number of lines to one commaseperated line
# usage: echo -e "jan\ncold\nrain\njune\nhot\dry\n" | lines_to_csv ',' 'a,b,c'
#        will output:
#                      "jan","cold","rain"
#                      "june","hot","dry"

lines_to_csv(){
  lines="$(cat -)"; delimiter="$1"; columns="$2";
  outputline=""
  ncolumns="$( echo "$columns" | grep -o "$delimiter" | wc -l)";
  i=0; echo "$lines" | while read line; do 
    [[ "${#outputline}" > 0 ]] && outputline="$outputline""$delimiter\"$line\"" || outputline="\"$line\""
    ((i++)); (( i > ncolumns )) && echo "$outputline" && i=0 && outputline=""
  done
}
      
/ /bash/function/string/lines_to_csv  # copy/paste this line into your terminal


lowercase()

converts uppercase string to lowercase ('FOO' to 'foo')

                                                                                        
        # converts uppercase string to lowercase ('FOO' to 'foo')
# example: echo "foo" | lowercase    (output: foo)
 
lowercase(){
  cat - | tr '[:upper:]' '[:lower:]'
} 
      
/ /bash/function/string/lowercase  # copy/paste this line into your terminal


multigrep()

greps on keywords (keyword1 AND keyword2 AND ..)

                                                                                        
        # greps on keywords (keyword1 AND keyword2 AND ..)
# @param string keyword
# usage: printf "foo bar\nbar foo\n" | multimatch foo bar

multigrep() { 
  awk 'BEGIN{for(i=1;i<ARGC;i++) a[i]=ARGV[i]; ARGC=1} {for (i in a) if ($0 !~ a[i]) next; print}' "$@"
}
      
/ /bash/function/string/multigrep  # copy/paste this line into your terminal


removedoublespaces()

removes double spaces (handy when using 'cut')

                                                                                        
        # removes double spaces (handy when using 'cut')
# usage: echo " 1  foo    bar" | removedoublespaces     (outputs: " 1 foo bar")

removedoublespaces(){
  cat - | tr -s " "
}
      
/ /bash/function/string/removedoublespaces  # copy/paste this line into your terminal


replacenewlines()

replaces newlines (enters) in string with given parameter

                                                                                        
        # replaces newlines (enters) in string with given parameter
# usage: cat file.txt | replacenewlines ","

replacenewlines(){
  cat - | sed ':a;N;$!ba;s/\n/"$1"/g'
}
      
/ /bash/function/string/replacenewlines  # copy/paste this line into your terminal


runascmd()

expands string with spaces to command with arguments (and runs it)

                                                                                        
        # expands string with spaces to command with arguments (and runs it)
# @param string command
# usage: stringtocmd "ls -la /tmp"

function runascommand(){
  ${1}
}
      
/ /bash/function/string/runascmd  # copy/paste this line into your terminal


striphtml()

removes html/xml-tags from input string

                                                                                        
        # strip html/xml tags from input string
# usage: echo "<b>foo</b>" | striphtml       <-- returns 'foo'

striphtml(){
  echo "$1" | sed 's|<[^>]*>||g'
}
      
/ /bash/function/string/striphtml  # copy/paste this line into your terminal


strlen()

returns the length of a string

                                                                                        
        # returns length of a string
# @param string input
# usage: strlen "foo" (output: 3)
#        length="$( strlen "foo" )"; echo "$length"  (output: 3)

strlen(){
  echo "${#1}"
}
      
/ /bash/function/string/strlen  # copy/paste this line into your terminal


substr()

returns part of string

                                                                                        
        # returns part of string
# @param string input
# @param string startoffset
# @param string number of characters
#
# usage: substr foobar 0 3     (outputs 'foo')
#        short="$( substr "foobar" 0 3 )"; echo "$short"

function substr(){
  echo "${1:$2:$3}"
}
      
/ /bash/function/string/substr  # copy/paste this line into your terminal


supercut()

the 'chucknorris'-cut, removes double delimiters and such. When 'cut' fails: use supercut!

                                                                                        
        # the 'chucknorris'-cut, handy for csv and ls, removes double delimiters and such. When 'cut' fails: use supercut!
# for csv parsing with quote-support use bashlive's 'csvget'
# @param string columnnumber
# @param string delimiter [optional, default: space]
# usage: ls -la     | supercut 4        <-- prints the 4th column
#        cat my.csv | supercut 3 ;      <-- prints the 4rd column from a ';'-seperated csv without quotes

supercut(){
  [[ ! -n $2 ]] && delimiter=' ' || delimiter=${2:0:1}; cat - | tr -s "$delimiter" | cut -d"$delimiter" -f $1
}
      
/ /bash/function/string/supercut  # copy/paste this line into your terminal


trim()

remove leading and trailing whitespaces

                                                                                        
        # remove leading and trailing whitespaces
# usage: echo " foo bar " | trim 

trim(){
  cat - | sed -e 's/^ *//g' -e 's/ *$//g'
}
      
/ /bash/function/string/trim  # copy/paste this line into your terminal


ucfirst()

print first character in capital

                                                                                        
        # print first character in capital
# usage: echo "hello" | ucfirst      <-- outputs 'Hello'
#
#        str="hello"
#        str="${str^}"               <-- shortcut function

ucfirst(){
  cat - | echo "${1^}"
}
      
/ /bash/function/string/ucfirst  # copy/paste this line into your terminal


uppercase()

converts lowercase string to uppercase ('foo' to 'FOO')

                                                                                        
        # converts lowercase string to uppercase ('foo' to 'FOO')
# example: echo "foo" | uppercase    (output: FOO)

uppercase(){
  cat - | tr '[:lower:]' '[:upper:]'
} 
      
/ /bash/function/string/uppercase  # copy/paste this line into your terminal


urldecode()

decodes an urlencoded string (%20 to ' ' e.g.)

                                                                                        
        # decodes an urlencoded string (%20 to ' ' e.g.)
# @param string input
# usage: echo "http://foo.com/foo.php$(urldecode "?foo=bar bar")"

urldecode(){
  data="$(cat - | sed 's/+/ /g')"
  printf '%b' "${data//%/\x}"
} 
      
/ /bash/function/string/urldecode  # copy/paste this line into your terminal


urlencode()

applies a raw url encode on the string

                                                                                        
        # applies a raw url encode on the string
# usage: urlencode "foo foo bar"      <-- outputs: foo%3dfoo%20bar
# @param string stringtoencode

urlencode() {
  local string="${1}"; local strlen=${#string}; local encoded=""
  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}"    # You can either set a return variable (FASTER) 
}
      
/ /bash/function/string/urlencode  # copy/paste this line into your terminal


wraplines()

prints lines which do not exceed the terminal width

                                                                                        
        # prints lines which do not exceed the terminal width
# usage: cat somefile.txt | wraplines

function wraplines(){
  cat - | cut -c 1-$(tput cols)
}
      
/ /bash/function/string/wraplines  # copy/paste this line into your terminal


System


getarch()

check for system arch, returns (64 or 32)

                                                                                        
        # check for system arch, returns [64|32]

function getarch()
{   
    [ -z "$MACHTYPE" ] && _arch_var_arch=$(uname -m) || _arch_var_arch=$(printf "%s\\n" "$MACHTYPE" | cut -d- -f1)

    case $_arch_var_arch in
        x86_64)
            _arch_var_arch=64;
            ;;
        i686)
            _arch_var_arch=32;
            ;;
        *)
            return 1
            ;;
    esac

    printf "%s\\n" "$_arch_var_arch"
}
      
/ /bash/function/system/getarch  # copy/paste this line into your terminal


getdistro()

return distro name in a lower string

                                                                                        
        # return distro name in a lower string

function getdistro()
{   
    _distro_var_DIST_INFO="/etc/lsb-release"
    if [ -r "$_distro_var_DIST_INFO" ]; then
        . "$_distro_var_DIST_INFO"
    fi

    if [ -z "$DISTRIB_ID" ]; then
        _distro_var_DISTRIB_ID="Unknown";
        if [ -f /etc/arch-release ]; then
            _distro_var_DISTRIB_ID="Arch"
        elif [ -r /etc/knoppix-version ]; then
            _distro_var_DISTRIB_ID="Knoppix"
        elif [ -r /etc/sidux-version ]; then
            _distro_var_DISTRIB_ID="Sidux"
        elif [ -r /etc/debian_version ]; then
            _distro_var_DISTRIB_ID="Debian"
        elif [ -r /etc/issue ]; then
            _distro_var_DISTRIB_ID=$(cat /etc/issue.net | awk '{print $1}')
            if [ X"$_distro_var_DISTRIB_ID" = X"Ubuntu" ]; then
                _distro_var_DISTRIB_ID=Ubuntu
            fi
        elif [ -r /etc/gentoo-release ]; then
            _distro_var_DISTRIB_ID="Gentoo"
        elif [ -f /etc/lfs-version ]; then
            _distro_var_DISTRIB_ID="LFS"
        elif [ -r /etc/pclinuxos-release ]; then
            _distro_var_DISTRIB_ID="PCLinuxOS"
        elif [ -f /etc/mandriva-release ] || [ -f /etc/mandrake-release ]; then
            _distro_var_DISTRIB_ID="Mandriva"
        elif [ -f /etc/redhat-release ]; then
            _distro_var_DISTRIB_ID="RedHat"
        elif [ -f /etc/fedora-release ]; then
            _distro_var_DISTRIB_ID="Fedora"
        elif [ -r /etc/vector-version ]; then
            _distro_var_DISTRIB_ID="VectorLinux"
        elif [ -r /etc/slackware-version ]; then
            _distro_var_DISTRIB_ID="$(cat /etc/slackware-version)"
        elif [ -f /etc/release ]; then
            _distro_var_DISTRIB_ID="Solaris"
        elif [ -r /etc/SuSE-release ]; then
            _distro_var_DISTRIB_ID="$(grep -i suse /etc/SuSE-release)"
        elif [ -f /etc/yellowdog-release ]; then
            _distro_var_DISTRIB_ID="YellowDog Linux"
        elif [ -f /etc/zenwalk-version  ]; then
            _distro_var_DISTRIB_ID="Zenwalk"
        fi
        printf "%s\\n" "$_distro_var_DISTRIB_ID" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
    else
        printf "%s\\n" "$DISTRIB_ID" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
    fi
}
      
/ /bash/function/system/getdistro  # copy/paste this line into your terminal


islivecd()

detect a livecd system, return 0 on success, 1 otherwise

                                                                                        
        # detect a livecd system, return 0 on success, 1 otherwise


function islivecd()
    grep boot=live /proc/cmdline >/dev/null && return 0
    grep boot=casper /proc/cmdline >/dev/null && return 0
    return 1
}
      
/ /bash/function/system/islivecd  # copy/paste this line into your terminal


ismonitorattached()

returns 0 if monitor is attached

                                                                                        
        # returns 0 if monitor is attached

function ismonitorattached()
{
    xrandr | grep "^VGA" | grep " connected" >/dev/null && return 0
    xrandr | grep "^HDMI" | grep " connected" >/dev/null && return 0
    return 1
}
      
/ /bash/function/system/ismonitorattached  # copy/paste this line into your terminal


launch()

attempt for universal way of opening a filetype with default application (regardless of windowmanager

                                                                                        
        #!/bin/bash
# attempt for universal way of opening a filetype with default application (regardless of windowmanager)
# usage: open my.pdf   (HINT: put this in your .bashrc: alias o='launch' )
# @param string (arguments)

function launch(){
  if   which xdg-open   &>/dev/null;
    then xdg-open "$@"
  elif which gnome-open &>/dev/null;
    then gnome-open "$@";
  elif which kde-open   &>/dev/null;
    then kde-open "$@";
  elif which open       &>/dev/null; 
    then open "$@"
  fi
} 
      
/ /bash/function/system/launch  # copy/paste this line into your terminal


Template


bashdown()

bash as templating language (like smarty e.g.)

                                                                                        
        # 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 | fetch 

# fetches a document and interprets bashsyntax in string (bashdown) templates 
# @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
fetch(){
  IFS=''; cat - | while read line; do 
    for k in "${!args[@]}"; do [[ "$k" == "0" ]] && continue;
      value="$( echo "${args["$k"]}" | sed -e 's/[\/&]/\\&/g' | sed "s/'/\"/g" )"; eval "$k="$value";"
    done; 
    line="$(eval "echo \"$( echo "$line" | sed 's/"/\\"/g')\"")"; echo "$line" # process bash in snippet
  done
}
      
/ /bash/function/template/bashdown  # copy/paste this line into your terminal