views:

451

answers:

5

I have the following code in KornShell

FAILURE=1
SUCCESS=0

isNumeric(){

    if [ -n "$1" ]; then
        case $1 in
            *[!0-9]* | "")  return $FAILURE;
                       * )  return $SUCCESS;
        esac;
    else
        return $FAILURE;
    fi;
}

#...
FILE_EXT=${FILE#*.}

if [ isNumeric ${FILE_EXT} ]; then
    echo "Numbered file."
fi
#...

In some cases the file name not have an extension, and this causes the FILE_EXT variable to be empty, which causes the following error: ./script[37]: test: 0403-004 Specify a parameter with this command. How should I be calling this function so that I do not get this error?

A: 

[ -z "$1" ] will test for an empty $1, as will [ "" = "$1" ].

Or you could simply prepend a "0" to $1 (i.e, "0$1") to force it to be non-empty before checking if it is numeric (if you want empty extensions to be treated as numeric).

Loadmaster
+1  A: 

I would do this:

if [ isNumeric ${FILE_EXT:="no"} ]; then
    echo "Numbered file."
fi

If all you want to do is determine that the file has a numeric extension

The ${FILE_EXT:="no"} will expand to either the value of FILE_EXT, or 'no' if FILE_EXT is null or empty.

John Weldon
False positive for filenames that consist only of digits since $FILE_EXT will contain the full filename if there is no dot at all.
Dennis Williamson
Ya, good catch.
John Weldon
A: 

I had some probs to run your script (Maybe that's because im using pdksh). So I have adjusted it slightly. Try this:

#!/usr/bin/ksh

FILE=$1
FAILURE=1
SUCCESS=0

isNumeric ()
{
    if [ -n "$1" ]
    then
        case $1 in
            *[!0-9]* | "")
                echo "$1 not a number"
                return $FAILURE
            ;;
                       * )
                echo "$1 is a number"
                return $SUCCESS
            ;;
        esac
    else
        echo "parameter is empty"
        return $FAILURE
    fi
}

#...
FILE_EXT=${FILE#*.}
echo $FILE_EXT

isNumeric "${FILE_EXT}"
if [ "$?" = "0" ]
then
    echo "Numbered file."
fi
+1  A: 

you should use ${FILE##*.} with double "#" instead. also what do you mean the variable $FILE_EXT will be empty? if your file don't have extension, then when you do ${FILE#*.} you will get the just file name in FILE_EXT. how is it empty ?

+2  A: 

You should leave off the square brackets when you are testing the exit code of a function, otherwise, you'll always get "true". Also, you should quote your variable. You could put an additional test for an empty extension as shown:

FILE_EXT=${FILE#*.}

if isNumeric "${FILE_EXT}" &&
    [ "${FILE_EXT}" != "${FILE}" -a "${FILE_EXT}" != "" ]
then
    echo "Numbered file."
fi

Edit: added test to handle filenames that end in "."

Dennis Williamson
This doesn't account for files that end in '.'
John Weldon