absolute pathnames (with bash)?

absolute pathnames (with bash)?

Post by Michael A. Mill » Sat, 05 Apr 2003 04:01:05



I have a script that prepares an input file for another job that
will run from a different directory.  Since part of that input
file includes file names, I want include the full absolute path
name.  Can anyone suggest a way to get the full, absolute path to
a file?  The dirname command sometimes works, but it will return
a relative path as well.  I always want the absolute path.

Mike

 
 
 

absolute pathnames (with bash)?

Post by Michael Heimin » Sat, 05 Apr 2003 05:06:18



Quote:> I have a script that prepares an input file for another job that
> will run from a different directory.  Since part of that input
> file includes file names, I want include the full absolute path
> name.  Can anyone suggest a way to get the full, absolute path to
> a file?  The dirname command sometimes works, but it will return
> a relative path as well.  I always want the absolute path.

Perhaps something like this.

FNAME="`pwd`/filename"

--
Michael Heiming

Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of SPAM

 
 
 

absolute pathnames (with bash)?

Post by Michael A. Mill » Sat, 05 Apr 2003 05:21:25


    > Perhaps something like this.

    > FNAME="`pwd`/filename"

That only works if I start with a relative path.  For an absolute
path, that fails:

echo `pwd`/usr/local
/home/mm/usr/local

Maybe one way is to check to see if the first character of the
filename is '/'.  If it is, leave it alone.  If it is not,
prepend `pwd`.

Mike

 
 
 

absolute pathnames (with bash)?

Post by Heiner Steve » Sat, 05 Apr 2003 04:44:16


 > I have a script that prepares an input file for another job that
 > will run from a different directory.  Since part of that input
 > file includes file names, I want include the full absolute path
 > name.  Can anyone suggest a way to get the full, absolute path to
 > a file? [...]

I don't know if it works with BASH, but for ksh I'm using
the following function:

# resolvepath - print absolute path name for program
function resolvepath {
    typeset prog=$1
    typeset path

    [[ -e $prog ]] || return 1

    case "$prog" in
        /*)     path=$prog;;
        *)      path=$PWD/$prog;;
    esac

    dir=${path%/*}
    file=${path##*/}
    dir=$(cd "$dir"; pwd)

    echo "$dir/$file"
    return 0

Quote:}

Heiner
--
 ___ _

\__ \  _/ -_) V / -_) ' \    Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_|   http://www.shelldorado.com/
 
 
 

absolute pathnames (with bash)?

Post by Chris F.A. Johnso » Sat, 05 Apr 2003 05:52:21




>     > Perhaps something like this.

>     > FNAME="`pwd`/filename"

> That only works if I start with a relative path.  For an absolute
> path, that fails:

> echo `pwd`/usr/local
> /home/mm/usr/local

> Maybe one way is to check to see if the first character of the
> filename is '/'.  If it is, leave it alone.  If it is not,
> prepend `pwd`.

case $filename in
  /*) FNAME=$filename ;;
  *) FNAME=`pwd`/$filename ;;
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

 
 
 

absolute pathnames (with bash)?

Post by David Thompso » Sat, 05 Apr 2003 16:33:15



Quote:> I have a script that prepares an input file for another job that
> will run from a different directory.  Since part of that input
> file includes file names, I want include the full absolute path
> name.  Can anyone suggest a way to get the full, absolute path to
> a file?  The dirname command sometimes works, but it will return
> a relative path as well.  I always want the absolute path.

Turns out this snippet of mine that made use of 'pwd -P'
(to resolve symbolic links) will do exactly what you want.
Given $FILE, you want to guarantee that $FILE contains a
fully-pathed filename,

  DIR=$(dirname $FILE)
  NEW=$(cd "$NEW" 2> /dev/null && pwd -P)
  if [[ -n "$NEW" ]] ; then
    FILE="$NEW/${FILE##*/}"
  fi

--
David Thompson

 
 
 

absolute pathnames (with bash)?

Post by David Thompso » Sat, 05 Apr 2003 16:44:17



Quote:> Turns out this snippet of mine that made use of 'pwd -P'
> (to resolve symbolic links) will do exactly what you want.
> Given $FILE, you want to guarantee that $FILE contains a
> fully-pathed filename,

>   DIR=$(dirname $FILE)
>   NEW=$(cd "$NEW" 2> /dev/null && pwd -P)

               ^^^
typo  -->     $DIR

My Exceed cut&paste in Windows wasn't working, so I
retyped by hand, painfully aware that my fingers would
betray my brain and*up somewhere.  ;)

Quote:>   if [[ -n "$NEW" ]] ; then
>     FILE="$NEW/${FILE##*/}"
>   fi

Actually, you only need to use a single variable;
my use of variable $NEW above is not necessary,

  DIR=$(dirname $FILE)
  DIR=$(cd "$DIR" 2> /dev/null && pwd -P)
  if [[ -n "$DIR" ]] ; then
    FILE="$DIR/${FILE##*/}"
  fi

--
David Thompson

 
 
 

absolute pathnames (with bash)?

Post by Chris F.A. Johnso » Sat, 05 Apr 2003 16:51:19




>> I have a script that prepares an input file for another job that
>> will run from a different directory.  Since part of that input
>> file includes file names, I want include the full absolute path
>> name.  Can anyone suggest a way to get the full, absolute path to
>> a file?  The dirname command sometimes works, but it will return
>> a relative path as well.  I always want the absolute path.

> Turns out this snippet of mine that made use of 'pwd -P'
> (to resolve symbolic links) will do exactly what you want.
> Given $FILE, you want to guarantee that $FILE contains a
> fully-pathed filename,

>   DIR=$(dirname $FILE)

   If you have $( ), you also have ${FILE%/*}; no need for an external
   command.

Quote:>   NEW=$(cd "$NEW" 2> /dev/null && pwd -P)

   The -P option to pwd is not standard (but is in [pd]ksh and bash).

Quote:>   if [[ -n "$NEW" ]] ; then
>     FILE="$NEW/${FILE##*/}"
>   fi

--
    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
 
 
 

absolute pathnames (with bash)?

Post by David Thompso » Sat, 05 Apr 2003 17:08:17




> >   DIR=$(dirname $FILE)

>    If you have $( ), you also have ${FILE%/*}; no need
>    for an external command.

Well, yes, but you'd have to handle fringe cases
yourself, ie, what if $FILE has no slashes?

  $ FILE=xxx
  $ echo ${FILE%/*}
  xxx
  $ dirname $FILE
  .

If want to avoid dirname, you'd have to do something like,

  DIR=${FILE%/*}
  [[ "$DIR" == "$FILE" ]] && DIR="."

Despite the extra process (which I'll never cry about ;)
the use of dirname neatly encapsulates and handles these
issues.

--
David Thompson

 
 
 

absolute pathnames (with bash)?

Post by Chris F.A. Johnso » Sat, 05 Apr 2003 19:30:20





>> >   DIR=$(dirname $FILE)

>>    If you have $( ), you also have ${FILE%/*}; no need
>>    for an external command.

> Well, yes, but you'd have to handle fringe cases
> yourself, ie, what if $FILE has no slashes?

>   $ FILE=xxx
>   $ echo ${FILE%/*}
>   xxx
>   $ dirname $FILE
>   .

> If want to avoid dirname, you'd have to do something like,

>   DIR=${FILE%/*}
>   [[ "$DIR" == "$FILE" ]] && DIR="."

    Or (very slightly faster in my tests):

case $FILE in
     "") DIR="/"
     */*) DIR=${FILE%/*} ;;
     *) DIR="." ;;
esac

    Or you can duplicate the full POSIX dirname without any external
    commands:

dirname ()
{
    fn_path=$1

    ## For POSIX compliance only; I don't like it
    while case $fn_path in
      */) fn_path=${fn_path%/} ;;
      *) false ;;
    esac; do :; done

    case $fn_path in
        "")
            DIR='/'
        ;;
        */*)
            DIR="${fn_path%/*}"
        ;;
        *)
            DIR='.'
        ;;
    esac

    ### For speed, comment out the echo, and refer to $DIR directly
    ### rather than using dir=`dirname $FILE`
    echo "$DIR"

Quote:}
> Despite the extra process (which I'll never cry about ;)
> the use of dirname neatly encapsulates and handles these
> issues.

    A few instances of calling the external command are insignificant,
    but it adds up if it's called many times in a loop, or in several
    loops.

    In a complex script, eliminating dirname and other external
    commands can make a big difference.

    Setting a variable in a function, rather than using command
    substitution also makes a big difference.

--
    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

 
 
 

absolute pathnames (with bash)?

Post by Michael A. Mill » Thu, 10 Apr 2003 01:16:22


Thanks for all the help!

Mike

 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. 2.4..20-ac2 -> file.c

3. Symlinks to files with absolute pathnames - do we need them ?

4. My modem is not working

5. Converting relative pathnames to absolute?

6. XFS will not start automatically

7. Easiest way to resolve to an absolute pathname

8. sh while read variable scope?

9. possible absolute pathname bug in ld.so-1.4

10. Absolute pathnames to commands in shell scripts

11. Q: Absolute pathname for a process's executable

12. How can I read a cpio archive with absolute pathnames?

13. Getting absolute pathname in POSIX