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.

Writing Scripts with Bash 245TABLE 11.2Continued$NUM1 -eq $NUM2$NUM1 -ne $NUM2$NUM1 -lt $NUM2$NUM1 -le $NUM2$NUM1 -gt $NUM2$NUM1 -ge $NUM2Returns true if the two integers are equal in valueReturns true if the two integers are not equal in valueReturns true if the first integer is less than the second integerReturns true if the first integer is less than or equal to the secondintegerReturns true if the first integer is greater than the second integerReturns true if the first integer is greater than or equal to thesecond integer11TIPBe sure to include the spaces around the operators and operand. Also, unless you areusing the operands to compare integers, be sure to place quotation marks around thevariables when comparing their values because the values compared are expected tobe strings. For example,if [ “$VAR1” == “$VAR2” ]then...fiis the proper syntax to determine if two variables are equal.To keep a copy of the last system resource report generated, you can add the followingline to the beginning of the script:mv /var/log/resources /var/log/resources.oldHowever, you will receive an error from the mv command if the file doesn’t exist such asthe very first time you run the script. To make the script more robust, you can use a conditionalto check for the existence of the file before renaming it as shown in Listing 11.5.LISTING 11.5Check for Existence of File#Keep previously generated reportif [ -e “/var/log/resources” ];thenmv /var/log/resources /var/log/resources.oldfiLoopsAnother useful part of the Bash scripting language is the inclusion of loops. There arethree types of loops: while, for, and until.A while loop allows you to execute a sequence of commands while a test is true such aswhile an integer is larger than another integer or while a string does not equal anotherstring. Listing 11.6 shows the basic syntax of a while loop.

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

Saved successfully!

Ooh no, something went wrong!