|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 ]]
Last edited by Scrutinizer; 04-08-2012 at 12:45 AM.. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
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
|
||||
|
||||
|
The more modern syntax is Code:
while read x
do
if (( charCount < ${#x} ))
then
charCount=${#x}
longest=$x
fi
done < $1 |
|
#4
|
||||
|
||||
|
Minor nitpick: Code:
done < "$1" Otherwise it will break for filenames with spaces or strange characters.. Quote:
![]() Code:
$ [[ "bbb" < "aaaa" ]] && echo hello $ [[ " bbb" < "aaaa" ]] && echo hello hello Last edited by Scrutinizer; 04-08-2012 at 02:13 AM.. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Works like a charm now. Thank you for the help
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|