Script to generate names

Parv parv at pair.com
Fri Feb 3 21:57:26 PST 2006


in message <20060203204323.GV1940 at merkur.atekomi.net>,
wrote Will Maier thusly...
>
> On Fri, Feb 03, 2006 at 11:08:04AM +0100, Kristian Vaaf wrote:
> > I'm looking for pointers on how to make a simple shell script that
> > will generate new names based on words (one word per line) from
> > two different files, and output these to a third file.
>
> How bout this? Works on OpenBSD's sh; I assume it works on Free's sh
> as well. Might take a while to run, though...
...
> for WORD1 in $(< ${LIST1}); do
...
> done

It looks like OpenBSD (3.6) sh is pdksh (see "Shell startup" section;
process substitution is in "Substitution" section, paragraph 6) ...

  http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=OpenBSD+3.6&format=html


Process substitution does not work in FreeBSD 6 /bin/sh ...

  # cat p
  set -x
  for i in $(< q); do echo "$i"; done

  # cat q
  polka
  bikini
  state

  # sh p
  +


... but in zsh ...

  # zsh p
  +/home/parv/p:2> i=polka
  +/home/parv/p:2> echo polka
  polka
  +/home/parv/p:2> i=bikini
  +/home/parv/p:2> echo bikini
  bikini
  +/home/parv/p:2> i=state
  +/home/parv/p:2> echo state
  state


Anyway, here is one solution for FreeBSD /bin/sh ...

  in_1="list1"
  in_2="list2"
  save="list3"

  [ -f "$save" ] && mv -f "$save" "$save--OLD"
  {
    while read word_1
    do
      while read word_2
      do
        printf "%s%s\n%s%s\n" \
          "$word_1" "$word_2" \
          "$word_2" "$word_1"

        #  If all the possible combinations of all the words are
        #  needed, remove or comment out the following "break".
        break

      done <"$in_2"
    done <"$in_1"
  } | sort -u >> "$save"


Kristian, try the above code with or without the "break",  and
exchanging "$in_1" & "$in_2".  If "$in_1" file is smaller than
"$in_2", then the script will end earlier (in comparison to reverse
situation).


  - Parv

--



More information about the freebsd-questions mailing list