LinuxQuestions.org
Help answer threads with 0 replies.
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 01-28-2008, 08:00 AM   #1
naren_0101bits
Member
 
Registered: Jul 2004
Location: Guntur
Posts: 44

Rep: Reputation: 15
Unhappy problem in comparing numeric with string


Hi all,

I am having a problem in comparing numeric value with string.
I have a variable in my script which gets the value dynamically. It can be a numeric value or a string. [ FILE_COUNT=5 (or) FILE_COUNT="VARIABLE" ]

I have to do separate task based on its value numeric or sting variable VARIABLE.

I grep FILE_COUNT and obtained the value. But its giving error when i did either
if [ $FILE_COUNT == "VARIABLE" ]
or
if [ $FILE_COUNT -eq "VARIABLE" ]
as it is not two numeric or two string comparisons.

I tried using test also likewise
if [ test $FILE_COUNT = "VARIABLE" ] but invain

I am thinking of using "typeset -i <variable>" concept but it assigns 0 if it is a string and the dynamic value that i receive can also be 0.

So, can you please give me some light in to this issue to let me move forward in this.

Thanks in advance,

Naren
 
Old 01-28-2008, 08:10 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Maybe don't use output but check with something like this?
Code:
checkNumeric () { typeset -i chkvar; str="$1"; let "foo=$str" 2>/dev/null; return; }
]$ checkNumeric yesitsanumber; echo $?
1
]$ checkNumeric 1; echo $?
0
 
  


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: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Problem with comparing user input to result of a query php orfiyus Programming 3 07-10-2007 11:08 PM
[php] comparing found username with string Erhnam Programming 4 03-15-2005 09:34 PM
left numeric keyboard is typing numbers (problem with NumLock on/off) zstingx Linux - General 0 11-10-2003 09:59 AM
storing and comparing a string Randall Programming 18 03-10-2003 10:05 PM

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

All times are GMT -5. The time now is 08:05 AM.

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