<?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: Need to determine is variable is numeric (Scripting) in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590518#M32809</link>
    <description>Hi Mike:&lt;BR /&gt;&lt;BR /&gt;Here is one approach using a shell function plus a small test script.&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;&lt;BR /&gt;is_an_integer()&lt;BR /&gt;{&lt;BR /&gt;  H=1 &lt;BR /&gt;  if [ $# -ge 1 ]&lt;BR /&gt;    then&lt;BR /&gt;      XX=`echo ${1} | awk '/^-*[0-9]+$/ { print $0}'`&lt;BR /&gt;      if [ -n "${XX}" ]&lt;BR /&gt;        then&lt;BR /&gt;          H=0&lt;BR /&gt;        fi&lt;BR /&gt;    fi&lt;BR /&gt;  return ${H}&lt;BR /&gt;} # is_an_integer&lt;BR /&gt;&lt;BR /&gt;VARS="78 jack909 -909 90-9 0"&lt;BR /&gt;&lt;BR /&gt;for VAR in ${VARS}&lt;BR /&gt;  do&lt;BR /&gt;     echo "${VAR} \c"&lt;BR /&gt;     is_an_integer ${VAR}&lt;BR /&gt;     STAT=$?&lt;BR /&gt;     if [ ${STAT} -eq 0 ]&lt;BR /&gt;       then&lt;BR /&gt;         echo "Yes"&lt;BR /&gt;       else&lt;BR /&gt;         echo "No"&lt;BR /&gt;       fi&lt;BR /&gt;  done&lt;BR /&gt; &lt;BR /&gt;Regards, Clay</description>
    <pubDate>Fri, 05 Oct 2001 18:01:42 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2001-10-05T18:01:42Z</dc:date>
    <item>
      <title>Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590516#M32807</link>
      <description>Hey Gang!&lt;BR /&gt;&lt;BR /&gt;I need a way to determine if a variable is an integer or not.  Nothing complicated, just something that would function in the following script ...&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;read variable1&lt;BR /&gt;&lt;BR /&gt;if test (variable1 is an integer)&lt;BR /&gt; then&lt;BR /&gt;  echo Variable 1 is an integer&lt;BR /&gt; else&lt;BR /&gt;  echo Variable 1 is text or a non-integer number&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Obviously the script is going to do more than the echo statements, but you get the idea.  The user may enter variable1 as text, a number or anything else the read statement can assign variable1 to be...and the if statement cannot error out.&lt;BR /&gt;&lt;BR /&gt;I am stumped and any help would be greatly appreciate.  Any method by which to accomplish the basic jist of this script is fine to.&lt;BR /&gt;&lt;BR /&gt;Thanks for the help!&lt;BR /&gt;Mike&lt;BR /&gt;</description>
      <pubDate>Fri, 05 Oct 2001 17:23:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590516#M32807</guid>
      <dc:creator>Mike Rightmire</dc:creator>
      <dc:date>2001-10-05T17:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590517#M32808</link>
      <description>Try this fun&lt;BR /&gt;&lt;BR /&gt;printf "Type in something:"                               &lt;BR /&gt;read i                                                   &lt;BR /&gt;if [ $i ]                                                 &lt;BR /&gt;then                                                      &lt;BR /&gt;echo "${i}0 + 0 "|bc |grep syntax &amp;gt; /dev/null             &lt;BR /&gt;if [ $? = 0 ]                                             &lt;BR /&gt;then                                                      &lt;BR /&gt;echo You typed some text                                  &lt;BR /&gt;else                                                      &lt;BR /&gt;echo You typed a number                                   &lt;BR /&gt;fi                                                        &lt;BR /&gt;                                                          &lt;BR /&gt;else                                                      &lt;BR /&gt;echo "You typed nothing"                                  &lt;BR /&gt;fi</description>
      <pubDate>Fri, 05 Oct 2001 17:41:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590517#M32808</guid>
      <dc:creator>Sridhar Bhaskarla</dc:creator>
      <dc:date>2001-10-05T17:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590518#M32809</link>
      <description>Hi Mike:&lt;BR /&gt;&lt;BR /&gt;Here is one approach using a shell function plus a small test script.&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;&lt;BR /&gt;is_an_integer()&lt;BR /&gt;{&lt;BR /&gt;  H=1 &lt;BR /&gt;  if [ $# -ge 1 ]&lt;BR /&gt;    then&lt;BR /&gt;      XX=`echo ${1} | awk '/^-*[0-9]+$/ { print $0}'`&lt;BR /&gt;      if [ -n "${XX}" ]&lt;BR /&gt;        then&lt;BR /&gt;          H=0&lt;BR /&gt;        fi&lt;BR /&gt;    fi&lt;BR /&gt;  return ${H}&lt;BR /&gt;} # is_an_integer&lt;BR /&gt;&lt;BR /&gt;VARS="78 jack909 -909 90-9 0"&lt;BR /&gt;&lt;BR /&gt;for VAR in ${VARS}&lt;BR /&gt;  do&lt;BR /&gt;     echo "${VAR} \c"&lt;BR /&gt;     is_an_integer ${VAR}&lt;BR /&gt;     STAT=$?&lt;BR /&gt;     if [ ${STAT} -eq 0 ]&lt;BR /&gt;       then&lt;BR /&gt;         echo "Yes"&lt;BR /&gt;       else&lt;BR /&gt;         echo "No"&lt;BR /&gt;       fi&lt;BR /&gt;  done&lt;BR /&gt; &lt;BR /&gt;Regards, Clay</description>
      <pubDate>Fri, 05 Oct 2001 18:01:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590518#M32809</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-10-05T18:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590519#M32810</link>
      <description>Worked like a charm!!!  Thanks.  For my edification, how exactly does the logic work....what is happening during the script to determine what is what.  Just so I understand better.  The script is working GREAT!  Thanks!&lt;BR /&gt;Mike&lt;BR /&gt;</description>
      <pubDate>Fri, 05 Oct 2001 18:09:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590519#M32810</guid>
      <dc:creator>Mike Rightmire</dc:creator>
      <dc:date>2001-10-05T18:09:49Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590520#M32811</link>
      <description>Mike,&lt;BR /&gt;&lt;BR /&gt;This is not a professional script and is based on a very "raw" logic.&lt;BR /&gt;&lt;BR /&gt;What we are doing here is that we are trying to&lt;BR /&gt;add the variable to an integer. &lt;BR /&gt;&lt;BR /&gt;If the variable is a text, obviously the above operation will give a syntax error.&lt;BR /&gt;&lt;BR /&gt;Else, it will be successful.&lt;BR /&gt;&lt;BR /&gt;-Sri</description>
      <pubDate>Fri, 05 Oct 2001 18:14:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590520#M32811</guid>
      <dc:creator>Sridhar Bhaskarla</dc:creator>
      <dc:date>2001-10-05T18:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590521#M32812</link>
      <description>Hi Mike:&lt;BR /&gt;&lt;BR /&gt;This works quite well:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;typeset X=$1&lt;BR /&gt;if [ `expr "$X" : '[0-9,\-]*'` -ne `expr "$X" : '.*'` ]&lt;BR /&gt;then&lt;BR /&gt;echo "not a number!"&lt;BR /&gt;else&lt;BR /&gt;echo "ok, is a number!"&lt;BR /&gt;fi&lt;BR /&gt;#.end.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Fri, 05 Oct 2001 18:17:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590521#M32812</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-10-05T18:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590522#M32813</link>
      <description>case "$number" in&lt;BR /&gt;  +([0-9]))  print "it is a integer";;&lt;BR /&gt;      *)     print "isn't a integer";;&lt;BR /&gt;esac</description>
      <pubDate>Fri, 05 Oct 2001 18:55:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590522#M32813</guid>
      <dc:creator>Curtis Larson_1</dc:creator>
      <dc:date>2001-10-05T18:55:08Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590523#M32814</link>
      <description>Hi Mike:&lt;BR /&gt;&lt;BR /&gt;BTW, the snipet of code I suggested works by returning and comparing the number of characters in each 'expr'ession.  If the number of numeric characters in the first expression consists (and, the way I wrote it, including the minus sign) is equal to the total number of characters as expressed in the second expresssion, then the first expression must consist only of numbers and therefore be "numeric".&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 06 Oct 2001 02:02:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590523#M32814</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-10-06T02:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590524#M32815</link>
      <description>Hello Mike,&lt;BR /&gt;&lt;BR /&gt;if do not need to use an "if" statement, then &lt;BR /&gt;you may be easier with "case":&lt;BR /&gt;&lt;BR /&gt;echo "enter something: \c"&lt;BR /&gt;read answer&lt;BR /&gt;case "$answer" in&lt;BR /&gt;[0-9]*) echo "Yes, it is a number" ;;&lt;BR /&gt;*) echo "No, definitely not a number" ;;&lt;BR /&gt;esac&lt;BR /&gt;&lt;BR /&gt;and you should always quote substitutions!!!&lt;BR /&gt;&lt;BR /&gt;HTH,&lt;BR /&gt;Wodisch</description>
      <pubDate>Sat, 06 Oct 2001 16:41:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590524#M32815</guid>
      <dc:creator>Wodisch</dc:creator>
      <dc:date>2001-10-06T16:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: Need to determine is variable is numeric (Scripting)</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590525#M32816</link>
      <description>There are always many different ways to accomplish the same thing in Unix.  Here is yet another way:&lt;BR /&gt;&lt;BR /&gt;if [ $( echo $VARIABLE1 | /usr/bin/tr -d [:digit:] &lt;BR /&gt;| /usr/bin/wc -c) -gt 1 ]&lt;BR /&gt;then&lt;BR /&gt;echo "$VARIABLE1 contains non-digits"&lt;BR /&gt;fi&lt;BR /&gt;---&lt;BR /&gt;&lt;BR /&gt;The logic:&lt;BR /&gt;&lt;BR /&gt;Test for all numbers by running the string through /usr/bin/tr to remove all digits. If the resultant string length is greater than 1, then non-digits exist. The reason for -gt 1 is that the string terminator is counted,&lt;BR /&gt;&lt;BR /&gt;Note that I use ALL CAPS for env variables to make them easier to distinguish among the shell commands.  I always use full pathnames for Unix commands in scripts for security and to avoid issues with messed-up $PATH variables.</description>
      <pubDate>Sat, 06 Oct 2001 18:52:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/need-to-determine-is-variable-is-numeric-scripting/m-p/2590525#M32816</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2001-10-06T18:52:04Z</dc:date>
    </item>
  </channel>
</rss>

