24.07.2018 Views

Bash-Beginners-Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

11.2.2. Setting the path<br />

This section might be found in your /etc/profile file. The function pathmunge is defined and then used<br />

to set the path for the root and other users:<br />

pathmunge () {<br />

if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then<br />

if [ "$2" = "after" ] ; then<br />

PATH=$PATH:$1<br />

else<br />

PATH=$1:$PATH<br />

fi<br />

fi<br />

}<br />

# Path manipulation<br />

if [ `id -u` = 0 ]; then<br />

pathmunge /sbin<br />

pathmunge /usr/sbin<br />

pathmunge /usr/local/sbin<br />

fi<br />

pathmunge /usr/X11R6/bin after<br />

unset pathmunge<br />

The function takes its first argument to be a path name. If this path name is not yet in the current path, it is<br />

added. The second argument to the function defines if the path will be added in front or after the current PATH<br />

definition.<br />

Normal users only get /usr/X11R6/bin added to their paths, while root gets a couple of extra directories<br />

containing system commands. After being used, the function is unset so that it is not retained.<br />

11.2.3. Remote backups<br />

The following example is one that I use for making backups of the files for my books. It uses SSH keys for<br />

enabling the remote connection. Two functions are defined, buplinux and bupbash, that each make a .tar<br />

file, which is then compressed and sent to a remote server. After that, the local copy is cleaned up.<br />

On Sunday, only bupbash is executed.<br />

#/bin/bash<br />

LOGFILE="/nethome/tille/log/backupscript.log"<br />

echo "Starting backups for `date`" >> "$LOGFILE"<br />

buplinux()<br />

{<br />

DIR="/nethome/tille/xml/db/linux-basics/"<br />

TAR="Linux.tar"<br />

BZIP="$TAR.bz2"<br />

SERVER="rincewind"<br />

RDIR="/var/www/intra/tille/html/training/"<br />

cd "$DIR"<br />

tar cf "$TAR" src/*.xml src/images/*.png src/images/*.eps<br />

echo "Compressing $TAR..." >> "$LOGFILE"<br />

Chapter 11. Functions 134

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!