Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test if $var is an integer or string 1

Status
Not open for further replies.

hfaix

MIS
Nov 25, 2003
596
US
Is it possible to "test" to see if the value $var is an integer or string?

Thanks!
 
In the Korn shell, you can force a variable to be an integer with something like...
Code:
   typeset -i INTVAR
Then, if you try to assign a string to it, you will get an error message.

You can force it to be a string with...
Code:
   typeset +i STRVAR
...but keep in mind that integers are valid characters in a string.

The command "[tt]typeset[/tt]" by itself will list all of the variables. If they are an integer, it will say [tt]integer[/tt] next to the variable. If it's a normal variable (string), it will say [tt]export[/tt] next to it. If you type the command "[tt]typeset -i[/tt]", it will only list integer variables, so maybe something like...
Code:
   (( $(typeset -i|grep INTVAR|wc -l) )) && print "INTVAR is an integer"
Hope this helps.


 
If you want to test if that variable contains an integer, even if the variable is a string variable (i.e. [tt]VAR="1234"[/tt]), then you can do this...
Code:
    [[ $VAR = [0-9]*([0-9]) ]] && print "VAR is an Integer"
This will work whether the variable is defined as an integer or a string.

Hope this helps.

 
This works great, however I don't know why. Would you mind explaining the [0-9]*([0-9]) portion. What is this?
 
It tests that $VAR starts with a numeric character (range from 0 to 9), has any amount of numeric characters (in the same range) and ends with a numeric character (range from 0 to 9). Obviously there are no other numerics outside the range 0-9 !

Dickie Bird (:)-)))
 
Thanks! I tested and this works well.

I would assume the * means everything in the middle of the first and last "numeric character" is numeric as well.
 
No, that's actually not what the asterisk means in this case. The construct is...
Code:
    *(X)
...where [tt]X[/tt] is any character or pattern. The "star parenthesis" means match zero or more occurances of this pattern. The
Code:
[0-9]
means we're matching only numeric characters.

Say you had files in a directory with the following names "A", "AA", "AAA", and a bunch of other files. If you types the following...
Code:
    ls -l *(A)
...it would only list "A", "AA", "AAA". If you typed...
Code:
    ls -l *([0-9])
...it would only list files that had all numeric names. If you used the following...
Code:
    ls -l *(go)
...it would match files "go", "gogo", "gogogo", and so forth.

The reason there's another
Code:
[0-9]
in front of it for the match we're trying is because the [tt]*()[/tt] means "zero or more occurances of this pattern". Since it will also match zero occurances of the pattern, we just prepend it with another numeric character pattern to make it "one or more occurances". So, the pattern...
Code:
    [0-9]*([0-9])
...means match one numeric character, plus zero or more numeric characters.

If it were a normal asterisk wild card, it would match ANY character and something like "23skd00" would match too, which is not what we want here.

Actually, a better way to do it would have been...
Code:
    [[ $VAR = +([0-9]) ]] && print "VAR is an Integer"
...where the [tt]+()[/tt] means match "one or more occurances of the patter".

Hope this helps.

 
The pattern match operators are...

[tt]?(pattern-list) [/tt]match zero or one occurance of any pattern.
[tt]*(pattern-list) [/tt]match zero or more occurances of any pattern.
[tt]+(pattern-list) [/tt]match one or more occurances of any pattern.
[tt]@(pattern-list) [/tt]match exactly one occurance of any pattern.
[tt]!(pattern-list) [/tt]match anything except any pattern.
[tt]pattern-list [/tt]one pattern, or multiple patterns
[tt] [/tt]separated with a | character.

This means that...
Code:
    !(*.c|*.h|*.o)
...would match anything NOT ending with .c, .h, or .o.

Hope this helps.

 
the [0-9] works for me, why doesn't the +([0-9]) work? it keeps on showing a syntax error for the +([ even without the + sign it still returns an error for the ([

 
What shell are you using? This is all for the Korn shell only.

 
While

Code:
    +([0-9])

does match integers, is 0000003 an integer?
(It may or may not be, depending on the situation).

Now something like:

Code:
    @(?([-+])[1-9]*([0-9])|0)

might be more applicable, integers with no leading zeros with an optional sign, or a plain 0. This would accept +23, -9092, 0, but not -0, +0, -000009, etc.

Converting this to something that expr(1) could use is a task left for the student :).

Derek

----------------
Derek Ludwig
derek<at>ludwig.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top