7.2.5.2. Some simple examples
A very simple script consisting of only one command, that says
hello to the user executing it:
[jerry@nowhere ~] cat hello.sh
#!/bin/bash
echo "Hello $USER"
|
The script actually consists of only one command, echo, which uses the value of ($) the
USER environment variable to print a
string customized to the user issuing the command.
Another one-liner, used for displaying connected users:
#!/bin/bash
who | cut -d " " -f 1 | sort -u
|
Here is a script consisting of some more lines, that I use to
make backup copies of all files in a directory. The script first
makes a list of all the files in the current directory and puts it
in the variable LIST. Then it sets the
name of the copy for each file, and then it copies the file. For
each file, a message is printed:
tille:~> cat bin/makebackupfiles.sh
#!/bin/bash
# make copies of all files in a directory
LIST=`ls`
for i in $LIST; do
ORIG=$i
DEST=$i.old
cp $ORIG $DEST
echo "copied $i"
done
|
Just entering a line like mv * *.old won't work, as you will notice
when trying this on a set of test files. An echo command was added in order to display some
activity. echo's are generally useful when a
script won't work: insert one after each doubted step and you will
find the error in no time.
The /etc/rc.d/init.d directory
contains loads of examples. Let's look at this script that controls
the fictive ICanSeeYou server:
#!/bin/sh
# description: ICanSeeYou allows you to see networked people
# process name: ICanSeeYou
# pidfile: /var/run/ICanSeeYou/ICanSeeYou.pid
# config: /etc/ICanSeeYou.cfg
# Source function library.
. /etc/rc.d/init.d/functions
# See how (with which arguments) we were called.
case "$1" in
start)
echo -n "Starting ICanSeeYou: "
daemon ICanSeeYou
echo
touch /var/lock/subsys/ICanSeeYou
;;
stop)
echo -n "Shutting down ICanSeeYou: "
killproc ICanSeeYou
echo
rm -f /var/lock/subsys/ICanSeeYou
rm -f /var/run/ICanSeeYou/ICanSeeYou.pid
;;
status)
status ICanSeeYou
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
|
First, with the . command (dot) a set of
shell functions, used by almost all shell scripts in /etc/rc.d/init.d, is loaded. Then a case command is issued, which defines 4 different
ways the script can execute. An example might be ICanSeeYou start.
The decision of which case to apply is made by reading the (first)
argument to the script, with the expression $1.
When no compliant input is given, the default case, marked with
an asterisk, is applied, upon which the script gives an error
message. The case list is ended with the
esac statement. In the start case
the server program is started as a daemon, and a process ID and
lock are assigned. In the stop case, the server process is
traced down and stopped, and the lock and the PID are removed.
Options, such as the daemon option, and
functions like killproc, are defined in the
/etc/rc.d/init.d/functions file. This
setup is specific to the distribution used in this example. The
initscripts on your system might use other functions, defined in
other files, or none at all.
Upon success, the script returns an exit code of zero to its
parent.
This script is a fine example of using functions, which make the
script easier to read and the work done faster. Note that they use
sh instead of bash,
to make them useful on a wider range of systems. On a Linux system,
calling bash as sh
results in the shell running in POSIX-compliant mode.
The bash man pages contain more
information about combining commands, for-
and while-loops and regular expressions, as
well as examples. A comprehensible Bash course for system administrators and
power users, with exercises, from the same author as this
Introduction to Linux guide, is at
https://tille.xalasys.com/training/bash/. Detailed
description of Bash features and
applications is in the reference guide
Advanced Bash Scripting.