Search code examples
ibm-midrange

Can a QShell script tell what directory it's stored in?


For IBMi V6R1's QShell qsh

I tired the advice of Getting the source directory of a Bash script from within

The incorrect output:

BASEDIR=$(dirname $0)
echo $BASEDIR

$ QSHELL/QZSHSH

QSHELL/QZSH is not the location of the executing script.

Please advise on a command to locate the path of the executing script.


Solution

  • You received that result because you ran the command interactively and not from a shell script.

    If I create a "test.sh" script in my home directory as:

    #!/bin/sh
    echo "$(cd "$(dirname "$0")" && pwd)"
    

    I receive the following results:

    $ echo $(dirname $0)
    QSHELL
    
    $ echo $0
    QSHELL/QZSHSH
    
    $ cd / && pwd
    /
    
    $ sh $HOME/test.sh
    /home/jamesa
    
    $ cd && pwd
    /home/jamesa
    
    $ sh test.sh
    /home/jamesa