BASH Profile Calculator and Extractor

Most likely my favourite addition to my otherwise untouched BASH profile is the calc function. From the shell on my Linux box I can simply type “calc 2*10+10/1” and be presented with a result. Having been on a Mac with spotlight the past 3 years, this is something I truly missed. Additionally I missed a universal extractor that was easily accessible. File-roller works sufficiently in the GUI, but I wanted something quick from the CLI. Now I can type “extract archive.tar.bz2” and away it goes…

Extract for your own .bashrc below:

calc() {
    echo “scale=3;$@” | bc -l
}
# Extract all things!
extract() {
    local c e i
    (($#)) || return
    for i; do
        c=”
        e=1
        if [[ ! -r $i ]]; then
            echo “$0: file is unreadable: \`$i'” >&2
            continue
        fi
        case $i in
            *.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))
                   c=’bsdtar xvf’;;
            *.7z)  c=’7z x’;;
            *.Z)   c=’uncompress’;;
            *.bz2) c=’bunzip2′;;
            *.exe) c=’cabextract’;;
            *.gz)  c=’gunzip’;;
            *.rar) c=’unrar x’;;
            *.xz)  c=’unxz’;;
            *.zip) c=’unzip’;;
            *)     echo “$0: unrecognized file extension: \`$i'” >&2
                   continue;;
        esac
        command $c “$i”
        e=$?
    done
    return $e
}

 

Updated: