Skip to main content
fixed error in example and tried to be more clear
Source Link
mrb
  • 10.5k
  • 3
  • 39
  • 36

[ and test are synonyms (except [ requires ]), so you don't want to use [ test:

[ -x /bin/cat ] && echo cat'cat is executableexecutable' test -x /bin/cat && echo cat'cat is executableexecutable' 

test returns a zero exit status if the condition is true, otherwise nonzero. This can actually be replaced by any program to check its exit status, where 0 indicates success and non-zero indicates failure:

# echoes "command succeeded" because echo rarely fails: if /bin/echo hi; then echo 'echo'command succeeded'; else echo 'command failed'; fi # echoes "command failed" because rmdir requires an argument if /bin/rmdir; then echo 'command succeeded'; else echo 'command failed'; fi 

If you are using bash (and not sh)However, you can use [[ condition ]]all of the above examples only test against the program's exit status, which behaves more predictably when there are spaces or other special characters in your condition. Otherwise it isand ignore the sameprogram's output.

For find, I think you will need to test if any output was generated. -n tests for a non-empty string:

if [[ -n $(find /var/log/crashes -name "app-*.log" -mmin -5) ]] then service myapp restart fi 

A full list of test arguments is available by invoking help test at the bash commandline.

If you are using bash (and not sh), you can use [[ condition ]], which behaves more predictably when there are spaces or other special cases in your condition. Otherwise it is generally the same as using [ condition ]. I've used [[ condition ]] in this example, as I do whenever possible.

I also changed `command` to $(command), which also generally behaves similarly, but is nicer with nested commands.

[ and test are synonyms (except [ requires ]), so you don't want to use [ test:

[ -x /bin/cat ] && echo cat is executable test -x /bin/cat && echo cat is executable 

test returns a zero exit status if the condition is true, otherwise nonzero. This can be replaced by any program to check its exit status:

# echo rarely fails: if /bin/echo hi; then echo 'echo failed'; fi 

If you are using bash (and not sh), you can use [[ condition ]], which behaves more predictably when there are spaces or other special characters in your condition. Otherwise it is the same.

For find, I think you will need to test if any output was generated. -n tests for a non-empty string:

if [[ -n $(find /var/log/crashes -name "app-*.log" -mmin -5) ]] then service myapp restart fi 

A full list of test arguments is available by invoking help test at the bash commandline.

[ and test are synonyms (except [ requires ]), so you don't want to use [ test:

[ -x /bin/cat ] && echo 'cat is executable' test -x /bin/cat && echo 'cat is executable' 

test returns a zero exit status if the condition is true, otherwise nonzero. This can actually be replaced by any program to check its exit status, where 0 indicates success and non-zero indicates failure:

# echoes "command succeeded" because echo rarely fails if /bin/echo hi; then echo 'command succeeded'; else echo 'command failed'; fi # echoes "command failed" because rmdir requires an argument if /bin/rmdir; then echo 'command succeeded'; else echo 'command failed'; fi 

However, all of the above examples only test against the program's exit status, and ignore the program's output.

For find, you will need to test if any output was generated. -n tests for a non-empty string:

if [[ -n $(find /var/log/crashes -name "app-*.log" -mmin -5) ]] then service myapp restart fi 

A full list of test arguments is available by invoking help test at the bash commandline.

If you are using bash (and not sh), you can use [[ condition ]], which behaves more predictably when there are spaces or other special cases in your condition. Otherwise it is generally the same as using [ condition ]. I've used [[ condition ]] in this example, as I do whenever possible.

I also changed `command` to $(command), which also generally behaves similarly, but is nicer with nested commands.

update note about echo in response to comment
Source Link
mrb
  • 10.5k
  • 3
  • 39
  • 36

[ and test are synonyms (except [ requires ]), so you don't want to use [ test:

[ -x /bin/cat ] && echo cat is executable test -x /bin/cat && echo cat is executable 

test returns a zero exit status if the condition is true, otherwise nonzero. This can be replaced by any program to check its exit status:

# echo neverrarely fails: if /bin/echo hi; then echo 'echo failed'; fi 

If you are using bash (and not sh), you can use [[ condition ]], which behaves more predictably when there are spaces or other special characters in your condition. Otherwise it is the same.

For find, I think you will need to test if any output was generated. -n tests for a non-empty string:

if [[ -n $(find /var/log/crashes -name "app-*.log" -mmin -5) ]] then service myapp restart fi 

A full list of test arguments is available by invoking help test at the bash commandline.

[ and test are synonyms (except [ requires ]), so you don't want to use [ test:

[ -x /bin/cat ] && echo cat is executable test -x /bin/cat && echo cat is executable 

test returns a zero exit status if the condition is true, otherwise nonzero. This can be replaced by any program to check its exit status:

# echo never fails: if /bin/echo hi; then echo 'echo failed'; fi 

If you are using bash (and not sh), you can use [[ condition ]], which behaves more predictably when there are spaces or other special characters in your condition. Otherwise it is the same.

For find, I think you will need to test if any output was generated. -n tests for a non-empty string:

if [[ -n $(find /var/log/crashes -name "app-*.log" -mmin -5) ]] then service myapp restart fi 

A full list of test arguments is available by invoking help test at the bash commandline.

[ and test are synonyms (except [ requires ]), so you don't want to use [ test:

[ -x /bin/cat ] && echo cat is executable test -x /bin/cat && echo cat is executable 

test returns a zero exit status if the condition is true, otherwise nonzero. This can be replaced by any program to check its exit status:

# echo rarely fails: if /bin/echo hi; then echo 'echo failed'; fi 

If you are using bash (and not sh), you can use [[ condition ]], which behaves more predictably when there are spaces or other special characters in your condition. Otherwise it is the same.

For find, I think you will need to test if any output was generated. -n tests for a non-empty string:

if [[ -n $(find /var/log/crashes -name "app-*.log" -mmin -5) ]] then service myapp restart fi 

A full list of test arguments is available by invoking help test at the bash commandline.

Source Link
mrb
  • 10.5k
  • 3
  • 39
  • 36

[ and test are synonyms (except [ requires ]), so you don't want to use [ test:

[ -x /bin/cat ] && echo cat is executable test -x /bin/cat && echo cat is executable 

test returns a zero exit status if the condition is true, otherwise nonzero. This can be replaced by any program to check its exit status:

# echo never fails: if /bin/echo hi; then echo 'echo failed'; fi 

If you are using bash (and not sh), you can use [[ condition ]], which behaves more predictably when there are spaces or other special characters in your condition. Otherwise it is the same.

For find, I think you will need to test if any output was generated. -n tests for a non-empty string:

if [[ -n $(find /var/log/crashes -name "app-*.log" -mmin -5) ]] then service myapp restart fi 

A full list of test arguments is available by invoking help test at the bash commandline.