No e-Patents

heise online News

Provider-Haftung kein Thema mehr für ACTA-Verhandlungen

Öffentlich-Rechtliche und Private üben "ein bisschen Frieden"

Internet Explorer: Datenklau durch zwei Jahre alte Lücke

Sky-CEO: Wir können es ohne weiteres Geld von Murdoch schaffen

Musik-Download übers Radio

Neue 3D-Blu-ray-Titel und erste Details zu Skys 3D-Eventkanal

Themen-Special: Power-Tools fürs Internet

Runder Tisch zur digitalen Dividende angeregt

Internetportal soll Kulturgüter schützen helfen

BKA: Fehlendes Gesetz lähmt Internet-Ermittlungen

3D-Schnittprogramm aus Berlin

Schnelle SDHC-Speicherkarten mit SDXC-Technik

How a sh script finds it's own place

Datum: 31.10.2009 17:09

If a shell scripts has to refer other files who's place is only known relative to the script, and the script does not want to rely on the current directory of the shell process calling it, it has to know where it stands in absolute terms.

Newer bash releases provide some special environment variables, but we do not want to rely on them.

This snippets shows how to do this more or less savely and reliable, see below for details

#!/bin/sh

MYSELF=$( which $0 )
echo "Myself  (can be relative): $MYSELF"

MYDIR=$( dirname $( which $0 ) )
echo "My dir (can be relative): $MYDIR"

MYSELFCAN=$( readlink -f $( which $0 ) )
echo "Myself canonical = $MYSELFCAN"

MYDIRCAN=$( dirname $( readlink -f $( which $0 ) ) )
echo "My dir canonical: $MYDIRCAN"

MYSELFSHORT=$( basename $0 )
echo "Myself short: $MYSHORT"

Considering this script is called test.sh and lies in a myscripts/ directory, this solution ...

 

Contact  -  Kontakt  -  +  -  @  -  Impressum  -  AGB  -  © 2008 IBCL