[plug] problem found in "if" statement

Jon Miller jlmiller at mmtnetworks.com.au
Thu Aug 28 18:02:46 WST 2003


The output from the awk statement is an integer, without the awk statement the output is a column with about 6-8 integers.  So based on this output I only need the 1st listing to test hence the awk statement.  If the return value is blank then it needs to run the rest of the script commands.  If it's an integer it needs to exit.

Thanks

Jon L. Miller, MCNE, CNS
Director/Sr Systems Consultant
MMT Networks Pty Ltd
http://www.mmtnetworks.com.au

"I don't know the key to success, but the key to failure
 is trying to please everybody." -Bill Cosby



>>> devenish at guild.uwa.edu.au 7:38:16 AM 28/08/2003 >>>
In message <sf4d4a76.097 at mmtnetworks.com.au>
on Thu, Aug 28, 2003 at 12:18:58AM +0800, Jon  Miller wrote:
> Is there a way to find out if the result is a string or integer?

Output of a command has three inherent parts:

 - standard output (string of bytes)
 - standard error (string of bytes)
 - return code (integer)

So, if you are collecting the standard output (which is what you appear
to be doing so far), it is always like a string and never an integer.
However, as Bernd mentioned, you could see if the text *represents* an
integer by searching for digits within the text. In some shells, you can
ask the shell to interpret the text as a number automatically (but I
don't think this applies in Bash).

> The general output is a listing of PID in a single column. I would
> think these are integers.

They are strings that represent integers. Your shell (Bash) and its
helpers (e.g. [ and test) can treat those strings as integers -- IF the
strings are NON-EMPTY and contain only acceptable numeric characters.
As mentioned before, EMPTY output (i.e. blank string) does NOT normally
count as a number. If you try and do `if [ "$BLANK" -lt 0 ];` then you
will get an ERROR when $BLANK is empty, because empty is NOT a number.
It would be like asking for this: `if [ "" -lt 0 ];` -- you're asking
the [ programme whether it things NOTHING is "less than" ZERO.

In message <20030827050851.GD19277 at gudgeon.innovative.iinet.net.au>
on Wed, Aug 27, 2003 at 01:08:52PM +0800, Bernd Felsche wrote:
> On Wed, Aug 27, 2003 at 12:10:12PM +0800, Jon  Miller wrote:
> > Is there a way to check the "type" of a value.  By this I would
> > like to check if the value of a variable is either a string or
> > number.
> 
> For _simple_ numbers, you can use 'expr'. Simply add zero and
> check the return status.
> 
> More rigourously, you could use tr, sed, awk, etc to strip out
> numeric patterns from a string and see if there's anything left;
> which would indicate non-numeric.
> 
> The again, isn't "pi" numeric? :-)







More information about the plug mailing list