[xubuntu-users] dash - looping through alphabet
Steve Litt
slitt at troubleshooters.com
Sun Jul 6 18:14:41 UTC 2014
On Sun, 06 Jul 2014 07:26:34 +0200
Ralf Mardorf <ralf.mardorf at rocketmail.com> wrote:
> On Sat, 2014-07-05 at 19:49 -0400, Steve Litt wrote:
> > I couldn't get anything to work without putting spaces between every
> > letter from a to z.
>
> Thank you,
>
> but if this is needed, then I don't want to use a loop, but do it this
Wouldn't blame you a bit, but I can make it much easier. Read on...
> > Would you consider a C solution?
>
> No, it should be a shell script and for this script I prefer dash over
OK, how bout a partial C solution that gives Unix a much-needed "do one
thing and do it well" command? The following rng executable takes a
single, two letter argument, and returns a space-delimited range
from the first letter to the second letter, inclusive:
=============================================================
#include<unistd.h>
int main(int argc, char *argv[]){
char buf[10];
char start = argv[1][0];
char end = argv[1][1];
char i;
for(i=start; i <= end; i++){
buf[0] = i;
write(1, buf, 1);
if(i < end)
write(1, " ", 1);
}
return 0;
}
=============================================================
Because the preceding doesn't include <stdio.h>, it's fairly small,
6854 bytes. So rng eg returns "e f g".
So, when you iterate in your shellscript, do this:
=============================================================
#!/bin/dash
for c in `./rng az`; do
echo $c
# Do whatever else with $c
done
=============================================================
Works equally well in dash and bash. Obviously, in real life you put
rng in a directory on the path and get rid of the "./".
As far as pure shellscript solutions, I tried a lot of things,
including the well documented and highly spoken of:
printf "%03o" ascii_number
and none worked. The "%03o" thing just printed the number again.
Unix really should have a chr command. Rather than making a chr
command, I just made a rng command that makes your shellscript loop
logic trivial.
SteveT
Steve Litt * http://www.troubleshooters.com/
Troubleshooting Training * Human Performance
More information about the xubuntu-users
mailing list