#!/bin/bash
echo "\$0:" $0
echo "\$PWD: " $PWD
framp@obelix:/disks/sdc6/home/framp> ./Desktop/tst.sh
$0: ./Desktop/tst.sh
$PWD: /disks/sdc6/home/framp
framp@obelix:/disks/sdc6/home/framp>
/disks/sdc6/home/framp
/disks/sdc6/home/framp/Desktop
./tst.sh
.../Desktop
cd ~/Desktop
Re: Bash script own path
>How does a script find its own path?
>$PWD gives me the path from which it is run...
In short: you can't (reliably). Depending on the interactive shell or
invoking program, $0 contains the full path or not. Executables can use
execv to insert any string they like into $0. You can try using the
which command, if $0 does not supply the information
# error checking omitted
PROGNAME=`basename $0` # strip of path
PROGDIR=`dirname $0`
if [ "$PROGDIR" = "." ]
then
PROG=`which $PROGNAME`
PROGDIR=`dirname $PROG`
fi
/home/prinzunix/Desktop/tst.sh test
Viel einfacher... ohne noch einen Shell- und basename/dirname-Prozess aufmachen zu müssenoc2pus schrieb:ok, das gibt den Pfad vom Aufruf ...
schau mal hier:
http://www.issociate.de/board/post/285661/Bash_script_own_path.html
Re: Bash script own path
>How does a script find its own path?
>$PWD gives me the path from which it is run...
# error checking omitted
PROGNAME=`basename $0` # strip of path
PROGDIR=`dirname $0`
progname="${0##*/}";
mypath="${0%/*}";
PRG_DIR=`dirname $0` # e.g. ../clesh/bin
OLD_PWD=`pwd`
cd $PRG_DIR
PRG_DIR=`pwd`
cd $OLD_PWD
echo $PRG_DIR # e.g. /usr/local/java/clesh/bin