Ways to test on string length 

> 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

Ways to test on string length 

> How about bash? :-)

Normally for such simple examples ksh is quite close to bash syntax. It works if you do a s/print/printf/:

$ 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 

I only need one "expr" command:

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

The following part print the string length:

expr "$fname" : '.*'

Heiner

documented on: 2000.12.21 Thu 22:33:46