Parsing out environment variable in c-shell

Parsing out environment variable in c-shell

Post by Joseph E. Schmalhofer II » Tue, 07 Oct 1997 04:00:00



c-shell script.
I have an environment variable that contains a directory structure. I'd
like to parse out the subdirectories into other variables for use in
some logic checking. Any ideas on how I might do this?

Example:
I have:
$DM_HOME = /dir/sub/sub_sub/sub_sub2
I want:
$DM_SUB = sub
$DM_SUB1 = sub_sub
$DM_SUB2 = sub_sub2

TIA
Joe Schmalhofer

 
 
 

Parsing out environment variable in c-shell

Post by Vladimir Smelyans » Tue, 07 Oct 1997 04:00:00



: c-shell script.
: I have an environment variable that contains a directory structure. I'd
: like to parse out the subdirectories into other variables for use in
: some logic checking. Any ideas on how I might do this?

: Example:
: I have:
: $DM_HOME = /dir/sub/sub_sub/sub_sub2
: I want:
: $DM_SUB = sub
: $DM_SUB1 = sub_sub
: $DM_SUB2 = sub_sub2

The solution is not shell specific. I'm using sh, hopefully you can easy
port it to csh.

DM_SUB2=`basename $DM_HOME`
DIRNAME1=`dirname $SD_HOME`
DM_SUB1=`basename $DIRNAME1`
DIRNAME2=`dirname $DIRNAME1`
DM_SUB=`basename $DIRNAME2`

and so on.

Vlad.

 
 
 

Parsing out environment variable in c-shell

Post by robbie robinett » Sat, 11 Oct 1997 04:00:00



Quote:> c-shell script.
> I have an environment variable that contains a directory structure. I'd
> like to parse out the subdirectories into other variables for use in
> some logic checking. Any ideas on how I might do this?

I would suggest that you investigate the dirname command.  You might try
something
like:

tmp=$DM_HOME
while [[ -n "$(dirname $tmp) ]] ; do
    tmp=$(basename $tmp)
    eval dm_sub$i=$tmp
done

--
---

The Kernel Group Inc.                http://www.tkg.com