0

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.

Gaita
  • 15
  • 3
  • Note that, instead of putting `then` on a new line, it's usual to use a semicolon and put it on the same line: `if is_active_instance ; then`. This makes the code a bit easier to read, by giving less emphasis to boilerplate syntax. – ruakh May 29 '19 at 20:01
  • It's not a general recommendation, but it is an option for people who want to impose K&R indenting style on a language that wasn't meant for it. – that other guy May 29 '19 at 20:08

2 Answers2

2

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
0
Here is an example of how to do this in BASH.  Your code is on the right track but needs syntax changes to work in BASH.

Instead of checking for a NOT you would check for "Yes" or "No", and you may change the outputs to zeroes and ones if you wish and test for those.

Copy the code between CODE STARTS and CODE ENDS into ./active_instance.sh.

Type the line below and press RETURN.
     chmod 755 ./active_instance.sh


CODE STARTS HERE ==================
#!/usr/bin/env bash

for some_db in mariadb mysqld postgres oracle sybase
do
        echo -n "Checking ${some_db}..."
        set `ps -ef|grep -v grep|grep ${some_db}|wc`

        if test ${1} -gt 0
                then
                        echo "Yes"
                else
                        echo "No"
        fi
done
CODE ENDS HERE ==================


To run, type the line below and press RETURN.
     ./active_instance.sh

Sample execution:

./active_instance.sh
Checking mariadb...Yes
Checking mysqld...Yes
Checking postgres...Yes
Checking oracle...No
Checking sybase...No