[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Setting Multiple Shell Variables from One Run of awk



On Tue, Sep 30, 2008 at 01:48:46PM -0500, Martin McCormick wrote:
> 	Right now, I have a shell script that does the following:
> 
> hostname=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $1}'`
> domain=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $2}'`
> top0=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $3}'`
> top1=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $4}'`

eval can be used to set more than one variable in a single command. This
is used by e.g. ssh-agent.

# Using a separate function for slightly more clarity
domain_parser() {
	echo $1 | sed -e 's/^\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)$/hostname="\1" ; domain="\2"; top0="\3"; top1="\4"/'
}
eval `domain_parser $NEWDEV`


But then, why do you assume a domain name has exactly four elements?

-- 
Tzafrir Cohen         | tzafrir@jabber.org | VIM is
http://tzafrir.org.il |                    | a Mutt's
tzafrir@cohens.org.il |                    |  best
ICQ# 16849754         |                    | friend


Reply to: