Bashims

Useful bash scripts / tricks

Reading CSV

cat csv.doc | IFS=',' read col1 col2
do
echo $col1 $col2
done

Local Variables

By default, when a variable is introduced within bash function, that variable has a global scope, meaning that the caller will also see change of value. To prevent that use local

function thou_shall_not() {
  name="naughty"
}

function thou_shall() {
 local name="Not naughty"
}

name="Not naughty"

thou_shall_not
echo "Function is $name"

name="Not naughty"
thou_shall
echo "Function is $name"

Reading/Writing to same file

cat file.txt |sort|tee file.txt

Running command without TTY

true | (setsid ./testscript.sh) 2>&1 | cat

Exit Handler

function clean_up() {
  true # some cleaup code
}
trap clean_up EXIT

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *