HP3000-L Archives

September 2000, Week 4

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Mark Bixby <[log in to unmask]>
Reply To:
Mark Bixby <[log in to unmask]>
Date:
Wed, 27 Sep 2000 11:02:30 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (46 lines)
Somebody asked via private e-mail for an explanation of what's happening in
this script, so here goes...

Mark Bixby wrote:
> A POSIX shell solution:
>
> nomad:/BIXBY/PUB/demo$ cat ./split
> #!/bin/sh
>
> TEMPVAR="[log in to unmask]"
>
> OLDIFS="$IFS"
> IFS=.
> set -- $TEMPVAR
> IFS="$OLDIFS"

The primary magic of splitting the string at the periods is being done via the
IFS environment variable.  From "man sh":

     IFS  contains a series of characters to be use as internal field
          separator characters.  During word expansion (see Word
          Expansion), the presence of any of these characters within a word
          causes that word to be split.  In addition, the shell uses these
          characters to separate values put into variables with the read
          command.  Lastly, the first character in the value of IFS
          separates the positional parameters in $* expansion.  By default,
          IFS contains space, tab, and newline.

The "set --" command does word expansion on the parameters that follow, and
then assigns the first word to $1, the second word to $2, etc.

Because IFS affects the fundamental parsing of the shell, you need to save &
restore the original value or else your script could behave REALLY strangely on
subsequent commands that are expecting whitespace in IFS.

> echo "file=$1"
> echo "group=$2"
> echo "account=$3"
>
> nomad:/BIXBY/PUB/demo$ ./split
> file=AI@J
> group=GROUP1
> account=ACCOUNT1
>
> - Mark B.

ATOM RSS1 RSS2