我怎样才能做一个输出 bash shell 中另一个函数的 not 的函数?

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

我有一个现有函数 is_active_instance,它确定数据库实例是否为 运行(真)。我正在使用一个名为 is_inactive_instance 的新函数,如果 is_active_instance return 为假,则需要 return 为真。

如何从 is_inactive_instance 调用 is_active_instance 并将它的 return 否定为 return 与主程序一致?

我已经尝试用 ! 调用 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

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

不要使用任何 [s:

is_inactive_instance(){
    ! is_active_instance
}

另请参阅 Comparing numbers in Bash 了解如何让您的 is_active_instance 发挥作用。

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  -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