1 Taugrel

Tldp Bash Conditional Assignment

+

plus

-

minus

*

multiplication

/

division

**

exponentiation

%

modulo, or mod (returns the remainder of an integer division operation)

5/3 = 1, with remainder 2

This operator finds use in, among other things, generating numbers within a specific range (see Example 9-11 and Example 9-15) and formatting program output (see Example 27-16 and Example A-6). It can even be used to generate prime numbers, (see Example A-15). Modulo turns up surprisingly often in numerical recipes.

Example 8-1. Greatest common divisor

+=

plus-equal (increment variable by a constant)

results in being incremented by .

-=

minus-equal (decrement variable by a constant)

*=

times-equal (multiply variable by a constant)

results in being multiplied by .

/=

slash-equal (divide variable by a constant)

%=

mod-equal (remainder of dividing variable by a constant)

Arithmetic operators often occur in an expr or let expression.

Example 8-2. Using Arithmetic Operations

7.1.1. General

At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command. The if construction allows you to specify such conditions.

The most compact syntax of the if command is:

if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi

The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.

The TEST-COMMAND often involves numerical or string comparison tests, but it can also be any command that returns a status of zero when it succeeds and some other status when it fails. Unary expressions are often used to examine the status of a file. If the argument to one of the primaries is of the form , then file descriptor "N" is checked. , and and their respective file descriptors may also be used for tests.

7.1.1.1. Expressions used with if

The table below contains an overview of the so-called "primaries" that make up the TEST-COMMAND command or list of commands. These primaries are put between square brackets to indicate the test of a conditional expression.

Table 7-1. Primary expressions

PrimaryMeaning
[ ]True if exists.
[ ]True if exists and is a block-special file.
[ ]True if exists and is a character-special file.
[ ]True if exists and is a directory.
[ ]True if exists.
[ ]True if exists and is a regular file.
[ ]True if exists and its SGID bit is set.
[ ]True if exists and is a symbolic link.
[ ]True if exists and its sticky bit is set.
[ ]True if exists and is a named pipe (FIFO).
[ ]True if exists and is readable.
[ ]True if exists and has a size greater than zero.
[ ]True if file descriptor is open and refers to a terminal.
[ ]True if exists and its SUID (set user ID) bit is set.
[ ]True if exists and is writable.
[ ]True if exists and is executable.
[ ]True if exists and is owned by the effective user ID.
[ ]True if exists and is owned by the effective group ID.
[ ]True if exists and is a symbolic link.
[ ]True if exists and has been modified since it was last read.
[ ]True if exists and is a socket.
[ ]True if has been changed more recently than , or if exists and does not.
[ ]True if is older than , or is exists and does not.
[ ]True if and refer to the same device and inode numbers.
[ OPTIONNAME ]True if shell option "OPTIONNAME" is enabled.
STRING ]True of the length if "STRING" is zero.
STRING ] or [ STRING ]True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 < STRING2 ] True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ] True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ]"OP" is one of , , , , or . These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

Expressions may be combined using the following operators, listed in decreasing order of precedence:

Table 7-2. Combining expressions

OperationEffect
[ ! EXPR ]True if EXPR is false.
[ ( EXPR ) ]Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ]True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]True if either EXPR1 or EXPR2 is true.

The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.

7.1.1.2. Commands following the then statement

The CONSEQUENT-COMMANDS list that follows the then statement can be any valid UNIX command, any executable program, any executable shell script or any shell statement, with the exception of the closing fi. It is important to remember that the then and fi are considered to be separated statements in the shell. Therefore, when issued on the command line, they are separated by a semi-colon.

In a script, the different parts of the if statement are usually well-separated. Below, a couple of simple examples.

7.1.1.3. Checking files

The first example checks for the existence of a file:

7.1.1.4. Checking shell options

To add in your Bash configuration files:

The environment
 

The above example will work when entered on the command line:

However, if you use testing of conditions that depend on the environment, you might get different results when you enter the same command in a script, because the script will open a new shell, in which expected variables and options might not be set automatically.

7.1.2. Simple applications of if

7.1.2.1. Testing exit status

The 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:

7.1.2.2. Numeric comparisons

The 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:

7.1.2.3. String comparisons

An 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:

Similar to the "&&" expression which indicates what to do if the test proves true, "||" specifies what to do if the test is false.

Regular expressions may also be used in comparisons:

Real Programmers
 

Most programmers will prefer to use the test built-in command, which is equivalent to using square brackets for comparison, like this:

No exit?
 

If you invoke the exit in a subshell, it will not pass variables to the parent. Use { and } instead of ( and ) if you do not want Bash to fork a subshell.

See the info pages for Bash for more information on pattern matching with the "(( EXPRESSION ))" and "[[ EXPRESSION ]]" constructs.

Leave a Comment

(0 Comments)

Your email address will not be published. Required fields are marked *