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
# flexible xmessage (uses xmessage or gmessage) or printf for printing messages
function x11message()
{ #prints a message using xmessage || gxmessage
[ -z "$1" ] && return 1
XMESSAGE=$(command -v gxmessage 2>/dev/null) || XMESSAGE=$(command -v xmessage 2>/dev/null)
MESSAGE="$(expr "$0" : '.*/\([^/]*\)'): $*"
printf "%s\\n" "$MESSAGE" | fold -s -w ${COLUMNS:-80} >&2
if [ -n "$DISPLAY" ] && [ -n "$XMESSAGE" ]; then
printf "%s\\n" "$MESSAGE" | fold -s -w ${COLUMNS:-80} | $XMESSAGE -center -file -
@coderofsalvation
coderofsalvation / gist:8324193
Created January 8, 2014 20:39
checks if string is valid email (bash function)
# checks if string is valid email
function isemail(){
emailTest=$(echo "${1}" | grep '^[a-zA-Z0-9._%+-]*@[a-zA-Z0-9]*[\.[a-zA-Z0-9]*]*[a-zA-Z0-9]$')
if [[ "$emailTest" != "" ] ; then return 0; else return 1; fi
}
@coderofsalvation
coderofsalvation / isvalidip4.bash
Created January 8, 2014 20:40
returns 0 if parameter is a valid ip4 address, non-zero otherwise
# returns 0 if parameter is a valid ip4 address, non-zero otherwise
function isvalidip4()
{ #return 0 if parameter is a valid ip4 address, non-zero otherwise
#https://groups.google.com/forum/#!original/comp.unix.shell/NDu-kAL5cHs/7Zpc6Q2Hu5YJ
[ -z "$1" ] && return 1
case "$*" in
""|*[!0-9.]*|*[!0-9]) return 1 ;;
esac
@coderofsalvation
coderofsalvation / isvalidinterface.bash
Created January 8, 2014 20:40
returns 0 if parameter is a valid network interface, 1 otherwise
# returns 0 if parameter is a valid network interface, 1 otherwise
function isvalidinterface()
{
[ -z "$1" ] && return 1
ip addr show | grep "$1": >/dev/null && return 0
return 1
}
@coderofsalvation
coderofsalvation / gist:8324242
Created January 8, 2014 20:42
uploads images e too imgur.com, returns url on success, 1 otherwise (bash function)
# uploads images e too imgur.com, returns url on success, 1 otherwise
# @dependancy curl
function uploadimg()
{
[ -z "$1" ] && return 1
for img
do
if [ -f "$img" ] ; then
@coderofsalvation
coderofsalvation / str_replace.bash
Created January 8, 2014 20:45
replaces a string with another string (bash function)
# replaces a string with another string
# @param string source string
# @param string search string
# @param string replacement string"
# usage: result="$(str_replace "foo bar" "foo" "hi")"
function str_replace()
{
[ -z "$1" ] && return 1
[ -z "$2" ] && return 1
@coderofsalvation
coderofsalvation / strlen.bash
Created January 8, 2014 20:46
prints out the length of a string (bash function)
# prints out the length of a string
# @param string input
function strlen()
{
[ -z "$1" ] && return 1
printf "%s\\n" "${#1}"
}
@coderofsalvation
coderofsalvation / stroverlap.bash
Created January 8, 2014 20:47
prints out the duplicate parts of 2 strings (bash function)
# prints out the duplicate parts of 2 strings
# @param string input1
# @param string input2
function stroverlap()
{
[ -z "$1" ] && return 1
[ -z "$2" ] && return 1
printf "%s\\n" $@ | awk 'x[$0]++'
}
@coderofsalvation
coderofsalvation / gist:8324347
Created January 8, 2014 20:48
prints out the differences of two strings (bash function)
# prints out the differences of two strings
# @param string input1
# @param string input2
function strdiff()
{
[ -z "$1" ] && return 1
[ -z "$2" ] && return 1
set -x
@coderofsalvation
coderofsalvation / strtoupper.bash
Created January 8, 2014 20:48
converts a string to lower characters
# converts a string to lower characters
# @param string input
function strtoupper()
{
[ -z "$1" ] && return 1
printf "%s\\n" "$@" | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # tr '[:upper:]' '[:lower:]'
}