12.07.2015 Views

Red Hat Enterprise Linux 5 Administration Unleashed

Red Hat Enterprise Linux 5 Administration Unleashed

Red Hat Enterprise Linux 5 Administration Unleashed

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

242CHAPTER 11Automating Tasks with ScriptsLISTING 11.2Continuedfree -mecho “”echo “”echo “DISK USAGE REPORT:”echo “-------------------------------------------------”echo “Output from df -h:”df -hecho “”echo “Output from vmstat -d:”vmstat -decho “”The echo command has other benefits. For example, the echo command can be used toprint a message at the beginning and end of the program so the user running it knowsthe status of the program. Additional usages of echo will also be discussed as this chapterexplores other Bash programming features.VariablesAn administrator would most likely want to run this script on more than one system. Buthow can he keep track of which file was generated from each system? Because eachsystem has a unique hostname, he can add the hostname to the report. At the top of thescript, before the uptime command, the following line can be added:echo “System Resources Report for $HOSTNAME”$HOSTNAME in this line is a variable, denoted by the dollar sign in front of it. In this case,the variable is an environment variable given its default value by the system. Other variablescan be declared and given values in a Bash script and then referenced later in thescript as shown in Listing 11.3.LISTING 11.3Using Variables in Bash#!/bin/bashVARIABLE=valueecho “The value of $VARIABLE is $VARIABLE”Table 11.1 lists additional environment variables that are useful when writing scripts.

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

Saved successfully!

Ooh no, something went wrong!