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 / bashweb
Created November 10, 2013 14:43
bashweb is simple but powerfull bashscript which can start an generalpurpose httpserver. Its applications can be restful services, or simple html applications which can be ran locally in the browser.
#!/bin/bash
TMPFILE="/tmp/.bashweb.$(whoami)"
PORT=8080
start(){
[[ -n "$1" ]] && PORT="$1"; TMPFILE="$TMPFILE.$PORT"; [[ ! -p $TMPFILE ]] && mkfifo "$TMPFILE"
while [[ -p "$TMPFILE" ]]; do
[[ -f "$TMPFILE.log" ]] && tail -n5 $TMPFILE.log
{ read line<$TMPFILE;
logmsg(){ cat - | while read l; do echo "[$(date)] $1> $l" >> $TMPFILE.log; [[ ! "$1" == "in" ]] && echo "$l"; done
@coderofsalvation
coderofsalvation / template.sh
Last active September 7, 2018 09:17
Simple powerfull bash template engine (aka macgyver-style smarty)
echo -e '<b>${title}</b>\n$(ls -la)' > foo.html
export title=FOOOOO;
cat foo.html | sed 's/"/\\\"/g;s/.*/echo "&"/e'
@coderofsalvation
coderofsalvation / sql2csv
Last active December 29, 2015 15:39
execute an sqlquery and output as csv (handy for generating quikcndirty sql reports e.g.)
[[ ${#3} == 0 ]] && echo "Usage: [PASSWORD=""] $0 <dbname> <username> <query>" && exit 11
db="$1"
user="$2"
query="$3"
# execute query, quote quotes, and delimit with ","
mysql -u"$user" -p"$PASSWORD" "$db" -B -e "set names 'utf8'; $query" | \
sed -e 's/"/\\"/g' |
sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' | sed ':a;N;s/\r//g;s/\\n//g'
@coderofsalvation
coderofsalvation / bashcolors
Last active December 29, 2015 15:39
bash colors snippet (Usage: printf "$C_GREEN this is green $C_NORMAL this is normal")
# 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"
@coderofsalvation
coderofsalvation / csv
Last active December 29, 2015 15:39
csv bashsnippet which allows outputting only nth column of a csv (Usage: cat my.csv | ./csv column 2
# quickndirty csv column grep function (for easily selecting columns from csv e.g.)
#
# Usage: cat my.csv | csv column 2
#
# csv is a handy database-format, its editable with an texteditor, importable/exportable by
# tech and end-users (mysql/excel/php etc)
#
column(){
cat - | sed 's/,,,/###/g' | sed 's/,,/##/g' | sed 's/,/#/g' | cut -f$1 -d"#" | sed 's/"//g'
@coderofsalvation
coderofsalvation / soxdrumslicer
Last active December 29, 2015 19:39
Simple drumloopslicer using purely bash, sox and vamp plugin on the console
#!/bin/bash
# Simple drumloopslicer using purely bash, sox and vamp plugin on the console
# @dependancy bc vamp-simple-host
slice(){
input="$1"
which bc || { echo "please install 'bc' from your package manager"; exit 1; }
which vamp-simple-host || { echo "please install 'vamp-examples' from your package manager"; exit 1; }
hits="$(vamp-simple-host vamp-example-plugins:percussiononsets "$input" 2>/dev/null | grep -E " [0-9].*" | sed 's/ //g;s/://g')";
declare -a hitArray;
@coderofsalvation
coderofsalvation / loopcrossfade
Last active June 1, 2022 05:32
bashscript which turns an wavfile into a seamless loopable wavfile (using a crossfade technique). The faderatio indicates the length of the fade used, the higher the number the shorter the fade.
# makes a audiofile to loop itself (seamless by using a crossfade trick) (needs sox audio utilities)
# @dependancy sox
# Usage: ./loopcrossfade <input.wav> <faderatio> [outputdir]
#
loopcrossfade(){
input="$1"; faderatio="$2"; outputdir="$3"; tmpinput="/tmp/$(basename "$input").loopcrossfade.wav"
[[ ! -f "$input" ]] && echo "cannot find $1" && exit 1
valid=$(echo "$faderatio > 1.99" | bc -l );
(( $valid == 0 )) && echo "faderatio should be 2.0 or bigger" && exit 1
@coderofsalvation
coderofsalvation / xpathbash
Last active December 30, 2015 00:09
xpath in bash using php
#!/bin/bash
# parse xpath on the commandline (when php is installed)
# example usage : xpath /tmp/test.xml /catalog/book[1]/title
# example output: XML Developer's Guide
#
# given that /tmp/test.xml looks like:
# <?xml version="1.0"?>
# <catalog>
# <book id="bk101">
# <author>Gambardella, Matthew</author>
@coderofsalvation
coderofsalvation / singleprocess.bash
Last active January 2, 2016 05:39
limit shellscript process to only one 1 process, to prevent running twice
# 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 )"
@coderofsalvation
coderofsalvation / exceptions.bash
Last active December 17, 2020 13:09
simple event/exception handling for bash using 2 simple functions. Handy to keep your bash code compact, responsive and extendable. The advantage of this is that it prevents a lot of if/else code.
# simple event / exception handling in bash
#
# onFoo(){
# echo "onFoo() called width arg $1!"
# }
#
# onExit(){
# echo "onExit called!"
# }
#