Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: How do you find the Directory of the open Shell Script File?

  1. #11
    Join Date
    Jun 2008
    Location
    UK
    Beans
    282
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: How do you find the Directory of the open Shell Script File?

    Quote Originally Posted by ofnuts View Post
    $PWD gives you the current directory, which isn't the directory where the shell script file is (can be invoked with an explicit path, or be somewhere in the PATH). The directory where the script comes from is obtained by:
    Code:
    mydir=$(dirname $0)
    Actually OP was so badly worded I didn't know what he wanted. It occurred to me that was what he wanted but I dismissed it.
    My preference would be:
    PHP Code:
    myname=${0##*/}
    mydir=${0%/\*\} 
    Quote Originally Posted by ofnuts View Post
    In other words:
    Code:
    #! /bin/bash
    
    me=$0
    mydir=$(dirname $0)
    
    echo "Me: $me"
    echo "My dir: $mydir"
    The problem is when you invoke the code as
    PHP Code:
    sh myprog 
    you end up with mydir being null. You then have to add something like
    PHP Code:
    mydir=${mydir:-$PWD
    And don't forget that if you invoke it with a relative path and then want to move to a different directory in the script ...

    PHP Code:
    #!/bin/bash
    myname=${0##*/}
    mydir=${0%/\*\}
    mydir=${mydir:-$PWD}
    mydir=$(cd $mydirpwd -p)

    printf "mydir is %s\n" $mydir 
    Basically what should happen is that mydir is set to something which is not null and then a subshell is invoked to go to the location of mydir and pass back the absolute path. But I haven't tested this so I may have it wrong.

    Andrew

  2. #12
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: How do you find the Directory of the open Shell Script File?

    Quote Originally Posted by apmcd47 View Post
    Basically what should happen is that mydir is set to something which is not null and then a subshell is invoked to go to the location of mydir and pass back the absolute path. But I haven't tested this so I may have it wrong.

    Andrew
    The absolute path of a file or dir can be obtained with readlink.
    Code:
    >pwd
    /usr/lib
    >readlink -e ../../usr/share/../local/man/man1/../man5/gimprc-2.7.5 
    /usr/local/share/man/man5/gimprc-2.7.5

  3. #13
    Join Date
    Oct 2010
    Location
    The Big, Bad, World!
    Beans
    120
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Thumbs down Re: How do you find the Directory of the open Shell Script File?

    Quote Originally Posted by ofnuts View Post
    The absolute path of a file or dir can be obtained with readlink.
    Code:
    >pwd
    /usr/lib
    >readlink -e ../../usr/share/../local/man/man1/../man5/gimprc-2.7.5 
    /usr/local/share/man/man5/gimprc-2.7.5
    I really need to learn Shell and PHP. Spent far too much time on C++ and Python.
    Choose Freedom, Choose Linux, Choose Ubuntu.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •