## Freitag, 10. Februar 2012

### Bash if statement

The Bash if statement is a bit funny. Say for every files-i.txt, there should be a files-i.dat.
This example can be used to filter out files that dont have the corresponding .dat file present.

for i in files-*txtdo    if [ ! -e ${i/txt/dat} ] then echo$i    fidone

Note the spaces after and before the opening and closing hard bracket. Also note that "then" is on its own line.

#### Kommentare:

1. Strange criticism.

if' simply looks for a 0 or a 1 as the output of *any* command. In bash, [ ... ] is "syntactic sugar" for test', e.g. if test ! -e file.dat'. In other words, the [ ... ] isn't necessarily part of if'.

And you also don't need to put then' on it's own line. You can separate the if test' from then with a semicolon, e.g. `if /bin/true ; then echo TRUE ; fi '.

Sorry for posting on an old post. Catching up in my feed reader.

1. Appreciate it. Am I to understand that the space between the semi-colons is required? On both sides?