The UNIX and Linux Forums  

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
length of string silas.john UNIX for Dummies Questions & Answers 8 02-06-2008 07:22 PM
read string, check string length and cut ozzy80 Shell Programming and Scripting 9 03-21-2007 05:56 PM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 06:11 AM
String length in ksh ssgrpid Shell Programming and Scripting 4 08-03-2005 02:39 PM
length of the string vasikaran UNIX for Dummies Questions & Answers 3 06-30-2005 05:59 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 09-15-2008
mail2sant mail2sant is offline
Registered User
  
 

Join Date: Apr 2008
Location: bangalore
Posts: 71
how can i compare te length of the string

how can i campare te length of the string

$var = unix

if ( $var -eq 4 )

then..

is it right
  #2 (permalink)  
Old 09-15-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348

Code:
var="UNIX"

if [ ${#var} -eq 4 ]; then
  # do something
fi

For basic usage like this you really should look at the appropriate man pages or tutorials.
  #3 (permalink)  
Old 09-15-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,560

Code:
if [ `printf $var|wc -c` -eq 4 ];then echo "4"; fi

  #4 (permalink)  
Old 09-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
ghostdog74: If $var contains whitespace or special characters, it needs to be double-quoted. The solution posted by Franklin52 seems more succinct and workable anyway; the built-in ${#var} length operator doesn't require any external processes, and doesn't have issues with quoting etc.
  #5 (permalink)  
Old 09-16-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,560
true, then put quotes. anyway, its good practice too

Code:
if [ `printf "$var"|wc -c` -eq 4 ];then echo "4"; fi

this method should work on most shells, including bourne, csh
  #6 (permalink)  
Old 09-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Actually I believe the syntax for csh is different.
  #7 (permalink)  
Old 09-16-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,560
Quote:
Originally Posted by era View Post
Actually I believe the syntax for csh is different.
my bad. what i meant is the way to get length, not the if/else.
Closed Thread

Bookmarks

Tags
shell, variable length

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 06:02 AM.


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