Operating System - Linux
1752770 Members
4849 Online
108789 Solutions
New Discussion юеВ

Re: Expand a path passed to a script

 
SOLVED
Go to solution
Steve Tesch
Advisor

Re: Expand a path passed to a script

First, my apologies, I addressed a previous reply to Victor when it should have been addressed to Ivan.

Leif - Thanks for the input but dirname and basename do not expand relative paths. They were one of my first attempts.

James - I appricate the quick response. I'll look into updating my perl and let you know how it turns out.

Thanks to all,

Steve
Ivan Ferreira
Honored Contributor

Re: Expand a path passed to a script

Still trying:

APP_PATH=/database/app
FULLPATH=`cd $APP_PATH/$1; pwd`
echo $FULLPATH
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor

Re: Expand a path passed to a script

And going, and going...


APP_PATH=/var/adm/syslog
RELATIVE=`echo $1 |grep "^\."|wc -l`

# If the arg starts with dot, then is a relative path, else just use the specified path
if [ $RELATIVE -gt 1 ]; then
REALPATH=`cd $APP_PATH/$1;pwd`
fi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: Expand a path passed to a script

Hi Steve:

The perl update is quick and simple and so, so worthwhile. Perl 5.8.x has incorporated lots of good things into its core.

Regards!

...JRF...
Steve Tesch
Advisor

Re: Expand a path passed to a script

Ivan,

Your solution works if the user passes a directory, and is very similar to the method I uploaded. However if they are passing a filename, it gets an error.

I need a solution for both possibilities.

Thanks for the continued effort.

Steve
Ivan Ferreira
Honored Contributor

Re: Expand a path passed to a script

Sorry, I did not see your previous reply addressed to victor (me) about the file.

Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Steve Tesch
Advisor

Re: Expand a path passed to a script

Ivan,

Your new solution works in most cases but not all. If you pass it a sub-directory of the current directory, it will return the current directory, instead of the full path to the sub-directory.

Your first solution did not have this problem. Also, this solution still can't handle a filename.

Thanks again,

Steve
Leif Halvarsson_2
Honored Contributor

Re: Expand a path passed to a script

CUR=/abc/def
DIR=$(dirname ../xyz/abc.txt)
FILE=$(basename ../xyz/abc.txt)

cd $DIR
ABSOLUTE=$(pwd)

FULL_PATH=${ABSOLUTE}/${FILE}

it should work also with an absolute path or a filename only

Rodney Hills
Honored Contributor
Solution

Re: Expand a path passed to a script

You could compile the following c program that takes a path as its only argument and returns the real path. (Even compiles with the brain dead supplied cc compiler).

#include
main(argc,argv,envp)
int argc;
char *argv[],*envp[];
{
char realp[255];
(void)realpath(argv[1],realp);
printf("%s\n",realp);
exit(0);
}

HTH

-- Rod Hills
There be dragons...
Steve Tesch
Advisor

Re: Expand a path passed to a script

Leif,

Very close! However, if the user passes a path or filename that is already in absolute format (i.e. /home), your solution doubles the initial forward slash (i.e. //home). If they pass the root directory, it returns three forward slashes (i.e. ///).

Thanks for the effort,

Steve