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 / strotolower.bash
Created January 8, 2014 20:50
convert a string to lower characters
# convert a string to lower characters
# @param string input
function strtolower()
{
[ -z "$1" ] && return 1
printf "%s\\n" "$@" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' # tr '[:upper:]' '[:lower:]'
}
@coderofsalvation
coderofsalvation / rmcron.bash
Created January 8, 2014 20:53
remove lines with certain strings from crontab
# remove lines with certain strings from crontab
# @param string search
rmcron()
{ #remove cron job, returns 1 on error
[ -z "$1" ] && return 1
crontab -l | grep -v "$1" | crontab
}
@coderofsalvation
coderofsalvation / isprivport.bash
Created January 8, 2014 20:56
check if port is a num and if it's privated (<1025), returns 0 on success, 1 otherwise (bashfunction)
# check if port is a num and if it's privated (<1025), returns 0 on success, 1 otherwise
# @param int portnumber
function isprivport()
{
[ -z "$1" ] && return 1
printf "%s\\n" "$1" | grep -v "[^0-9]" >/dev/null
if [ $? -eq 0 ]; then
[ $1 -gt 0 ] && [ $1 -lt 1025 ] && return 0 || return 1
@coderofsalvation
coderofsalvation / lock.bash
Created January 8, 2014 20:58
returns 0 if string is a number (bash function)
# returns 0 if string is a number
# @param string input
function isnumber()
{ #look for a number, returns 0 on success, 1 otherwise
[ -z "$1" ] && return 1
printf "%s\\n" "$@" | grep -v "[^0-9]" >/dev/null
}
@coderofsalvation
coderofsalvation / isnewer.bash
Last active January 2, 2016 15:39
compare the dates of two files, return true (0) if file1 has been modified more recently otherwise 1
# 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
@coderofsalvation
coderofsalvation / ismonitorattached.bash
Last active January 2, 2016 15:39
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
}
@coderofsalvation
coderofsalvation / islivecd.bash
Created January 8, 2014 21:00
etect 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
}
@coderofsalvation
coderofsalvation / isemptyfile.bash
Created January 8, 2014 21:01
detect if arg is an empty file, returns 0 on sucess, 1 otherwise (bashfunction)
# detect if arg is an empty file, returns 0 on sucess, 1 otherwise
# @param string file
function isemptyfile()
{ [ -z "$1" ] && return 1
if [ -f "$1" ] && [ X"$(wc -c "$1" | cut -d" " -f1)" = X"0" ]; then
return 0
else
return 1
fi
@coderofsalvation
coderofsalvation / getdistro.bash
Created January 8, 2014 21:02
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
@coderofsalvation
coderofsalvation / stacktrace.bash
Created January 8, 2014 21:03
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