[ale] bash, full filename

Larry Richardson larryr2 at bellsouth.net
Sun Nov 30 07:39:42 EST 2003


Maybe this little script will help.  Change the output format to suit 
your needs.

###############################
# fullpath.sh
# Given a relative path, create a full path.

# check for one arg
if [ $# -ne 1 ]; then
	echo "Usage : $0 {relative path}"
	exit -1
fi

# check to see if $1 exists
if [ -e $1 ]; then
	B=`basename $1`
	P=`dirname $1`
	#  echo BASE:$B  PATH:$P
	cd $P
	
	if [ `pwd` != "/" ]
	then
		FULLNAME=`pwd`/$B
	else
		FULLNAME=/$B
	fi
	
	echo $FULLNAME
else
	echo "$1 - Not a regular file"
	exit -1
fi
##########################################


Larry Richardson
larryr2 at bellsouth.net


> David Corbin wrote:
> 
>> On Saturday 29 November 2003 18:46, Geoffrey wrote:
>>
>>> David Corbin wrote:
>>>
>>>> How can I take a filename (which may or may not be relative) and
>>>> convert it to be a fully qualified filename, in bash?
>>>
>>>
>>> type vi |awk '{print $NF}'
>>>
>>> This will only work if it's in y
> 
> 
> 
> our path.  If not, you're going to have
> 
>>> to use locate or find.
>>
>>
>>
>> I don't think that comes close to what I want.  (But thanks for trying).
>>
>> I want to be able to convert:
>>
>> foobar          to  /home/dcorbin/foobar  (if I'm in /home/dcorbin)
>> /etc/passwd  to  /etc/passwd
>> ../fred/foobar to  /home/fred/foobar (if I'm in /home/dcorbin)
>>
>> PATHs  (in the sense of your example) are just not a factor.
> 
> 
> My mistake, I assumed (incorrectly) you were looking for full paths to 
> executables.
> 
> I don't think you're going to find a single tool for the examples you've 
> outlined.  Maybe we need to better understand the problem, why is it you 
> want the full path to these files?  What are all the possible scenarios?
> 
> It's likely you'll need to throw a script together to handle the various 
> possibilities:
> 
> for the first example (foobar) a simple find will locate it.  For the 
> filename that's already fully qualified (/etc/passwd) simply checking 
> for the leading '/' will resolve this problem.  For the filename 
> beginning with the '..' you'd check for the '..' if it's there you'd cd 
> to the previous directory and list the file.  If there's multiple '..', 
> you'll have to deal with a bit of recursion.
> 
> So, really, without knowing the purpose of the problem, it's hard to 
> know what all the possible examples might be:
> 
> /etc/../etc/passwd
> 
> ../../usr/lib
> 
> foobar when it's /home/dcorbin/foobar
> 
> foobar when it's /home/fred/foobar
> 




More information about the Ale mailing list