diff --git a/commonsh/10_utility b/commonsh/10_utility index 10fde67..0f553cd 100755 --- a/commonsh/10_utility +++ b/commonsh/10_utility @@ -4,41 +4,46 @@ unkey_host () { - [ ${#} -ne 1 ] && return 1 + [ $# -ne 1 ] && return 1 sed -i -e "/$1/d" $HOME/.ssh/known_hosts } err () { - case $@ in + 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.$@)" 2>/dev/null` + 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::$@::Errno if (Errno::$@)" 2>/dev/null` + 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 -w "$@" | grep -oE '[0-9]+' 2>/dev/null` + 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 "$@ = $errno" + echo "${errconst} = ${errno}" else return 1 fi ;; *) + errno=$1 unset errconst - if [ "${errconst}" = "" ] && ( command -v python > /dev/null 2>&1 ); then - errconst=`python -c "import errno;print(errno.errorcode[$@])" 2>/dev/null` - fi if [ "${errconst}" = "" ] && ( command -v ruby > /dev/null 2>&1 ); then - errconst=`ruby -e "puts Errno.constants.find_all{|err| eval('Errno::' + err + '::Errno') == $@ }"` + errconst=`ruby -e "puts Errno.constants.find_all{|err| eval('Errno::' + err + '::Errno') == ${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}] + ' = ${errno}')" 2>/dev/null` fi if [ "${errconst}" = "" ] && [ -e /usr/include/errno.h ] ; then - errconst=`cpp -dM /usr/include/errno.h | grep -w "E.* $@"` + errconst=`cpp -dM /usr/include/errno.h | grep -E "E[[:alnum:]]+[[:space:]]+${errno}$"` fi if [ "${errconst}" ] ; then @@ -46,15 +51,21 @@ err () else return 1 fi - - errno=$@ ;; esac - if ( command -v perl > /dev/null 2>&1 ) ; then - perl -MPOSIX -e 'print strerror($ARGV[0])."\n";' $errno - elif ( command -v python > /dev/null 2>&1 ) ; then - python -c "import os;print(os.strerror($errno))" + 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 } @@ -70,13 +81,13 @@ mkcd () extract () { - if ( command -v aunpack > /dev/null 2>&1 ) ; then - aunpack "${1}" + if [ ! -f "$1" ] ; then + echo "The file ("$1") does not exist!" return fi - if [ ! -f "$1" ] ; then - echo "The file ("$1") does not exist!" + if ( command -v aunpack > /dev/null 2>&1 ) ; then + aunpack "${1}" return fi