No e-Patents

heise online News

Was war. Was wird.

3D-Universalbrille auf Funkbasis kommt im Dezember

3D-Blu-ray-Update für PS3 im Oktober

Studie: Junge Rennspiel-Fans fahren auch im echten Leben rasanter

Tablet mit 3D-Display

Videotext-Seite hilft bei Umstellung auf digitales Sat-TV

All-in-One-PC mit 3D-Touch-Display

Google vereinheitlicht Datenschutzrichtlinien

Verbatim bringt 100-GByte-Disc BDXL nach Europa

Blickwinkelstabile 16:9-Monitore bei LG

AMD zeigt Fusion-CPU mit integrierter DirectX-11-Grafik im Betrieb

Googles Musikladen öffnet wahrscheinlich noch vor Jahresende

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