LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2006, 06:44 AM   #1
qrshat
Member
 
Registered: Oct 2005
Posts: 56

Rep: Reputation: 15
understand variable type in shell script


hi guru,

in shell script i have got a variable and i want to search the type of this variable. i don't know the value is numeric or string and i am looking for a utility,function or command that return the type of the variable.

For example A=123 or A=abc. and i want to search that does A store a numeric value or sting value?

thanks in advance.
 
Old 09-08-2006, 06:51 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
With ksh or bash, "typeset -p" will show the variable type, if any.
 
Old 09-08-2006, 07:40 AM   #3
qrshat
Member
 
Registered: Oct 2005
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlliagre
With ksh or bash, "typeset -p" will show the variable type, if any.
thanks jlliagre

but "typeset -p" shows the variables and their values in my solaris.
but my main problem is the type of the variable. is it numeric or string. i just want to learn the variable is numeric or string. i don't interest its value.
 
Old 09-08-2006, 07:59 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Nope, Solaris (I assume you really mean ksh on Solaris) shows the type of the variable, should it have a type. eg:
Code:
$ typeset -p | grep RANDOM
typeset -i RANDOM
Here RANDOM is of type integer, but the shell, being loosely typed, doesn't mandates a variable to have a type, in which case it is a string by default.
 
Old 09-08-2006, 09:52 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
You could try:
A_Num=expr {$A+"0"}
if [ A_Num -eq A ] and so on ..

End
 
Old 09-09-2006, 02:12 AM   #6
qrshat
Member
 
Registered: Oct 2005
Posts: 56

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by AnanthaP
You could try:
A_Num=expr {$A+"0"}
if [ A_Num -eq A ] and so on ..

End
Thanks,
it's a nice idea. and it sounds good..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script and Dynamic variable xanthium Programming 11 07-12-2011 06:05 AM
Shell Script Random Variable Daniel Programming 14 12-03-2007 05:00 AM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM
expanding variable in shell script dipenchaudhary Programming 8 02-08-2006 05:05 PM
Help with shell script, can't show variable stormrider_may Programming 5 01-27-2006 05:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration