如何在bash shell中执行一个不输出另一个函数的函数?

时间:2019-05-29 19:40:43

标签: database bash function shell redhat

我有一个现有函数is_active_instance,该函数确定数据库实例是否正在运行(真)。我正在使用一个名为is_inactive_instance的新函数,如果is_active_instance返回false,则该函数必须返回true。

如何从is_inactive_instance中调用is_active_instance并否定其返回值以将True返回主程序?

我已经尝试用调用is_instance_active!更改原始功能的结果。

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

在主体中,我将需要能够检测到实例是否正在运行,已停止或出于其他目的。

2 个答案:

答案 0 :(得分:2)

请勿使用任何[

is_inactive_instance(){
    ! is_active_instance
}

另请参见Comparing numbers in Bash,了解如何使is_active_instance正常工作。

答案 1 :(得分: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