Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-07-2012
SCB SCB is offline
Registered User
 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Strings comparing incorrectly

Hello I'm very new to Linux and shell scripting so I only know basic stuff. I'm making a script with the purpose of finding the longest string or word in a file. Here's what I got so far:

Code:
#!/bin/bash 
longest=""
for i in $(strings -n $1); do

if  [[ $longest < $i ]]
then 
 longest=$i
fi
done
echo $longest

So basically the script will use a file given in the command line which contains words and then return the longest one.

This is the file I'm using to test it out it's called test:

Code:
apples
are
the
best
in
the
world

now I run it with bash -x script2 test
so I can see what's happening and this is what I get:

Code:
+ longest=
++ strings -n 1 test
+ for i in '$(strings -n 1 $1)'
+ [[ '' < apples ]]
+ longest=apples
+ for i in '$(strings -n 1 $1)'
+ [[ apples < are ]]
+ longest=are
+ for i in '$(strings -n 1 $1)'
+ [[ are < the ]]
+ longest=the
+ for i in '$(strings -n 1 $1)'
+ [[ the < best ]]
+ for i in '$(strings -n 1 $1)'
+ [[ the < in ]]
+ for i in '$(strings -n 1 $1)'
+ [[ the < the ]]
+ for i in '$(strings -n 1 $1)'
+ [[ the < world ]]
+ longest=world
+ echo world
world

So by the looks of it it's just replacing the value of the current word with the next one.
So my question is how can I accurately compare the words so it returns the longest one?
I'm guessing my mistake is in this line:

Code:
if  [[ $longest < $i ]]

Moderator's Comments:
Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-08-2012 at 12:45 AM..
Sponsored Links
    #2  
Old 04-07-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,565
Thanks: 14
Thanked 440 Times in 425 Posts

Code:
#! /bin/bash

longest=""
charCount=0

while read x
do
    if [ $charCount -lt ${#x} ]
    then
        charCount=${#x}
        longest=$x
    fi
done < $1

echo "Longest Word: $longest; Number of Chars: $charCount"

Sponsored Links
    #3  
Old 04-07-2012
fpmurphy's Avatar
who?
 
Join Date: Dec 2003
Location: /dev/ph
Posts: 4,429
Thanks: 47
Thanked 356 Times in 330 Posts
The more modern syntax is

Code:
while read x
do
    if (( charCount < ${#x} ))
    then
        charCount=${#x}
        longest=$x
    fi
done < $1

    #4  
Old 04-08-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,349
Thanks: 144
Thanked 1,755 Times in 1,592 Posts
Minor nitpick:

Code:
done < "$1"

Otherwise it will break for filenames with spaces or strange characters..


Quote:
Originally Posted by SCB View Post
[..]So by the looks of it it's just replacing the value of the current word with the next one.[..]
It is keeping the one that is lexically greater. If somehow the shorter string were padded with leading spaces to make them equal length, then your comparison would work, if you enclosed both variables in double quotes. But doing so would involve knowing the length of the larger of the two variables and that's what you were set to find out in the first place


Code:
$ [[ "bbb" < "aaaa" ]] && echo hello
$ [[ " bbb" < "aaaa" ]] && echo hello
hello


Last edited by Scrutinizer; 04-08-2012 at 02:13 AM..
Sponsored Links
    #5  
Old 04-08-2012
SCB SCB is offline
Registered User
 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Works like a charm now. Thank you for the help , didn't know it would be so much easier using a while loop.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing two strings infyanurag Shell Programming and Scripting 1 10-15-2008 02:38 AM
comparing 2 strings satish@123 Shell Programming and Scripting 2 05-16-2008 07:59 AM
Comparing Two Strings Anji Shell Programming and Scripting 8 01-09-2008 05:32 AM
Comparing strings yakyaj UNIX for Advanced & Expert Users 2 03-23-2007 01:22 AM
comparing two strings ragha81 Shell Programming and Scripting 24 09-21-2006 11:52 AM



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