I have a script that needs to check if the given parameters are a combination of 0123456789 and not a word or another irelevant character.please help
#!/bin/sh
for vi in "$@"
do
if [ "`echo $vi | sed 's/^[0-9]*$//'`" != "" ]; then
echo "ERROR: invalid parameter: "$vi;
fi
done
bash/ksh
a="$*"
if [[ ${#a} -eq $(expr "$a" : '[0-9 ]*') ]] ; then
echo numbers
else
echo non-number
fi
Another solution (for KSH)
for arg in "$@"
do
if [[ "$arg" = +([0-9]) ]]
then
echo "Valid parameter <$arg>"
else
echo "Invalid parameter <$arg>"
fi
done
Jean-Pierre.
is there anything for tcsh please?
I'm really not a fan of tcsh, but anyway:
#!/bin/tcsh
set a = "$*"
if ( `expr "$a" : '.*'` == `expr "$a" : '[0-9 ]*'` ) then
echo numbers
else
echo non-number
endif
Another method(using awk):
for var in $@; do
if [[ `echo $var | awk -v var=${var} 'BEGIN{if(var!~/[[:digit:]]/)print 1;}'` -eq 1 ]]; then
echo "Not a number"
exit 0
fi
done