Search code examples
databasebashfunctionshellredhat

How can I do a function that outputs the not of another function in bash shell?


I have an existing function is_active_instance, which determines if a database instance is running (true) or not. I am working in a new function called is_inactive_instance which needs to return true if is_active_instance returns false.

How can I call is_active_instance from is_inactive_instance and negate its return to return True to main program?

I already tried to call is_instance_active with ! to change the result of the original function.

is_active_instance(){
    dbservice=""
    if is_mysql_db
    then
        dbservice="mysqld"
    elif is_mariadb_db
    then
        dbservice="mysqld"
    elif is_postgre_db
    then
        dbservice='postgresql'
    fi
    [ $(ps -ef | grep -v grep | grep $dbservice* | wc -l) > 0 ]
}

is_inactive_instance(){
    [ [ ! is_active_instance ] ]
}

if is_active_instance
then
        echo "Is active instance"
elif is_inactive_instance
then
        echo "Is inactive instance"
else
        echo "Other result"
fi

In Main body I will need to be able to detect if the instance is running, stopped or other for my purposes.


Solution

  • Don't use any [s:

    is_inactive_instance(){
        ! is_active_instance
    }
    

    Also see Comparing numbers in Bash for how to make your is_active_instance work.