IMC!


Contents


Photos

 







Browsing around...
News  News Links  Links Blog  Blog Italiano  Español 
08 - Conditional expressions in the bash

 | 

In this section a list of all the conditional tests already discussed in the previous sections is shown and completed. The conditions are always written inside square brackets.

File operators
A series of operators exists in order to verify files conditions, according to the following syntax:

[ check_operator /etc/file_to_check]

These controls are:

OperatorMeaning
-d:Checks that the file is a directory
-e:Checks that the file exists
-f:Checks that the file is regular
-g:Checks that the SGID bit is set
-r:Checks that the file has the permissions for
reading for the user executing the script
-s:Checks that the file has a non zero dimension
-u:Checks that the SUID bit is set
-w:Checks that the file is writable

Number operators
Two numbers can be compared with the following tests. The variables must be written in the form:

"$variable_name"

The numerical tests are:

ControlMeaning
x -eq y true if x is equal to y
x -ne y true if x is not equal to y
x -gt y true if x is bigger than y
x -lt y true if x is smaller than y
x -ge y true if x is bigger than or equal to y
x -le y true if x is smaller than or equal to y

Strings controls
Two strings can be compared with the following tests:

"$variable_name"

These are the available scripts:

ControlMeaning
x = y true if x is equal to y
x != y true if x is equal to y
-n x true if x is not NULL or void
-z x true if x is NULL or void


Logical operators
The AND and OR logical operators can be used, according to the following syntax:

[ condition1 ] && [ condition2 ]

The logical operators are;
SymbolMeaning
& &AND
||OR

 | 







Comments

No comment present!

Write a comment

You can write here a comment to the article you've just read. Smiles, links and images are not allowed. The maximum comment length is 4000 characters. Please be polite, all the offensive messages will be deleted.

Your comment (lascia bianco!):
Uses (max 25 characters, required)
Web site (max 255 characters, optional)
e-Mail (max 255 characters, optional, will not be published) Your opinion (lascia bianco!):
Comment (max 4000 characters, required):





Valid HTML 4.01 Transitional
E-Mail - 315.65 ms

Valid HTML 4.01 Transitional