Linux Know-How provides a collection of introductory texts on often needed Linux skills. |
Home Bash Guide for Beginners Conditional statements Simple applications of if | |||||||||
See also: More advanced if usage, Using case statements | |||||||||
Search the VIAS Library | Index | |||||||||
Simple applications of if
Testing exit statusThe ? variable holds the exit status of the previously executed command (the most recently completed foreground process). The following example shows a simple test:
The following example demonstrates that TEST-COMMANDS might be any UNIX command that returns an exit status, and that if again returns an exit status of zero:
The same result can be obtained as follows:
Numeric comparisonsThe examples below use numerical comparisons:
This script is executed by cron every Sunday. If the week number is even, it reminds you to put out the garbage cans:
String comparisonsAn example of comparing strings for testing the user ID:
With Bash, you can shorten this type of construct. The compact equivalent of the above test is as follows:
Regular expressions may also be used:
See the info pages for Bash for more information on pattern matching with the "(( EXPRESSION ))" and "[[ EXPRESSION ]]" constructs.
|
|||||||||
Home Bash Guide for Beginners Conditional statements Simple applications of if |