<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Validating a numeric value in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126821#M803401</link>
    <description>Hi,&lt;BR /&gt; &lt;BR /&gt;so basically you only want numbers to be entered:&lt;BR /&gt; &lt;BR /&gt;read i&lt;BR /&gt; &lt;BR /&gt;echo $i | grep -q "^[0-9]*$"&lt;BR /&gt;if [ $? -eq 0 ]&lt;BR /&gt;then&lt;BR /&gt;   echo integer&lt;BR /&gt;else&lt;BR /&gt;   echo not integer&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;regards,&lt;BR /&gt;Thierry.</description>
    <pubDate>Mon, 24 Nov 2003 02:16:10 GMT</pubDate>
    <dc:creator>Thierry Poels_1</dc:creator>
    <dc:date>2003-11-24T02:16:10Z</dc:date>
    <item>
      <title>Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126820#M803400</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I'm developing a script (using Bourne shell) to reorganise space allocation for Informix database tables. The script displays the original space size (an integer value) and then requests a new value to use.&lt;BR /&gt;&lt;BR /&gt;I'd like to validate this entered value to ensure that no alpha or alternate characters are used such as commas or decimal points. I only want an integer to be accepted. ie. 2000 is okay, but not 19.9 or 35,000. &lt;BR /&gt;&lt;BR /&gt;In post &lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=109402" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=109402&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;JRF suggests the following.&lt;BR /&gt;--------------------&lt;BR /&gt;Here's one way, where "X" contains the data of interest:&lt;BR /&gt;&lt;BR /&gt;if [ `expr $X : '[0-9]*'` -ne `expr $X : '.*'` ]&lt;BR /&gt;then&lt;BR /&gt;echo "Integer expected!"&lt;BR /&gt;fi&lt;BR /&gt;--------------------------------&lt;BR /&gt;&lt;BR /&gt;I tried this out and got errors. (Syntax error at line 7 : `&amp;amp;' is not expected) I'm guessing this relates to the fact that I'm using Bourne shell? I was pretty lost with the "[" etc too... Haven't encountered these before.&lt;BR /&gt;&lt;BR /&gt;Would the above suggested syntax work in Bourne shell for what I'm trying to do, with only minor changes, or do I need to take a totally different approach? &lt;BR /&gt;&lt;BR /&gt;I thought that maybe testing the result of a mathematical "mod" expression might be a way of achieving things. Any resultant of a mod other than 0 would mean the number isn't an integer. I'm not sure if the function exists though... &lt;BR /&gt;&lt;BR /&gt;Thanks for any assistance.&lt;BR /&gt;&lt;BR /&gt;Regards.&lt;BR /&gt;&lt;BR /&gt;Paul.</description>
      <pubDate>Sun, 23 Nov 2003 22:21:06 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126820#M803400</guid>
      <dc:creator>Paul Verheyen</dc:creator>
      <dc:date>2003-11-23T22:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126821#M803401</link>
      <description>Hi,&lt;BR /&gt; &lt;BR /&gt;so basically you only want numbers to be entered:&lt;BR /&gt; &lt;BR /&gt;read i&lt;BR /&gt; &lt;BR /&gt;echo $i | grep -q "^[0-9]*$"&lt;BR /&gt;if [ $? -eq 0 ]&lt;BR /&gt;then&lt;BR /&gt;   echo integer&lt;BR /&gt;else&lt;BR /&gt;   echo not integer&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;regards,&lt;BR /&gt;Thierry.</description>
      <pubDate>Mon, 24 Nov 2003 02:16:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126821#M803401</guid>
      <dc:creator>Thierry Poels_1</dc:creator>
      <dc:date>2003-11-24T02:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126822#M803402</link>
      <description>The "[" is html speak for a character represented by its ASCII code, in this case, decimal 91, or open square bracket.  Similarly, "]" is close square bracket.&lt;BR /&gt;&lt;BR /&gt;The grep -v solution shown above sounds sensible.</description>
      <pubDate>Mon, 24 Nov 2003 03:28:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126822#M803402</guid>
      <dc:creator>Alan Turner</dc:creator>
      <dc:date>2003-11-24T03:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126823#M803403</link>
      <description>Paul&lt;BR /&gt;&lt;BR /&gt;Any reason why you are using Bourne shell, it's pretty archaic?&lt;BR /&gt;&lt;BR /&gt;If you used ksh, or posix-sh, it does all the work for you.&lt;BR /&gt;You can declare variables and predefine them as numeric, using typeset -i.&lt;BR /&gt;&lt;BR /&gt;eg&lt;BR /&gt;&lt;BR /&gt;typeset -i number=0&lt;BR /&gt;read number &amp;amp;&amp;amp; do_something &lt;BR /&gt;&lt;BR /&gt;etc&lt;BR /&gt;&lt;BR /&gt;-- Graham</description>
      <pubDate>Mon, 24 Nov 2003 04:17:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126823#M803403</guid>
      <dc:creator>Graham Cameron_1</dc:creator>
      <dc:date>2003-11-24T04:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126824#M803404</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;let Y=X&lt;BR /&gt;if test Y -ne X&lt;BR /&gt;then&lt;BR /&gt;  echo integer expected&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;greetings,&lt;BR /&gt;&lt;BR /&gt;Michael&lt;BR /&gt;</description>
      <pubDate>Mon, 24 Nov 2003 06:47:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126824#M803404</guid>
      <dc:creator>Michael Schulte zur Sur</dc:creator>
      <dc:date>2003-11-24T06:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126825#M803405</link>
      <description>Hi,&lt;BR /&gt;tricky, if you also have to prohibit an empty string or a series of spaces. Try for instance this:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;EMPTY1=$(echo "$1" | tr -d "[:space:]")&lt;BR /&gt;if [ "$EMPTY1" != "" ]&lt;BR /&gt;then&lt;BR /&gt;        EMPTY2=$(echo "$EMPTY1" | tr -d "0-9")&lt;BR /&gt;        if [ "$EMPTY2" = "" ]&lt;BR /&gt;        then&lt;BR /&gt;                echo "$1" is an integer&lt;BR /&gt;        else&lt;BR /&gt;                echo "$1" is not an integer&lt;BR /&gt;        fi&lt;BR /&gt;else&lt;BR /&gt;        echo "$1" is an empty string&lt;BR /&gt;fi&lt;BR /&gt; &lt;BR /&gt;or wait for the perl one-liner...&lt;BR /&gt;&lt;BR /&gt;regards,&lt;BR /&gt;John K.</description>
      <pubDate>Mon, 24 Nov 2003 08:03:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126825#M803405</guid>
      <dc:creator>john korterman</dc:creator>
      <dc:date>2003-11-24T08:03:45Z</dc:date>
    </item>
    <item>
      <title>Re: Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126826#M803406</link>
      <description>If you're using /usr/bin/sh on an HP system then it's not bourne but the posix shell. The real bourne shell is /usr/old/bin/sh.&lt;BR /&gt;&lt;BR /&gt;In the Posix/korn shell, I think the most logical is to use an if statement of the form:-&lt;BR /&gt;&lt;BR /&gt;if [[ ${X} = +([0-9]) ]];&lt;BR /&gt;&lt;BR /&gt;matches any integer including leading zeroes or:&lt;BR /&gt;&lt;BR /&gt;if [[ ${X} = [1-9]*([0-9]) ]];&lt;BR /&gt;&lt;BR /&gt;matches any integer NOT starting with a zero.&lt;BR /&gt;&lt;BR /&gt;Graham's solution will also work but you'd probably want to include 2&amp;gt;/dev/null on the read statement to avoid the annoying error message 'The specified number is not valid for this command.'.&lt;BR /&gt;&lt;BR /&gt;Remember that recent shell patches have imposed the (in my opinion) unreasonable assumption that unless you declare it otherwise, a number which starts with a 0 is octal rather than decimal. So declare your variable thus:&lt;BR /&gt;typeset -i10 X&lt;BR /&gt;&lt;BR /&gt;Then any assignment can be checked by the normal methods such as:-&lt;BR /&gt;let X=? 2&amp;gt;/dev/null || &lt;FAIL action="https://community.hpe.com/"&gt;&lt;BR /&gt;read X 2&amp;gt;/dev/null&lt;BR /&gt;if [[ ${?} -ne 0 ]];&lt;BR /&gt;then &lt;FAIL action="https://community.hpe.com/"&gt;&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;John&lt;/FAIL&gt;&lt;/FAIL&gt;</description>
      <pubDate>Mon, 24 Nov 2003 10:11:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126826#M803406</guid>
      <dc:creator>John Palmer</dc:creator>
      <dc:date>2003-11-24T10:11:38Z</dc:date>
    </item>
    <item>
      <title>Re: Validating a numeric value</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126827#M803407</link>
      <description>Thank you all for your help.&lt;BR /&gt;&lt;BR /&gt;Thierry's solution will basically suits my needs. The extra information provided by John K and John P will be useful too.&lt;BR /&gt;&lt;BR /&gt;Apologies. I got my shell environments mixed up. I'm a bit of a dill. If you saw a picture of me you'd quickly work that out. :-)&lt;BR /&gt;&lt;BR /&gt;Again, thanks all.&lt;BR /&gt;&lt;BR /&gt;Paul.</description>
      <pubDate>Mon, 24 Nov 2003 16:01:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/validating-a-numeric-value/m-p/3126827#M803407</guid>
      <dc:creator>Paul Verheyen</dc:creator>
      <dc:date>2003-11-24T16:01:49Z</dc:date>
    </item>
  </channel>
</rss>

