Tech Support > Operating Systems > Linux / Variants > Bash Scripting
Bash Scripting
Posted by A. S. Budden on November 20th, 2003


Hi there,

I'm trying to write a bash script. Most of it has gone fine, but I need
to get the full filename of a command line argument.

For example, if the user types:

myscript bob.txt

in the directory /home/al, I need /home/al/bob.txt. Similarly if they
type myscript ../../etc/bob.txt or /usr/share/bob.txt etc.

I know I can use dirname and pwd, but I it would be quite messy to deal
with situations like:

cd /home/al/documents
myscript ../../someoneelse/test/bob.txt

as from dirname and pwd I'd get
/home/al/documents/../../someoneelse/test/bob.txt instead of
/home/someoneelse/test/bob.txt

Can anyone give me any hints as to how to get round this please?

Many thanks in advance,

Al

Posted by Chris F.A. Johnson on November 20th, 2003


On Thu, 20 Nov 2003 at 11:55 GMT, A. S. Budden wrote:
file=../../someoneelse/test/bob.txt

fullpath=$(cd ${file%/*} && echo "$PWD/${file##*/}")

--
Chris F.A. Johnson http://cfaj.freeshell.org
================================================== =================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Posted by Floyd Davidson on November 20th, 2003


"A. S. Budden" <newsreader.mail@totalise.co.uk> wrote:
This will give you the path without the "../../"

DIRNAME=$(cd $(dirname "$1") && pwd)

Hence, your myscript file might look like this:

#!/bin/bash
#

echo "Command line arg: \"${1}\""

BASENAME=$(basename "${1}")
DIRNAME=$(cd $(dirname "${1}") && pwd)

echo "Directory: ${DIRNAME}"
echo "Full path: ${DIRNAME}${BASENAME}"


--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com

Posted by A. S. Budden on November 20th, 2003


Thus spake Chris F.A. Johnson:
Thanks! This works. I had to make a small alteration, so I thought I'd
post it here for anyone else's benefit. My programming is a bit weak,
so sorry if this is a naff solution. Deals with the situation where the
file is in the current working directory e.g. myscript bob.txt

incwd=`echo $filearg | sed 's#.*/.*#no#'`
if [ "$incwd" = "no" ]
then
fname=$(cd ${filearg%/*} && echo "$PWD/${filearg##*/}")
else
fname=$PWD/$filearg
fi

Posted by Floyd Davidson on November 20th, 2003


"A. S. Budden" <newsreader.mail@totalise.co.uk> wrote:
fname="$(cd $(dirname "${filearg}") && pwd)/$(basename "${filearg}")"

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com

Posted by A. S. Budden on November 20th, 2003


Thus spake Floyd Davidson:
That's a very neat solution! Thank you.

Al

Posted by Chris F.A. Johnson on November 20th, 2003


On Thu, 20 Nov 2003 at 14:35 GMT, A. S. Budden wrote:
Theres no need for an external command (sed):

case $file in
*/*) fname=$(cd ${filearg%/*} && echo "$PWD/${filearg##*/}") ;;
*) fname=$PWD/$filearg ;;
esac

--
Chris F.A. Johnson http://cfaj.freeshell.org
================================================== =================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Posted by Floyd Davidson on November 20th, 2003


"A. S. Budden" <newsreader.mail@totalise.co.uk> wrote:
Yeah, but I blew it. That last line should read

echo "Full path: ${DIRNAME}/${BASENAME}"

with a / between the directory name and the basename.

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com

Posted by A. S. Budden on November 20th, 2003


Thus spake Floyd Davidson:
Yes, I realised this, about 5 seconds after I'd sent my post!

Thanks again,

Al


Similar Posts