Writing Shell Scripts - Novell LINUX ENTERPRISE SERVER 11 - ADMINISTRATION Administration Manual

Hide thumbs Also See for LINUX ENTERPRISE SERVER 11 - ADMINISTRATION:
Table of Contents

Advertisement

example, the log files of your system are in /var/log/messages (only acces-
sible for root).

5.2 Writing Shell Scripts

Shell scripts are a convenient way of doing all sorts of tasks: collecting data, searching
for a word or phrase in a text and many other useful things. The following example
shows a small shell script that prints a text:
Example 5.1 A Shell Script Printing a Text
#!/bin/sh
# Output the following line:
echo "Hello World"
The first line begins with the Shebang characters (#!) which is an indicator that
this file is a script. The script is executed with the specified interpreter after the
Shebang, in this case /bin/sh.
The second line is a comment beginning with the hash sign. It is recommended
to comment difficult lines to remember what they do.
The third line uses the built-in command echo to print the respective text.
Before you can run this script you need some prerequisites:
1. Every script should contain a Shebang line (this is already the case with our example
above.) If a script does not have this line, you have to call the interpreter yourself.
2. You can save the script wherever you want. However, it is a good idea to save it
in a directory where the shell searches for it. The search path in a shell is determined
by the environment variable PATH. For example, save it in the directory ~/bin/
under the name hello.sh.
3. The script needs executable permissions. Set the permissions with the following
command:
chmod +x ~/bin/hello.sh
If you have fullfilled all of the above prerequisites, you can execute the script with either
~/bin/hello.sh or hello.sh. The first call uses an absolute path whereas the
Bash and Bash Scripts
45

Advertisement

Table of Contents
loading

This manual is also suitable for:

Suse linux enterprise server 11

Table of Contents