The
GNU Bourne Again Shell (Bash) scripting language is useful when trying to
automate a sequence of commands or when you want to execute the same command
over and over again until a certain limit is reached.
All
Bash scripts must start with a line that defines it as a Bash script:
#!/bin/bash
Any
other lines that begin with the hash mark (#) are considered comments and are
not processed as lines of code. For example, you can add a small description of
what the program does, the author, when it was last modified, and a version
number to the top of the script:
#!/bin/bash
#
This program creates daily backups for Company Name
#
For internal use only
#
Author: Your Name Here
#
Last modified: May 15, 2012
#
Version: 1.5
Executing Commands in a Bash
Script
To
execute a series of commands in a Bash script, list each command on a separate
line. For example, Listing 11.1 shows the very basic Bash script that gathers
information about system resources.
LISTING
11.1 Generating a System Resources Report, Version 1
#!/bin/bash
#Script
to generate a system resources report
#Author:
Raziq
uptime
mpstat
free
-m
df
-h
vmstat
-d
As
you can see, the commands are listed in the script, each on separate lines.
Although the resulting file is useful to an administrator if generated on a
regular basis, it would be more useful if the output were put into context. A
useful Bash command when writing scripts is the echo command. Any text in
quotation marks following the echo command is displayed to the terminal.
If
used on the command line, the echo command can be used to display messages to
the terminal. If used in a Bash script, the echo command can be used to write
messages to a file if the output is redirected to a file.
0 comments:
Post a Comment