[plug] check value type

James Devenish devenish at guild.uwa.edu.au
Wed Aug 27 14:13:44 WST 2003


In message <sf4c9fb0.021 at mmtnetworks.com.au>
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.

In some shells, the `typeset` function will let you tell the shell to
use a particular internal representation (e.g. integer rather than
string). For example (zsh):

% typeset -i BLAH
% BLAH=
% BLAH2=
% echo $BLAH
0
% echo BLAH2

%

However, the distinction between "number" and "string" is often
context-dependent for shell scripts.

In message <sf4c762f.081 at mmtnetworks.com.au>
on Wed, Aug 27, 2003 at 09:13:12AM +0800, Jon  Miller wrote:
> I've found a problem in an "if" statement and cannot find a fix.  The
> statement is
> if [ "$vMLSTAT" >= 0 ]; then

`man [` or `man test` is the documentation you want (yes: if you are
using Bash, then your "if" statement is probably using the `[` command)!
An Bernd mentioned, -gt is the thing you probably want.

Note: If you use [[ instead of [, you will use Bash's own conditional
expression evaluation and you will want to look at the "Conditional
Expressions" section of the Bash man page.

>  I'I've tried also the following
>  and got a error message stating that [ : : integer expression
>  expected
> if [ "$vMLSTAT" -lt 0 ]; then

This is the problem I mentioned earlier: when $vMLSTAT contains no lines
(i.e. it is empty) then [ will not treat it as an integer.





More information about the plug mailing list