Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop through a string 3

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
US
Hi,

In my korn shell script I have a string of length 9 characters which I need to loop through and get each character out to do a comparison. I know AWK could do that trick, but any way to do it with SHELL?

Thanks,
dbadmin
 
man expr (the substr operator)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

I found [tt]expr[/tt] abit slow. You can cycle through the characters with [tt]while[/tt] and [tt]read[/tt].
Code:
echo -n "abcdefghi" | \
while read -n 1 c; do
  [[ "$c" = [aeiou] ]] && echo -n "vowel : "
  echo "- $c"
done
I tested it with [tt]bash[/tt], but works in [tt]sh[/tt] compatibility mode, so should work on [tt]ksh[/tt] too.

Feherke.
 
so should work on ksh too
Not on mine (as I expected ...)
-n is not a standard option for the read builtin.
 
Hi

Uff. Thanks PHV. Seems that I really need to install [tt]ksh[/tt]. In my distribution's CD is only [tt]dpksh[/tt] 5.2.14 ( Public Domain Korn Shell ). Ok, I know, that is just a "fake" one, but what do you think, could be significant differences between that and the original [tt]ksh[/tt] ?

Feherke.
 
pdksh should be fine, I have only noticed minor differences to "standard" ksh. The ones I can recall are:

pdksh supports brace expansion, Solaris ksh doesn't.

[tt]pdksh-5.2.14-22 $ echo blah{1,2,3}
blah1 blah2 blah3
pdksh-5.2.14-22 $

Solaris 8 ksh $ echo blah{1,2,3}
blah{1,2,3}
Solaris 8 ksh $[/tt]

In pdksh, variables read in a pipeline are not inherited by the parent shell. This may be due to Linux' handling of pipelines rather than pdksh itself... bash behaves in the same way:

[tt]pdksh-5.2.14-22 $ echo "blah" | read n ; echo $n

pdksh-5.2.14-22 $

Solaris 8 ksh $ echo "blah" | read n ; echo $n
blah
Solaris 8 ksh $[/tt]

pdksh doesn't support process substitution, which I really miss!:

[tt]pdksh-5.2.14-22 $ diff <(echo one) <(echo two)
ksh: syntax error: `(' unexpected
pdksh-5.2.14-22 $

Solaris 8 ksh $ diff <(echo one) <(echo two)
1c1
< one
---
> two
Solaris 8 ksh $[/tt]

These differences could be because Sun have stuck with the ksh88 standard and pdksh uses ksh93... or something? I haven't checked. Visit for the lowdown from the creator of ksh.

Annihilannic.
 
Interesting! I just downloaded ksh.2005-02-02.linux.i386 from Kornshell.com to a Linux system and it passed all three of these tests, so perhaps that's what you (and I!) should use:

[tt]ksh.2005-02-02.linux.i386 $ echo blah{1,2,3}
blah1 blah2 blah3
ksh.2005-02-02.linux.i386 $ diff <(echo one) <(echo two)
1c1
< one
---
> two
ksh.2005-02-02.linux.i386 $ echo blah | read n ; echo $n
blah
ksh.2005-02-02.linux.i386 $[/tt]

That proves my theory about Linux pipelines wrong as well...

It's possible that this version not normally included with Linux because it uses the AT&T CPL instead of GPL?

Annihilannic.
 
Hi all,

Thanks for the replies. I have used substr, but it doesn't look neat, if I could use something like the while loop example... but in ksh it doesn't work.

Thanks,
dbadmin
 
not at a shell prompt right now, but this is how I would try it:

echo "abcdefgh"|fold -w1|while read char
do
echo $char
done

HTH,

p5wizard
 
Another idea:

Code:
for char in $(echo abcdefgh | sed 's/\(.\)/\1 /g')
do
    echo $char
done

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top