LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 03-31-2011, 11:16 AM   #31
arifsaha
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Rep: Reputation: 0
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
}
 
Old 04-09-2011, 04:19 AM   #32
konsolebox
Senior Member
 
Registered: Oct 2005
Location: Philippines
Distribution: Gentoo, Slackware, LFS
Posts: 1,526
Blog Entries: 5

Rep: Reputation: 98
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 Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota
Distribution: Slackware32-stable, Debian32-stable
Posts: 196

Original Poster
Rep: Reputation: 22
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 Alkaline; 07-08-2011 at 08:02 AM. Reason: code revision
 
  


Reply

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ls - Do not list full path only filename baddah Linux - General 6 03-06-2010 05:47 PM
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


All times are GMT -5. The time now is 11:13 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration