August 2011
Beginner to intermediate
600 pages
14h 29m
English
dirname
In the same category but with opposite functionality to basename, dirname returns the directory name from a path. This is useful when a script does not know the exact location that files will be held in, for example when the script is part of a tarball that could be extracted into the user’s home directory, or /tmp, or anywhere else. The following script uses dirname $0 quite extensively to produce its own relative paths to a fairly complex directory structure. It uses etc/install.cfg (relative to the location of the script) to determine whether or not to interactively ask the installer to accept the license terms.
$ cat install.sh #!/bin/bash ACCEPT_LICENSE=0 # may be overridden by the config file echo "Reading configuration..." CFG='dirname $0'/etc/install.cfg . $CFG echo "Done." mkdir 'dirname $0'/logs 2>/dev/null || exit 1 if [ "$ACCEPT_LICENSE" -ne "1" ]; then ${PAGER:-more} 'dirname $0'/LICENSE.TXT read -p "Do you accept the license terms?" case $REPLY in y*|Y*) continue ;; *) echo "You must accept the terms to install the software." exit 1 ;; esac fi rm -f 'dirname $0'/logs/status case 'uname' in Linux) for rpm in 'dirname $0'/rpms/*.rpm do rpm -Uvh $rpm 2>&1 | tee 'dirname $0'/logs/'basename ${rpm}'.log echo "${PIPESTATUS[0]} $rpm" >> 'dirname $0'/logs/status done ;; SunOS) for pkg in 'dirname $0'/pkgs/*.pkg ...