expanding dotted paths to absolute ones in bash or sh

I have a little script to help me manage a gallery of image files. It makes symbolic links to every file in and below the current directory, placing them in a target directory which is passed to the script as a parameter. Unfortunately, the script pukes when I pass a parameter that contains dotted paths.

$ linkall /home/joe/foo
$ linkall ~/foo

work beautifully. The latter one works because the parameter is expanded before it is passed.

But,

$ linkall ../foo

pukes.

Does anyone know of a way to expand the passed parameter into an absolute path? I've tried everything I can think of. I'm having a hard time even thinking of the right query to Google for. Whenever that happens, I look for a forum. :smiley: It seems to me that I'm probably looking for some code that gets used in a lot of scripts; basically boilerplate that I've just never come across yet.

Other than by strictly using absolute paths, I can't think of any workarounds off the top of my head.

I can post the script if that would help. It's 68 lines.

This is almost cheating, but how about...
cd $RELATIVE_PATH
ABSOLUTE_PATH=`/usr/bin/pwd`

It's a kludge...
It's a cheat...
It's portable...
And it works...
I love it!

I don't know why I didn't think of that little slight of hand. Maybe when I have 4000 posts, I'll think of them too.

With a little snugging up, the code plus the addition (plus the new comment) is now only one line longer than it was before. I think I can live with that. :slight_smile:

P.S.: If anyone knows of a "real" answer, I'd still be glad to hear it. I'm pretty sure my code (and the modification) is portable so maybe it doesn't matter.

P.P.S.: Thanks from a fellow Rocvillian! :cool:

I use this alias a lot:
alias realpath='readlink -f'

So in your script just do:

path=`realpath "$1"`

Thanks, Pixelbeat. I'm new to *nix but I gather "realpath" is standard equipment so I assume this should still be portable. I try to stay in the habit of writing portable code whenever I have the luxury (or whenever I'm getting paid by the hour :D).

I condensed your two lines into one and it has shortened my code by quite a few lines. Just in time, too, as I notice I was a little sloppy in my handling of error conditions. If only all of the users could be counted on to know what I had in mind.