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...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

244CHAPTER 11Automating Tasks with ScriptsBecause this script writes the output file to the /var/log/ directory, it must be run as rootas non-root users do not have permission to write to this directory. If you want non-rootusers to be able to run the script, change the output file to a location writable by all such asthe /tmp/ directory, and make the output filename unique to each user such as/tmp/resources-$HOSTNAME-$USER, where $USER is an environment variable for the usernameof the user currently logged in.ConditionalsSimilar to other programming languages, Bash allows for conditionals, or if/then statements.If a condition is met, then the commands are executed. Optionally, you can use an elsestatement to provide commands if the condition is not met or use an elif statement toprovide additional conditionals. Listing 11.4 shows the basic syntax and structure.LISTING 11.4Bash Syntax for Conditionalsif [ ];then...elif...else...fiTable 11.2 lists commonly used conditions (). For a complete list, refer to the“Conditional Expressions” section of the Bash man page or the “Bash Conditional Expressions”section of the “Bash Reference Manual” at http://www.gnu.org/software/bash/manual/.TABLE 11.2 Command Environment Variables for ScriptsConditionalDescription-d “filename” Returns true if file exists and is a directory-e “filename” Returns true if file exists-r “filename” Returns true if file exists and is readable-s “filename” Returns true if file exists and is bigger than zero bytes-w “filename” Returns true if file exists and is writable-x “filename” Returns true if file exists and is executable-N “filename” Returns true if file exists and has been modified since it was last read-n “$VARIABLE” Returns true if string is non-zero“$VAR1” == “$VAR2” Returns true if strings are equal“$VAR1” != “$VAR2” Returns true if strings are not equal“$VAR1” < “$VAR2” Returns true if the first string sorts before the second string lexicographicallyaccording to the system’s locale such as alphabetically forthe en_US.UTF-8 locale“$VAR1” > “$VAR2” Returns true if the first string sorts after the second string lexicographicallyaccording to the system’s locale such as alphabetically forthe en_US.UTF-8 locale

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

Saved successfully!

Ooh no, something went wrong!