#! /bin/sh # utility.sh # Miscellaneous Utility Functions unkey_host () { [ $# -ne 1 ] && return 1 sed -i -e "/$1/d" $HOME/.ssh/known_hosts } err () { case $1 in E*) errconst=$1 unset errno if [ "${errno}" = "" ] && command -v python > /dev/null 2>&1 ; then errno=`python -c "import errno;print(errno.${errconst})" 2>/dev/null` fi if [ "${errno}" = "" ] && command -v perl > /dev/null 2>&1 ; then errno=`perl -MErrno -e "print &Errno::${errconst}.\"\n\";" 2>/dev/null` fi if [ "${errno}" = "" ] && command -v ruby > /dev/null 2>&1 ; then errno=`ruby -e "puts Errno::${errconst}::Errno" 2>/dev/null` fi if [ "${errno}" = "" ] && [ -e /usr/include/errno.h ] ; then errno=`cpp -dM /usr/include/errno.h | grep -E "${errconst}[[:space:]]+[0-9]+$" | grep -oE '[0-9]+' 2>/dev/null` fi if [ "${errno}" ] ; then echo "${errconst} = ${errno}" else return 1 fi ;; *) errno=$1 unset errconst if [ "${errconst}" = "" ] && command -v ruby > /dev/null 2>&1 ; then errconst=`ruby -e "puts Errno.constants.find_all{|err| eval('Errno::' + err + '::Errno') == ${errno} }" 2>/dev/null` fi if [ "${errconst}" = "" ] && command -v python > /dev/null 2>&1 ; then errconst=`python -c "import errno;print(errno.errorcode[${errno}])" 2>/dev/null` fi if [ "${errconst}" ] ; then errconst="${errconst} = ${errno}" fi if [ "${errconst}" = "" ] && [ -e /usr/include/errno.h ] ; then errconst=`cpp -dM /usr/include/errno.h | grep -E "E[[:alnum:]]+[[:space:]]+${errno}$"` fi if [ "${errconst}" ] ; then echo $errconst else return 1 fi ;; esac unset errstr if [ "${errstr}" = "" ] && command -v perl > /dev/null 2>&1 ; then errstr=`perl -MPOSIX -e 'print strerror($ARGV[0])."\n";' $errno` fi if [ "${errstr}" = "" ] && command -v python > /dev/null 2>&1 ; then errstr=`python -c "import os;print(os.strerror($errno))" 2>/dev/null` fi if [ "${errstr}" ] ; then echo $errstr else return 2 fi } mkcd () { if [ -d "$1" ] ; then cd "$1" return fi mkdir -p "$1" && cd "$1" } extract () { if [ ! -f "$1" ] ; then echo "The file ("$1") does not exist!" return fi if command -v aunpack > /dev/null 2>&1 ; then aunpack "${1}" return fi filename=`echo "$1" | tr '[:upper:]' '[:lower:]'` case "$filename" in *.tar) tar xf "${1}" ;; *.tar.bz2|*.tbz2|*.tbz) bzip2 -dc "${1}" | tar xf - ;; *.tar.gz|*.tgz|*.tz) gzip -dc "${1}" | tar xf - ;; *.tar.lzma|*.tlz) lzma -dc "${1}" | tar xf - ;; *.tar.xz|*.txz) xz -dc "${1}" | tar xf - ;; *.tar.z|*.tar.Z|*.taz) uncompress -c "${1}" | tar xf - ;; *.tar.7z|*.t7z) 7zr x -so "${1}" | tar xf - ;; *.lzma) lzma -dc "${1}" > `basename "${1%.*}"` ;; *.a|*.deb) ar x "${1}" ;; *.ace) unace x "${1}" ;; *.arj) arj x "${1}" ;; *.cpio) cpio -id < "${1}" ;; *.rpm) rpm2cpio "${1}" | cpio -id ;; *.lha|*.lzh) lha xqf "${1}" ;; *.pax) pax -rf "${1}" ;; *.pax.bz2) bzip2 -dc "${1}" | pax -rf - ;; *.rar) unrar x -idq "${1}" ;; *.rz) rzip -d -f -k "${1}" ;; *.zip|*.jar) unzip -qo "${1}" ;; *.zoo) zoo x "${1}" ;; *.bz2|*.bz) bzip2 -dc "${1}" > `basename "${1%.*}"` ;; *.gz|*.z) gzip -dc "${1}" > `basename "${1%.*}"` ;; *.xz) xz -dc "${1}" > `basename "${1%.*}"` ;; *.Z) uncompress -c "${1}" > `basename "${1%.*}"` ;; *.7z) 7zr x "${1}" ;; *) echo "Unable to extract '"$1"'" return 1 ;; esac return $? } # vim: set ft=sh ts=3 sw=3 et: