[xubuntu-users] dash - looping through alphabet

Kevin O'Gorman kogorman at gmail.com
Mon Jul 7 04:29:54 UTC 2014


It looks to me like you need a while loop.  Dash is intended to be a subset
after all, and something had to be left out.

It can be a one-liner, as I gave before, or if you want spaces between the
letters (but not after the last one), you can use

i=1; while [ $i -lt 26 ] ; do  echo -n $( echo abcdefghijklmnopqrstuvwxy |
cut -c$i)' '; i=$((i+1)); done; echo z


On Sun, Jul 6, 2014 at 11:14 AM, Steve Litt <slitt at troubleshooters.com>
wrote:

> 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
>
>
> --
> xubuntu-users mailing list
> xubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/xubuntu-users
>



-- 
Kevin O'Gorman

programmer, n. an organism that transmutes caffeine into software.
Please consider the environment before printing this email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/xubuntu-users/attachments/20140706/31ddf18e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 441 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/xubuntu-users/attachments/20140706/31ddf18e/attachment.gif>


More information about the xubuntu-users mailing list