Computing.Net > Forums > Unix > ksh scripting - numeric vs. alpha

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

ksh scripting - numeric vs. alpha

Reply to Message Icon

Name: mark
Date: January 30, 2003 at 15:31:13 Pacific
OS: SCO
CPU/Ram: junky old stuff
Comment:

Got a ksh question. I am doing a read to input a variable. I only want to allow numeric characters (12345) for this variable, and to loop back when letters and non-numeric characters (abc#$FG) are input. I am doing several math functions later in the script and don't want them bombing out. Any suggestions or have a script example that handles this? Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: WilliamRobertson
Date: January 30, 2003 at 16:08:25 Pacific
Reply:

Trying this out, I realised that assigning a character string to an integer variable doesn't fail as I thought it would, but just gives the integer the default value 0. However, you can capture the input as a char string, assign that to an integer variable and see if the two match. Try something like this:

while true
do
read testval?"Enter an integer (decimal places will be truncated): "

testval=${testval%.*}
integer i=${testval}

if [[ ${i} != ${testval} ]]
then
print -u2 "Call that an integer?"
else
break
fi
done

print You entered $i


0

Response Number 2
Name: Jerry Lemieux
Date: January 31, 2003 at 07:09:45 Pacific
Reply:

This script will prompt for input and check to see if the input is numeric. Modify it to suit your needs. It will continue the loop if any non-numeric character is in the string.

#!/bin/ksh

while true
do

print "Input a number"
read junk

case $junk in
+([0-9]) ) print "numeric"
break
;;
* ) print "non-numeric, try again"
sleep 3
tput clear
;;
esac

done

Jerry


0

Response Number 3
Name: gatormark91
Date: January 31, 2003 at 08:49:04 Pacific
Reply:

I need to read ONLY values between 0 and 254. I thought about it overnight and decided I can do this using a case command and a loop -

#!/bin/ksh
function valuetest {
echo -n "input a number between 0 and 254: "
read number
count=0
#loop to test if entered value between 0-254
while ((count The above line would be 'continue' in
#a real script
;;
esac
((count=count+1))
done
#on failure, give error mesg and run again

echo $number "is not a value between 0 and 254, please try again"
valuetest
}
valuetest
#above line just calls the function for demonstration


0

Response Number 4
Name: gatormark91
Date: January 31, 2003 at 08:50:51 Pacific
Reply:

Jerry,

actually, wrote this before I read your post. Great minds think alike!

Thanks!


0

Response Number 5
Name: gatormark91
Date: January 31, 2003 at 08:55:18 Pacific
Reply:

I can't type either...

Where I wrote:

while ((count The above line would be 'continue' in
#a real script

in my script, replace with:

while ((count,255))
do
case "$number" in
$count) echo $number "number is good"
echo "exiting"
continue #returns to script
;;
esac

Sorry!


0

Related Posts

See More



Response Number 6
Name: gatormark91
Date: January 31, 2003 at 08:56:37 Pacific
Reply:

still can't type, should say:

while ((count255))

anyway, I got my answer, thanks all!!!


0

Response Number 7
Name: gatormark91
Date: January 31, 2003 at 09:08:06 Pacific
Reply:

count255!!!!!

Geez!!!


0

Response Number 8
Name: gatormark91
Date: January 31, 2003 at 09:08:46 Pacific
Reply:

Why won't this forum accept the less than and greater than symbols when I post????


0

Response Number 9
Name: Jerry Lemieux
Date: January 31, 2003 at 09:39:35 Pacific
Reply:

You just encountered a problem with posting to this forum. I normally, I open a file with:

exec 3 > ./outfile
exec 4 ./infile

The right angle bracket on the exec 3 will appear normally. However, you will not see the left angle bracket on the exec 4 line. If you put the left one first and then the right, everything in between will disappear.

Another way to do this is to do a typeset -i on the read variable like this:

typeset -i junk

When you read $junk you can redirect standard error and standard out to /dev/null like this:

read $junk 1>/dev/null 2>&1

This keeps the invalid value from being read and the case statement will not match a null variable as a numeric. Result is the same. The way I got the read to post properly is to add a right angle bracket after the left on the exec 4 line.

Jerry


0

Response Number 10
Name: WilliamRobertson
Date: February 1, 2003 at 16:24:13 Pacific
Reply:

Just for fun, here's what I got. Note that it continually re-prompts until you enter a valid value or Ctl-C. Also since you can combine the prompting and reading into one command, I think it's better to do so.

integer i

while true
do
read testval?"Enter an integer 0-254 (decimal places will be truncated): "

testval=${testval%.*}
integer i=${testval}

if [[ ${i} != ${testval} ]]
then
print -u2 "Call that an integer?"
elif (( i ; 255 ))
then
print -u2 "Value '$i' not in valid range 0-254."
else
break
fi
done

print You entered $i


0

Response Number 11
Name: WilliamRobertson
Date: February 1, 2003 at 16:28:29 Pacific
Reply:

OK, same problem posting characters that are interpreted as HTML. There is probably a way but since it's late let's just say the line

elif (( i ; 255 ))

should be

elif (( i lessthan 0 )) || ( i > 255 ))


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: ksh scripting - numeric vs. alpha

Need Help With KSH Script www.computing.net/answers/unix/need-help-with-ksh-script/6747.html

ksh script www.computing.net/answers/unix/ksh-script/6748.html

Test ssh connection from KSH script www.computing.net/answers/unix/test-ssh-connection-from-ksh-script/6950.html