Search Engine



RedhatEnterpriseLinux Blog

Writing Scripts with Bash

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.
Because the default shell in Red Hat Enterprise Linux is Bash, the bash RPM package is most likely already installed on your system. You might also want to check out the many commands from the coreutils and the util-linux packages such as rename to rename a group of files and base name to return the base name of a file without its extension.

All Bash scripts must start with a line that defines it as a Bash script:

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:
# 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
#Script to generate a system resources report
#Author: Raziq
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.


Post a Comment

Powered by Blogger.


Copyright © Redhat Enterprise linux. Original Concept and Design by My Blogger Themes
My name is Abdul Razaq but people call me Raziq. Here is my home page: I live in Quetta, Pakistan and work as an IT-Engineer.