Operating System - Linux
1828352 Members
3254 Online
109976 Solutions
New Discussion

Re: PATH value in a variable

 
Jose Mosquera
Honored Contributor

PATH value in a variable

Hi pals,

Please look this simple script content:
MAIN_PATH=/usr/local/bin
SUPPLEMENTARY_PATH=../..
FULL_PATH=$MAIN_PATH/$SUPPLEMENTARY_PATH
echo $FULL_PATH

/usr/local/bin/../..

How can I obtain (on-the-fly) the real/final path info content into $FULL_PATH? In this case must be "/usr".

Please omit the possibility to move to $FULL_PATH and to obtain the value of $PWD, I need to know the real/final content before moving.

For those that have a solution, please think that it would happen in this deliberate erroneous case of variable definition:
SUPPLEMENTARY_PATH=../../../../../..

Rgds.
6 REPLIES 6
Pete Randall
Outstanding Contributor

Re: PATH value in a variable

I'm not sure what you're really asking here but your syntax is wrong for a path. It should be

FULL_PATH=$MAIN_PATH:$SUPPLEMENTARY_PATH


Pete

Pete
Jose Mosquera
Honored Contributor

Re: PATH value in a variable

Pete,

You are wrong. The final PATH must be:
FULL_PATH=$MAIN_PATH/$SUPPLEMENTARY_PATH

Rgds.
Jonathan Fife
Honored Contributor

Re: PATH value in a variable

If you can access the MAIN_PATH and SUPPLEMENTARY_PATH variables directly, I'd do some sort of loop counting the number of '..'s in SUPPLEMENTARY_PATH and using the 'dirname' command to remove directories from MAIN_PATH. Something like:

MAINPATH=/usr/local/bin
SUPPPATH=../..

while [ $(echo $SUPPPATH | grep -c "\.\.") -gt 0 ]; do
SUPPPATH=$(dirname $SUPPPATH)
MAINPATH=$(dirname $MAINPATH)
done

echo $MAINPATH

There's probably an elegant perl script that will do the same...
Decay is inherent in all compounded things. Strive on with diligence
Peter Nikitka
Honored Contributor

Re: PATH value in a variable

Hi,

try with perl:

perl -e 'use Cwd 'abs_path'; print abs_path($ARGV[0]) . "\n";' PATHNAME

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: PATH value in a variable

Ups,

though functional, I recommend stripping some quotes:

perl -e 'use Cwd abs_path; print abs_path($ARGV[0]) . "\n";' PATHNAME

Sorry.

Additionally, here is a awk-function that did this as well, if I remember correctly:

awk 'function pathexp (name) { numdir=split(name,p,"/")
ndir_new=0
for(i=1;i<=numdir;i++) {
if(p[i]==".") continue
if(p[i]=="..") {if(ndir_new) {ndir_new--;continue}}
np[++ndir_new]=p[i]
}
if(!ndir_new) return(name)
new_name=np[1]
for(i=2;i<=ndir_new;i++) new_name=new_name"/"np[i]
return(new_name)
}
{print pathexp($1)}'

This expects your pathname at stdin.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Enrico P.
Honored Contributor

Re: PATH value in a variable

Hi,
you can set your FULL_PATH variable:

FULL_PATH=`cd $MAIN_PATH/;cd $SUPPLEMENTARY_PATH;pwd`

Enrico