The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
string comparison Jatsui Shell Programming and Scripting 5 02-04-2008 04:28 PM
Problem in ksh script ( String comparison ) amarnath AIX 1 07-18-2006 07:40 AM
Problem in ksh script ( String comparison ) amarnath Shell Programming and Scripting 3 07-13-2006 01:14 PM
String Comparison abey High Level Programming 1 10-19-2005 01:08 PM
Unix Programming Book Comparison theicarusagenda High Level Programming 6 03-04-2005 10:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-25-2009
salman4u salman4u is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 52
problem in string comparison in shell programming

Hello,
was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using
Code:
    if [ "p" > "q" ]
and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance
  #2 (permalink)  
Old 02-25-2009
Shahul's Avatar
Shahul Shahul is offline
Registered User
  
 

Join Date: Aug 2008
Location: India
Posts: 175
Arrow

Hi,

you can compare string..like below..

if [ $valuea -ge $valueb ]
then
command
else
command
fi

to compare two strings..

you can use lessthan --> -le
greater than -ge
equal -eq

Thanks
Sha
  #3 (permalink)  
Old 02-25-2009
avi.skynet avi.skynet is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 15
solution

hi
you can compare the string by the following commands as u have told strcmp() in C
if [ "$string1" == "$string2" ]
then

else


fi
  #4 (permalink)  
Old 02-26-2009
salman4u salman4u is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 52
Quote:
Originally Posted by Shahul View Post
Hi,

you can compare string..like below..

if [ $valuea -ge $valueb ]
then
command
else
command
fi

to compare two strings..

you can use lessthan --> -le
greater than -ge
equal -eq

Thanks
Sha
I dont think you can compare strings with -le -gt -ge. It's meant for only integer comparison.
  #5 (permalink)  
Old 02-25-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by salman4u View Post
was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using
Code:
    if [ "p" > "q" ]
and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance

In bash, you can compare alphabetically, but it is not standard. You need to escape the angle bracket:

Code:
if [ p \> q ]
  #6 (permalink)  
Old 02-26-2009
salman4u salman4u is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 52
Quote:
Originally Posted by cfajohnson View Post

In bash, you can compare alphabetically, but it is not standard. You need to escape the angle bracket:

Code:
if [ p \> q ]
cfajohnson You're the man. Thanks!!
  #7 (permalink)  
Old 02-26-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,930
Just a quick note - the string comparison is based on the ASCII value of the characters in the string and may fail for strings containing non-ASCII characters.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0