Ways to test on string length

Ways to test on string length

Post by * Tong » Sat, 23 Dec 2000 07:53:57



Hi,

I'm wondering how would you test on the length of a string? Say to
test whether the variable $fname has the length longer than 2?

I can use two (nested) expr command to do that or, one awk command
with echo:

  while `echo $fname | awk '{exit !(length() >2)}' `; do

any other ways? thanks!

--
Tong (remove underscore(s) to reply)
  http://members.xoom.com/suntong001/
  - All free contribution & collection & music from the heavens

 
 
 

Ways to test on string length

Post by Al Shark » Sat, 23 Dec 2000 08:07:25



> I'm wondering how would you test on the length of a string? Say to
> test whether the variable $fname has the length longer than 2?

> I can use two (nested) expr command to do that or, one awk command
> with echo:

>   while `echo $fname | awk '{exit !(length() >2)}' `; do

> any other ways? thanks!

No external programs needed using ksh:

if [ ${#fname} -le 2 ] ; then
   print "shorter or equals 2"
else
   print "longer than 2"
fi

or

case ${#fname} in
   0|1) print "shorter than 2" ;;
   2)   print "length = 2" ;;
   *)   print "longer than 2" ;;
esac

 
 
 

Ways to test on string length

Post by * Tong » Sat, 23 Dec 2000 08:24:13




> > I'm wondering how would you test on the length of a string? Say to
> > test whether the variable $fname has the length longer than 2?

> > I can use two (nested) expr command to do that or, one awk command
> > with echo:

> >   while `echo $fname | awk '{exit !(length() >2)}' `; do

> > any other ways? thanks!

> No external programs needed using ksh:

How about bash? :-)

--
Tong (remove underscore(s) to reply)
  http://members.xoom.com/suntong001/
  - All free contribution & collection & music from the heavens

 
 
 

Ways to test on string length

Post by Tony Higgin » Sat, 23 Dec 2000 08:40:07


You can use wc:

tony=abcdef

echo "$tony" | wc -m or wc -C then you need to -1 from the result as it
adds an additional one on for some strange reason.

Cheers


> Hi,

> I'm wondering how would you test on the length of a string? Say to
> test whether the variable $fname has the length longer than 2?

> I can use two (nested) expr command to do that or, one awk command
> with echo:

>   while `echo $fname | awk '{exit !(length() >2)}' `; do

> any other ways? thanks!

> --
> Tong (remove underscore(s) to reply)
>   http://members.xoom.com/suntong001/
>   - All free contribution & collection & music from the heavens

--
Tony
 
 
 

Ways to test on string length

Post by ratz » Sat, 23 Dec 2000 09:16:48





> > > I'm wondering how would you test on the length of a string? Say to
> > > test whether the variable $fname has the length longer than 2?

> > > I can use two (nested) expr command to do that or, one awk command
> > > with echo:

> > >   while `echo $fname | awk '{exit !(length() >2)}' `; do

> > > any other ways? thanks!

> > No external programs needed using ksh:

> How about bash? :-)

Hey, why don't you try such a simple ksh-example in bash? Normally for such
simple examples ksh is quite close to bash syntax.
It works if you do a s/print/printf/:

Quote:> # a=test
> # if [ ${#a} -le 2 ]; then printf "shorter or equals 2"; else printf "longer than 2"; fi

longer than 2

Best regards,
Roberto Nibali, ratz

--

 
 
 

Ways to test on string length

Post by ratz » Sat, 23 Dec 2000 10:03:24



> You can use wc:

> tony=abcdef

> echo "$tony" | wc -m or wc -C then you need to -1 from the result as it
> adds an additional one on for some strange reason.

What version of wc are you referring to? I'm using GNU textutils 2.0 and the
wc that come with it and there you have neither -m nor -C. I guess wc counts
the strlen of $tony with the closing \0.

regards,
Roberto Nibali, ratz

--

 
 
 

Ways to test on string length

Post by Kevin Mile » Sat, 23 Dec 2000 19:00:53



> Hi,

> I'm wondering how would you test on the length of a string? Say to
> test whether the variable $fname has the length longer than 2?

if [ ${#varname} -gt "2" ]
then
echo "longer than 2 characters
fi

--
Kev

 
 
 

Ways to test on string length

Post by Heiner Steve » Sun, 24 Dec 2000 08:58:09


 > I'm wondering how would you test on the length of a string? Say to
 > test whether the variable $fname has the length longer than 2?
 >
 > I can use two (nested) expr command to do that or, one awk command
 > with echo:
 >
 >   while `echo $fname | awk '{exit !(length() >2)}' `; do

I only need one "expr" command:

        if [ 0`expr "$fname" : '.*'` -gt 2 ] ...

The following part print the string length:

        expr "$fname" : '.*'

Heiner
--
 ___ _

\__ \  _/ -_) V / -_) ' \    SHELLdorado for Shell Script Programmers:
|___/\__\___|\_/\___|_||_|   http://www.oase-shareware.org/shell/

 
 
 

Ways to test on string length

Post by Andreas Eibac » Sun, 24 Dec 2000 19:06:35



Quote:> What version of wc are you referring to? I'm using GNU textutils 2.0 and
the
> wc that come with it and there you have neither -m nor -C. I guess wc
counts
> the strlen of $tony with the closing \0.

Cor!
Roberto, that was the most important post in this thread so far!
'cause everyone ought to stick to _that one_ rule ...

 DO NOT USE PROPRIETARY STUFF!

 Since Cygwin (ie a Un*x environment on Windows 9x/NT) sticks very closely
to the GNU versions of UN*X tools, Cygwin's "wc" command also does neither
provide '-C' nor '-m'.
I appreciate that - so you can make sure your code will work everywhere
instead of breaking up with error messages "unknown option" and consorts.

Andreas

 
 
 

Ways to test on string length

Post by Michael Slas » Sun, 31 Dec 2000 07:37:35



> echo "$tony" | wc -m or wc -C then you need to -1 from the result as it
> adds an additional one on for some strange reason.

The additional character is the newline added by echo

I would use, for LCD compability,

while [ `echo $myvar | wc -c | sed 's/[^0-9]//g'` -lt 3 ]
do
        some stuff
done

-Mike

> Cheers


> > Hi,

> > I'm wondering how would you test on the length of a string? Say to
> > test whether the variable $fname has the length longer than 2?

> > I can use two (nested) expr command to do that or, one awk command
> > with echo:

> >   while `echo $fname | awk '{exit !(length() >2)}' `; do

> > any other ways? thanks!

> > --
> > Tong (remove underscore(s) to reply)
> >   http://members.xoom.com/suntong001/
> >   - All free contribution & collection & music from the heavens

> --
> Tony