Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!

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
doneprint You entered $i

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
doprint "Input a number"
read junkcase $junk in
+([0-9]) ) print "numeric"
break
;;
* ) print "non-numeric, try again"
sleep 3
tput clear
;;
esacdone
Jerry

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 againecho $number "is not a value between 0 and 254, please try again"
valuetest
}
valuetest
#above line just calls the function for demonstration

I can't type either...
Where I wrote:
while ((count The above line would be 'continue' in
#a real scriptin my script, replace with:
while ((count,255))
do
case "$number" in
$count) echo $number "number is good"
echo "exiting"
continue #returns to script
;;
esacSorry!

You just encountered a problem with posting to this forum. I normally, I open a file with:
exec 3 > ./outfile
exec 4 ./infileThe 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

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
doneprint You entered $i

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 ))

![]() |
![]() |
![]() |

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