[plug] problem found in "if" statement

James Devenish devenish at guild.uwa.edu.au
Thu Aug 28 07:38:16 WST 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