Korn Shell - Test with variable that may be not set
- by C. Ross
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?