Operating System - Linux
1753402 Members
7620 Online
108792 Solutions
New Discussion юеВ

Re: Expand a path passed to a script

 
SOLVED
Go to solution
Ivan Ferreira
Honored Contributor

Re: Expand a path passed to a script

Hi, me again, check this sample:

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

# If the arg starts with dot, then is relative
if [ $RELATIVE -gt 0 ]; then
if [ -d $APP_PATH/$1 ]; then
REALPATH=`cd $APP_PATH/$1;pwd`
else
FILEDIR=`dirname $APP_PATH/$1`
REALDIRPATH=`cd $FILEDIR;pwd`
FILENAME=`basename $APP_PATH/$1`
REALPATH=`echo "$REALDIRPATH/$FILENAME"`
fi
else
REALPATH=$1
fi

echo $REALPATH
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

Rodney,

Excellent solution!

Not having much experience with compiling programs for Unix, I compiled it with no options and it works perfectly. None of the tests I've run have failed (unless the user provides a bogus entry but then there are larger problems anyway).

This has the added convenience of completely encapsulating the code in a single command for use in my scripts, keeping the complexity of the scripts themselves to a minimum.

I'm still going to upgrade my perl and check out that solution but I'm going to be using this as the permanent fix.

Thanks!

Steve
Steve Tesch
Advisor

Re: Expand a path passed to a script

Ivan,

I appreciate the effort but what your suggesting is already as large as the brute force method I uploaded at the beginning of this thread.

As you can see from the previous post, Rodney posted a simple C program that will wrap it all up nicely for me.

Thanks,

Steve
James R. Ferguson
Acclaimed Contributor

Re: Expand a path passed to a script

Hi (again) Steve:

...and I can't resist noting that Rodney's C-code leverages the 'realpath()' function that the perl module I suggested you use does. :-;

Regards!

...JRF...
Steve Tesch
Advisor

Re: Expand a path passed to a script

As a final follow up to the perl solution. I found that there are actually two versions of perl currently loaded on my system, version 5 and a newer version 5.6.1.

I was able to get the perl solution to work by pointing it to the newer version as shown here:

#!/bin/ksh

WHERE=`perl5.6.1 -le 'use Cwd qw(realpath);print realpath $ARGV[0];' $1`
echo $WHERE

Actually, I will likely use the perl solution in some cases and the compiled C solution in others.

The difference is, as provided above, the perl solution will generate an error if an invalid directory is passed, while the C solution will not.

Each solution has it's own merits depending on what exactly you're trying to accomplish and if/how you're going to handle invalid paths.

I want to thank everyone on this forum for their prompt responses and dedication to helping me find a workable solution. This has been a very enjoyable first experience.

Steve