User Tools

Site Tools


info:bash

bash / sh scripting

http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-in

Directory of the script as called is `dirname $0`.

Apache Tomcat's scripts follow symbolic links this way (presumably to be more platform-agnostic):

# resolve links - $0 may be a softlink
PRG="$0"
while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done
PRGDIR=`dirname "$PRG"`

The following appears to do the same thing:

#!/bin/sh
PRG="$0"
 
# need this for relative symlinks
while [ -h "$PRG" ] ; do
   PRG=`readlink "$PRG"`
done
 
scriptdir=`dirname "$PRG"`

$* vs. $@

Both refer to all parameters, but $@, when double-quoted (i.e. "$@"), makes the parameters individually quoted ("$1" "$2" ...). For details, see the Bash Reference Manual, Special Parameters section.

Exit on Errors ("Strict")

References: http://redsymbol.net/articles/unofficial-bash-strict-mode/, https://sipb.mit.edu/doc/safe-shell/

set -euo pipefail

Or possibly also with IFS:

set -euo pipefail
IFS=$'\n\t'
info/bash.txt · Last modified: 2016-01-21 21:12 by sam