The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 09-29-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Can you use ksh instead of sh? If so, you can easily test the answers with something like:
Code:
#!/bin/ksh

...possible code...

# Declare inputs as integers
integer n1=0 n2=0

...possible code...

# Take n1 input
until (( $n1 > 0 && $n1 < 21 )); do
  print -n "Please enter a positive integer between 1 and 20: "; read n1
  if (( $n1 < 0 || $n1 > 20 )); then
    print "$n1 is out of range."
  fi
done

# Take n2 input
until (( $n2 > 0 && $n2 < 21 )); do
  print -n "Please enter another positive integer between 1 and 20: "; read n2
  if (( $n2 < 0 || $n2 > 20 )); then
    print "$n2 is out of range."
  fi
done

If you want to display results and quit when the user hits Ctrl-C, you'll need to trap that signal. The syntax is
Code:
trap command signal1 signal2 ...

In your case, the signal would be INT.