LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2011, 11:16 AM   #31
arifsaha
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Rep: Reputation: 1
Lightbulb Yes even another code for absolute path


How about this one to get absolute path? Simpler, even local variables are not needed. Little modification will get you the file base name if needed, as soon as you can define its behaviour when you give directory as argument (should it be the last directory or empty string?).

Code:
function abspath {
	if [[ -d "$1" ]]
	then
		pushd "$1" >/dev/null
		pwd
		popd >/dev/null
	elif [[ -e $1 ]]
	then
		pushd $(dirname $1) >/dev/null
		echo $(pwd)/$(basename $1)
		popd >/dev/null
	else
		echo $1 does not exist! >&2
		return 127
	fi
}
 
1 members found this post helpful.
Old 04-09-2011, 04:19 AM   #32
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
you can try mine as well:
Code:
function getabspath {
	local -a T1 T2
	local -i I=0
	local IFS=/ A

	case "$1" in
	/*)
		read -r -a T1 <<< "$1"
		;;
	*)
		read -r -a T1 <<< "/$PWD/$1"
		;;
	esac

	T2=()

	for A in "${T1[@]}"; do
		case "$A" in
		..)
			[[ I -ne 0 ]] && unset T2\[--I\]
			continue
			;;
		.|'')
			continue
			;;
		esac

		T2[I++]=$A
	done

	case "$1" in
	*/)
		[[ I -ne 0 ]] && __="/${T2[*]}/" || __=/
		;;
	*)
		[[ I -ne 0 ]] && __="/${T2[*]}" || __=/.
		;;
	esac
}
Code:
getabspath "/path/.././././to/somewhere/"
echo "$__"

getabspath ".././././....//path/.././././to/somewhere/with/somefile.ext"
echo "$__"
note that the path or files does not need to exist.

the function can also be modified to accept another argument which could represent the name of the variable which will hold the result. i.e. like
Code:
getabspath <path> <variable_name>

Last edited by konsolebox; 04-09-2011 at 04:24 AM.
 
Old 07-07-2011, 05:20 PM   #33
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Manjaro, Debian64 stable
Posts: 528

Original Poster
Rep: Reputation: 167Reputation: 167
Calling a binary from a bash script can sometimes be preferred. Here's some C code which can be compiled using gcc -Wall -o getpath getpath.c

Code:
//      getpath.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{	
	if (argc != 2)
		{
			printf("Getpath takes one filename or directory as a parameter\n");
			exit (1);
		}
	
	char AbsPath[strlen(argv[1] + 1)];
	realpath(argv[1],AbsPath);
	printf("%s\n",AbsPath);
	return 0;
}
So if desired, copy the code above to a plain text file called getpath.c and compile it. Then use "./getpath <filename>" If you put getpath is in your path somewhere, the ./ can be omitted.

Last edited by Andy Alt; 07-08-2011 at 08:02 AM. Reason: code revision
 
Old 06-19-2012, 03:27 AM   #34
fatso83
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Rep: Reputation: Disabled
Thumbs up Thanks for the script!

The script from arifsaha was without doubt leaps and bounds better than any of the others; clean, simple and cross-platform compatible. Works just as well on BSD as on GNU based toolsets. I will try to give credit whenever I use it - which I suspect will be plenty more than the three applications so far :-)

Quote:
Originally Posted by arifsaha View Post
How about this one to get absolute path? Simpler, even local variables are not needed.
Code:
function abspath {
	if [[ -d "$1" ]]
	then
		pushd "$1" >/dev/null
		pwd
		popd >/dev/null
	elif [[ -e $1 ]]
	then
		pushd $(dirname $1) >/dev/null
		echo $(pwd)/$(basename $1)
		popd >/dev/null
	else
		echo $1 does not exist! >&2
		return 127
	fi
}
 
Old 06-19-2012, 03:34 AM   #35
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
just one comment: instead basename and dirname you can use parameter substitution:
Code:
${var##*/}  # <== basename
${var%/*}   # <== dirname







_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 06-19-2012, 10:01 AM   #36
fatso83
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Rep: Reputation: Disabled
Lightbulb Readability

I mentioned clean and simple; for the uninitiated, that looks like voodo I prefer verbosity over terseness if the terseness comes with a reduction in readability.
 
Old 06-19-2012, 11:11 AM   #37
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
actually it is not really so simple: using parameter substitution will be processed inside the shell, using basename and dirname will fork another task, will open pipe therefore it costs more, much more. If you were familiar with bash enough you should not speak about readability problems in such cases....
 
1 members found this post helpful.
Old 06-19-2012, 11:41 AM   #38
fatso83
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Rep: Reputation: Disabled
Thanks for the info

Quote:
Originally Posted by pan64 View Post
If you were familiar with bash enough you should not speak about readability problems in such cases...
I have never claimed to be a bash expert, and for the case of the original script, one did not need to be either, which is why I liked it For most people hacking together small scripts to get some job done it does not pay to invest time into learning the ins and outs of the various shell, which is why shell scripts should be mostly self-explanatory. Or just heavily commented But the info on why parameter substitution might prove a better fit was very interesting (for me, at least), and put the matter into another perspective. Thanks for the info - nice to know!
 
Old 06-26-2012, 07:05 PM   #39
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by fatso83 View Post
The script from arifsaha was without doubt leaps and bounds better than any of the others; clean, simple and cross-platform compatible. Works just as well on BSD as on GNU based toolsets. I will try to give credit whenever I use it - which I suspect will be plenty more than the three applications so far :-)
Unfortunately the directories need to exist in order for that to work. Also, you'll have to use external commands which is rather slower and require dependencies.
 
Old 06-26-2012, 07:09 PM   #40
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Check out my full post about getting absolute pathnames in all general shells and also for a binary format.

http://www.linuxquestions.org/questi...-scripts-3956/
 
Old 07-06-2012, 12:10 AM   #41
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Manjaro, Debian64 stable
Posts: 528

Original Poster
Rep: Reputation: 167Reputation: 167
The realpath command was added to coreutils, as of version 8.15, I believe. Not the best solution yet -- not all *nix distros have it installed yet, and I couldn't say anything about Macs.

See also: GNU core utilities
 
Old 07-06-2012, 10:24 AM   #42
fatso83
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by Andy Alkaline View Post
The realpath command was added to coreutils, as of version 8.15, I believe. Not the best solution yet -- not all *nix distros have it installed yet, and I couldn't say anything about Macs.

See also: GNU core utilities
Seems good - albeit it does not exist on Mac or on any of our servers (typically 3-5 years behind). So a cross-platform script comes in handy
 
Old 01-10-2013, 06:21 AM   #43
fatso83
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Rep: Reputation: Disabled
Parameter substitution not working?

I just tried the suggestions from pan64, but I was unable to make it work. Can anyone change the script above and make it work with parameter substitution? I have also searched the man page for bash on the "var##" pattern, and I cannot find anything referencing it.

Quote:
Originally Posted by pan64 View Post
just one comment: instead basename and dirname you can use parameter substitution:
Code:
${var##*/}  # <== basename
${var%/*}   # <== dirname







_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 01-10-2013, 06:39 AM   #44
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
please open a new thread and specify your problem, or at least show us what you tried:
Code:
$ var=/a/v/b/n/f/e
$ echo ${var##*/}
e
$ echo ${var%/*}
/a/v/b/n/f
$
 
Old 01-10-2013, 09:11 AM   #45
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I wonder how my script could not provide the one you require that you have to go all the troubles.
 
  


Reply

Tags
bash, bin, can, command substitution, console, link, mv, path, php, recycle, rm, scripts, sed, symbolic, trash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ls - Do not list full path only filename baddah Linux - General 12 06-17-2013 12:21 AM
bash - return home path babag Programming 2 06-01-2008 04:59 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
php mail script return path richard22 Linux - Software 1 08-22-2003 04:10 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration