if statement and calling function in if using bash

if [ check_log ];

When you use square brackets you’re invoking the test command. It’s equivalent to if test check_log which is shorthand for if test -n check_log, which in turn means “if "check_log" is not an empty string”. It doesn’t call your check_log function at all.

Change it to this:

if check_log;

By the way, the function could be more simply written as:

check_log() {
    ! [ -f "/usr/apps/appcheck.log" ]

The return value from a function is the exit status of the last command, so no need for explicit return statements.

Leave a Comment