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.

Writing Scripts with Bash 243TABLE 11.1Environment Variable$HOSTNAME$USER$UID$LANG$HOMECommand Environment Variables for ScriptsDescriptionHostname of the systemUsername of the user currently logged inUser ID of the user currently logged inLanguage set for the system such as en_US.UTF-8Home directory for the user currently logged in$0 The command executed to run the script, without any commandlinearguments$1, $2, etc. $1 is the value of the first command-line argument to the script,$2 is the value of the second argument, and so on.11Running the ScriptBefore going any further, it is a good idea to test the script. It is also beneficial to test thescript as you add functionality to make it easier to find errors if they exist.First, select a directory location for the script. As mentioned in Chapter 4, “Understanding<strong>Linux</strong> Concepts,” the Filesystem Hierarchy System (FHS) guidelines designate /usr/local/for locally installed software independent of operating system updates. Because this script is acommand, it should be in a bin directory as well. Also, pick a descriptive name for the scriptsuch as get-resources. So, saving it as /usr/local/bin/get-resources allows you to place itin a consistent location across systems and allows other administrators to find it easily.The next step is to make it executable with the command chmod +x get-resources. Ifyou aren’t in the /usr/local/bin/ directory when you execute this command, eitherchange into that directory or give the full path to the script. This command allowsanyone on the system to execute the script. To restrict file permissions further, refer to the“File Permissions” section in Chapter 4.To run the script, either give its full path /usr/local/bin/get-resources or execute thecommand ./get-resources if it is in the current working directory. If executing it fromanother script or specifying it in a crontab as discussed in this chapter, be sure to use the fullpath.When you execute the script, you can redirect the output to a file such as:./get-resources.sh > /var/log/resourcesIf the commands are run without syntax error, the /var/log/resources file shouldcontain all the output. Also watch the command line from which you execute the scriptfor any messages about syntax errors.Optionally, you can also use the $HOSTNAME variable to create a unique filename for eachfile generated by replacing /var/log/resources with /var/log/resources-$HOSTNAME inthe script. This proves useful if all the files are written to a remote shared directory fromeach system running the script.

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

Saved successfully!

Ooh no, something went wrong!