expanding dotted paths to absolute ones in bash or sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting expanding dotted paths to absolute ones in bash or sh
# 1  
Old 07-12-2005
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. Smilie 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.

Last edited by TanRanger; 07-12-2005 at 07:18 PM..
# 2  
Old 07-12-2005
This is almost cheating, but how about...
cd $RELATIVE_PATH
ABSOLUTE_PATH=`/usr/bin/pwd`
# 3  
Old 07-12-2005
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. Smilie

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! Smilie
# 4  
Old 07-13-2005
I use this alias a lot:
alias realpath='readlink -f'

So in your script just do:

path=`realpath "$1"`
# 5  
Old 07-13-2005
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 Smilie).

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

[BASH] eval command not expanding variables as expected.

Hi Guys, I wrote a collection of bash functions years ago and now need to use them again but I'm getting some error messages when eval tries to expand the variables names. I recollect that I used the shopt command to set one of the options but I can't quite remember the command that I... (8 Replies)
Discussion started by: ASGR
8 Replies

2. UNIX for Beginners Questions & Answers

Bash argument not expanding in script

I pass an argument to bash as run. The first command in green executes as expected, however the second in blue fails as the $run does not expand. I tried to escape the variable with \ thinking the quotes were making the literal translation and also "${run}" but both did not work to expand the... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Show only the filenames under a directory without relative and absolute paths.

I am able to list all the filenames under a directory & its sub-directories except blent.tar on Linux find "/tmp/" -type f | grep -v blent.tar | rev | cut -d '/' -f1 | rev Desired Output: THIRDPARTYLICENSEREADME.txt javaws libjavaplugin_oji.so libjavaplugin_oji.so... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

Is it possible to change paths inside a bash script?

i have some script with some paths inside it. The idea is to some files which is on desktop copy and move to another location. Problem is that inside script is similar to this: cp test1.zip /root/help/ because I allways have another zip files, does it possible to have some input which ask me... (18 Replies)
Discussion started by: tomislav91
18 Replies

5. Solaris

exclude absolute paths when extracting Tar file?

Hi, How do I extract data from TAR excluding absolute paths for Tar? (Solaris) Thanks (3 Replies)
Discussion started by: zam
3 Replies

6. Shell Programming and Scripting

how to remove absolute paths from zip archive

Hi, I need to write an bash script which works like it can copy files from remote machine through ssh to the server where script is running in zip format with the structure i want. I don't want to get absolute path in zip archive. Please let me know how it can be possible. ssh... (4 Replies)
Discussion started by: mirfan
4 Replies

7. UNIX for Dummies Questions & Answers

Absolute and Relative Paths?

Can someone cofirm that I have got the paths correct here? :confused: $PATH_TO_TMP_DIR='/tmp'; #$PATH_TO_TMP_DIR='home/tmp'; $PATH_TO_YOUR_IMG_DIR = '/temp_images'; #$PATH_TO_YOUR_IMG_DIR = 'home/public_html/Midwich/temp_images'; Thanks (1 Reply)
Discussion started by: stubie
1 Replies

8. UNIX for Dummies Questions & Answers

Expanding variables with Ed Bash 3.2.33

Hi, The following code finds the line containing fruits in test.txt and replaces instances of apple with banana. ed -s test.txt <<< $'/fruits/s/apple/banana/g\nw' What I want to do is put variables in the place of fruits, apple and banana. I have tried replacing ' with " to get... (2 Replies)
Discussion started by: de_la_espada
2 Replies

9. AIX

Is there a way to list all the functions dotted in to the env?

Hi People, Please advise if there is a command to retrieve the list of functions (user-defined) available at any certain point? Cheers (1 Reply)
Discussion started by: easwam
1 Replies

10. Post Here to Contact Site Administrators and Moderators

Dotted thread icons

Can you guys please enable the "dotted" icon option, so that the thread icon for a thread in which a user has posted in will appear with a dot in it? Thanks, Aaron (2 Replies)
Discussion started by: Spetnik
2 Replies
Login or Register to Ask a Question