Integers in SH

Integers in SH

Post by Klaus Wicovs » Sat, 12 Dec 1992 21:11:21



Hello Netters,

does anyone of you out there know how I can make sure that I have read an
integer rather than a string?

Example:

#!/bin/sh
echo "How many:"
read a

What if someone answers "990Lines"?

Any help is appreciated !!

--
   Klaus Wicovsky                                                  

   ESF Headquarters                    Tel : ++49+30-820 903 47      
   Hohenzollerndamm 152                Fax : ++49+30-820 903 19      

   Germany            

 
 
 

Integers in SH

Post by cameron shell » Sat, 12 Dec 1992 23:24:57



> does anyone of you out there know how I can make sure that I have read an
> integer rather than a string?

> Example:

> #!/bin/sh
> echo "How many:"
> read a

How about:

        case $a in
                [0-9]*) echo "integer";;
                *) echo "non-integer";;
        esac

</dev/cam
--
      Cameron Shelley        | "Epitaph, n.  An inscription on a tomb,

    Davis Centre Rm 3147     |  have a retroactive effect."
 Phone (519) 885-1211 x3499  |                          Ambrose Bierce

 
 
 

Integers in SH

Post by Mike Bartolone 5-42 » Sun, 13 Dec 1992 05:20:05



Quote:

>Hello Netters,

>does anyone of you out there know how I can make sure that I have read an
>integer rather than a string?

>Example:

>#!/bin/sh
>echo "How many:"
>read a

>What if someone answers "990Lines"?

>Any help is appreciated !!

>--
>   Klaus Wicovsky                                                  

#! /bin/sh

TypeCheck() {

   case $a in

        *[a-z,A-Z]*) type="Non-integer"               ;;
                  *) type="Integer"                   ;;
    esac

Quote:}

type="Non-integer"

while [ "$type" = "Non-integer" ]; do
echo "How many:"
read a
TypeCheck
done

---
sigs are like sound-bites..they never say enough, but frequently

 
 
 

Integers in SH

Post by DW » Tue, 15 Dec 1992 14:53:48



| does anyone of you out there know how I can make sure that I have read an
| integer rather than a string?
|
| Example:
|
| #!/bin/sh
| echo "How many:"
| read a
|
|
| What if someone answers "990Lines"?

Do you mean that you want to accept "990Lines" as an answer and strip
off the word or that you want to reject it because it is not an integer?

If you meant the second, there is an easy solution: a case statement based on
a pattern for integers only:

#!/bin/sh
echo "How many:"
read a
case "$a" in
*[!0-9+-]*|?*[-+]*) echo not an integer ;; # has a character that is neither
                                           # a digit nor a sign or has a sign
                                           # later than the first character
*) echo Now that\'s an integer. ;;
esac

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297

 
 
 

Integers in SH

Post by Oliver Lauma » Tue, 15 Dec 1992 19:15:33



> | does anyone of you out there know how I can make sure that I have read an
> | integer rather than a string?
> read a
> case "$a" in
> *[!0-9+-]*|?*[-+]*) echo not an integer ;; # [comment deleted]
> *) echo Now that\'s an integer. ;;
> esac

This is not portable; not all versions of /bin/sh (for instance, the
4.3 BSD shell) support the "!" operator in patterns.  As enumerating
the complement of [0-9] isn't feasible either, I suggest to solve the
problem by means of the "expr" command:

    #!/bin/sh
    echo -n "How many: "
    read a
    if [ `expr length $a = $a : "[0-9]*"` = 1 ]; then
        echo Thanks for this fine integer.
    else
        echo Nope, try again.
    fi

Note that this solution doesn't correctly handle an empty input; this
can be fixed easily.

--

 
 
 

Integers in SH

Post by Uwe Waldma » Tue, 15 Dec 1992 21:46:56



> does anyone of you out there know how I can make sure that I have read an
> integer rather than a string?

> Example:

> #!/bin/sh
> echo "How many:"
> read a

case "$a" in
*[!0-9]*)  echo 'not a number:' "$a" ;;
*[0-9]*)   echo 'number:' "$a" ;;
'')        echo 'not a number (empty):' "$a" ;;
esac

This should work, provided that your sh understands the "[!...]" syntax
for the complement of a character class. (Some older Bourne shells don't!
In this case, you can do something similar with the expr command.)
Note that the order of the cases must not be changed: You have to check
_first_, whether $i contains a character that is not a digit, _then_
whether it contains at least one digit (i.e., that it is nonempty).

--
Uwe Waldmann, Max-Planck-Institut fuer Informatik
Im Stadtwald, D-W6600 Saarbruecken 1, Germany

 
 
 

1. sh ksh and integer limit on test

I've got a little problem with /bin/sh or /bin/ksh on SCO Unix. I suppose
it's a limit on the integer size in a test statement.
Trying to execute a script like this one:

bignum=6442450943
if [ $bignum -le 1 ]
then
   echo "LESS THAN 1"
else
   echo "GREATER THAN 1"
fi
...
I correctly get display the message "GREATER THAN 1", but if I set
bignum=6442450944 or more the test becomes true and I get displayed the
message "LESS THAN 1"

The strange thing is not that there is a limit, but the number who is the
break point: 6442450944. It is not 2^32 or similar.
Is there anybody who can confirm this or give me a detailed explanation of
the problem or where to find out what and which (if there) are these limits
?

Thanks
Maurizio B.

2. vpopmail problems

3. Execute a sh script under perl and sh: sh script; perl script?

4. KDevelop problem

5. root's shell, /sbin/sh, /bin/sh and /usr/ksh

6. KIP Servers: How do I get an IP?

7. #!/bin/sh #!/usr/bin/sh can I do both for 2 diff machines

8. HELP!!! With my computer configuration is LinuxPPC going to work?

9. ASCII 128-255 in sh script: sh strips bit 7!!!

10. sh var -> awk var -> sh var

11. sh Xinstall.sh -check error

12. Any difference between /sbin/sh and /bin/sh?

13. Making a cd with makecdfs.sh burncd.sh