in 'sh', how to find the directory the script is located in from inside the script??

in 'sh', how to find the directory the script is located in from inside the script??

Post by mik » Mon, 16 Apr 2001 16:13:57



hello.

I am stuck. I need a way to find the full path of the directory the
script is in, from inside the script itself.

the obvious way of using 'pwd' does not of course work, since this returns
the current working directory from where the script was invoked, which
could by anywhere else on the file system.

for example, if I type

 ../../script.sh

Then script.sh cwd is NOT where script.sh is, but it is where the command
was issued from.

any ideas?

thanks,
Mike.

 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by Michael Heimin » Mon, 16 Apr 2001 17:11:12



> hello.

> I am stuck. I need a way to find the full path of the directory the
> script is in, from inside the script itself.

> the obvious way of using 'pwd' does not of course work, since this returns
> the current working directory from where the script was invoked, which
> could by anywhere else on the file system.

> for example, if I type

>  ../../script.sh

> Then script.sh cwd is NOT where script.sh is, but it is where the command
> was issued from.

> any ideas?

> thanks,
> Mike.

cd /your_path

in your script and you know where you are & pwd should work.

Michael Heiming

 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by mik » Mon, 16 Apr 2001 17:47:40



Quote:

>cd /your_path

>in your script and you know where you are & pwd should work.

huh?

did you actually read the question?

The whole point is to find the full absolute path of the script is
located. Your answer is to cd to that path first?

The script itself can located anywhere, say in /usr/bin/.  How does
the script itself finds the directory is /usr/bin/ ??

remember, I can invoke it as

 ../../bin/script.sh

where I happened to be in /usr/bin/foo1/foo2/  when I tryped the command.
doing 'pwd' from inside the script will result in /usr/bin/foo1/foo2/ and
not /usr/bin/

do you understand the problem now?

 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by Michael Heimin » Mon, 16 Apr 2001 21:21:53




> >cd /your_path

> >in your script and you know where you are & pwd should work.

> huh?

> did you actually read the question?

I tried...

Quote:> The whole point is to find the full absolute path of the script is
> located. Your answer is to cd to that path first?

> The script itself can located anywhere, say in /usr/bin/.  How does
> the script itself finds the directory is /usr/bin/ ??

Why should the script be located anywhere? If it's in the PATH issuing
something like (in the script):

SL=`which scriptname`
echo $SL  

should work, I meant (first post) the script should cd to its location,
not the user before starting it, of course..:-)

Quote:

> remember, I can invoke it as

>  ../../bin/script.sh

> where I happened to be in /usr/bin/foo1/foo2/  when I tryped the command.
> doing 'pwd' from inside the script will result in /usr/bin/foo1/foo2/ and
> not /usr/bin/

> do you understand the problem now?

I tried hard, but don't really understood what you want to achieve...?

Michael Heiming

 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by Randal L. Schwar » Tue, 17 Apr 2001 02:27:03


Kelly> The following (untested) example should be helpful:

Kelly> #!/bin/sh
Kelly> script_path=`cd \`dirname $0\` ; pwd`
Kelly> echo $script_path

Unless it's in a directory with whitespace in the name. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by mik » Tue, 17 Apr 2001 03:50:11




Quote:

>The following (untested) example should be helpful:

>#!/bin/sh
>script_path=`cd \`dirname $0\` ; pwd`
>echo $script_path

thanks Kelly. That is exactly what I want. I did know about the direname
and basename commands.
 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by mik » Tue, 17 Apr 2001 03:53:29



Quote:

>should work, I meant (first post) the script should cd to its location,
>not the user before starting it, of course..:-)

no problem. The example by Kelly on how the script
can 'cd' to its location then doing pwd I assume what you had in mind
also.

Quote:

>I tried hard, but don't really understood what you want to achieve...?

just knowing the full path of a script resides from inside the script :)

thanks to all for the time to help. I appreciate it.

 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by Randal L. Schwar » Wed, 18 Apr 2001 07:34:24



Kelly> #!/bin/sh
Kelly> script_path=`cd \`dirname $0\` ; pwd`
Kelly> echo $script_path

Quote:

>> Unless it's in a directory with whitespace in the name. :)

Kelly> Duly noted. But why would anyone (outside of Redmond) do such a silly
Kelly> thing as that? ;-)

Have you ever mounted a Mac partition?  Apparently not.  Always full
of spaces.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

 
 
 

in 'sh', how to find the directory the script is located in from inside the script??

Post by brian hile » Thu, 19 Apr 2001 12:29:26




>> ...
> Have you ever mounted a Mac partition?  Apparently not.  Always full
> of spaces.

_And_ slashes. That sometimes are entrained in the filenames of
Unix volumes with buggy remote filesystem software... Impossible
to remove/rename/access....

Below is a function to resolve a path to an absolute pathname. Mind
the -p option to do a path lookup, as necessary. BTW, on the subject,
note the bug report line that mentions that "whence" (a ksh builtin!)
_cannot_ be given an argument with a space in it!!

=Brian

#! /bin/echo error: only source
#*TAG:42576 4:Jan 9 1973:0755:resolvepath:

# Copyright: (c) 2000
# Description: resolve and canonicize full pathname of arguments
# Name: resolvepath
# Requires:

# See-also: File-PathConvert-0.4.tar (perl)
# Usage: resolvepath [-hlp] path...
# Version: 1.08

#01
function resolvepath # [-hlp] path...
{       set -o noglob
        # Ksh Bug: OPTIND cannot be declared integer!
        typeset -i rc
        typeset IFS OPTARG arg dir fn headers= symlink= oarg opt usepath=
        while getopts :HhLlPp opt
        do      case $opt in
                (h)     headers=ON ;;
                (+h|H)  headers=OFF ;;
                (l)     symlink=ON ;;
                (+l|L)  symlink= ;;
                (p)     usepath=ON ;;
                (+p|P)  usepath= ;;
                ([:?])  print -ru2 "usage: $0 [-hlp] path...
-h      - prepend the output with argument header
-l      - show logical path with symlinks resolved [physical path]
-p      - apply path lookup, if applicable"
                        return 2 ;;
                esac
        done
        shift OPTIND-1
        if [[ $headers = OFF ]]
        then    headers=
        else    (($#>1)) && headers=ON
        fi
        for arg
        do      oarg=$arg
                if [[ $usepath = ON && ! -d $arg ]]
                then    # Ksh Bug: "whence" cannot handle args with spaces
                        arg=$(whence -p "$arg") ||
                        {       print -ru2 'resolvepath: whence: error:' \
                                "\"$oarg\" not found"
                                rc=rc+1 continue
                        }
                fi
                [[ -a $arg ]] ||
                {       print -ru2 "resolvepath: error: \"$arg\" not found"
                        rc=rc+1 continue
                }
                [[ $arg != */* ]] && arg="./$arg"
                if [[ -d $arg ]]
                then    dir=$arg fn=
                else    dir=${arg%/*} fn=${arg##*/}
                fi
${DIAG:+print -ru2 "[ $0: dirpart=\"$dir\", filepart=\"$fn\" ]"}
                # Ksh Bug: "cd -P dir" works, but "cd -P -- dir" does not!
                [[ $dir = -* ]] && dir="./$dir"       # work-around for above bug
                \cd ${symlink:+-P} "$dir" || rc=rc+1 continue
                print -r -- "${headers:+$oarg:     }${PWD%/}/$fn"     # <= TAB
                \cd - >&-
        done
        return $rc

Quote:}

#02 EMBEDDED MAN-PAGE FOR "src2man"
: '
#++
NAME
        resolvepath - resolve and canonicize the full pathname of argument

SYNOPSIS
        resolvepath [-hlp] path...

OPTIONS
        -h      - Prepend the output with argument header.
        -l      - Show logical path with symlinks resolved. [physical path]
        -p      - Apply path lookup, if applicable.

DESCRIPTION
        ...

        If and only if the path is a directory, the output is guaranteed
        to be terminated with a slash ("/").

RETURN CODE
        Returns 0 if successful, 2 for options parsing errors, otherwise
        the number of arguments in error.

EXAMPLE
        $ resolvepath .
        /home/brian/side/lib/

        $ resolvepath cat
        resolvepath: error: "cat" not found
        $ resolvepath -p cat
        /bin/cat
        $ resolvepath -lp cat
        /usr/bin/cat

        $ resolvepath /
        /
        $ resolvepath //
        /
        $ resolvepath /..
        /
        $ resolvepath /.//..///.////../////.//////..///////.////////..
        /

ENVIRONMENT
        PATH

SEE ALSO
        predictshell(3S), stat(3S)

AUTHOR

CAVEATS
        The algorithm that is used requires the directory component of
        the resolved argument to be executable; i.e. you must have
        permission to chdir to it.

BUGS
        Filenames with embedded spaces will be failed to be recognized;
        this is a bug in the ksh builtin "whence", not resolvepath(3S).

#--
'

 
 
 

1. Execute a sh script under perl and sh: sh script; perl script?

I can execute the following perl scripts under either shell or Perl
====

        if 0;
[perl scripts ...]
====
i.e.
sh script
perl script

How do I execute a sh script under either shell or Perl
simililar to what shown above?

Thanks.
--
Michael Wang

http://www.mindspring.com/~mwang    

2. ncsa 1.4.2 and imagemaps w/o .conf?

3. What's 'side effects' of Ksh built-ins?

4. ISDN help needed

5. 'sh' script spawning Sun 'cmdtool' running program

6. shoelace problem

7. Can't get '&' to work from inside a script

8. mouse in xdos: problem

9. Can ISP detect when dial-ins are 'overloaded' ?

10. how do 'plug-ins' work?

11. Can ISP detect when dial-ins are 'overloaded' ?

12. Script: Who's shell is /bin/sh in script?

13. SH can't find script?