Question

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.

Was it helpful?

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

OTHER TIPS

How are you running your script?

Here is a sample I cobbled together from the cited post:

#!/bin/bash
BASEDIR=$(dirname $0)
echo BASEDIR=$BASEDIR
echo dollar zero=$0
echo "The script you are running has basename `basename $0`, dirname `dirname $0`"
echo "The present working directory is `pwd`"

qsh

bash_script
BASEDIR=/home/buck
dollar zero=/home/buck/bash_script
The script you are running has basename bash_script, dirname /home/buck The present working directory is /home/buck

./bash_script
BASEDIR=.
dollar zero=./bash_script
The script you are running has basename bash_script, dirname . The present working directory is /home/buck

cd /

/home/buck/bash_script
BASEDIR=/home/buck
dollar zero=/home/buck/bash_script
The script you are running has basename bash_script, dirname /home/buck The present working directory is /

cd /home/buck

/home/buck/bash_script
BASEDIR=/home/buck
dollar zero=/home/buck/bash_script
The script you are running has basename bash_script, dirname /home/buck The present working directory is /home/buck

exit

qsh cmd(bash_script)

BASEDIR=/home/buck
dollar zero=/home/buck/bash_script
The script you are running has basename bash_script, dirname /home/buck The present working directory is /home/buck

QSH CMD('/home/buck/bash_script')

BASEDIR=/home/buck
dollar zero=/home/buck/bash_script
The script you are running has basename bash_script, dirname /home/buck The present working directory is home/buck

QSH CMD('./bash_script')

BASEDIR=.
dollar zero=./bash_script
The script you are running has basename bash_script, dirname . The present working directory is /home/buck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top