Database 如何在bashshell中输出另一个函数的not?

Database 如何在bashshell中输出另一个函数的not?,database,bash,function,shell,redhat,Database,Bash,Function,Shell,Redhat,我有一个现有函数是_active _instance,它确定数据库实例是否正在运行(true)。我正在使用一个名为is_inactive_instance的新函数,如果is_active_instance返回false,则需要返回true 如何从is_inactive_实例调用is_active_实例,并对其返回进行否定以将True返回主程序 我已经试着呼叫正在使用的实例!更改原始函数的结果 is_active_instance(){ dbservice="" if is_mys

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

如何从is_inactive_实例调用is_active_实例,并对其返回进行否定以将True返回主程序

我已经试着呼叫正在使用的实例!更改原始函数的结果

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
}

另请参阅,了解如何使您的
处于活动状态\u实例
工作。

不要使用任何
[
s:

is_inactive_instance(){
    ! 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 ${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

另请参见如何使您的
处于活动状态\u实例
工作。

请注意,与其将
然后
放在新行上,通常使用分号并将其放在同一行上:
如果处于活动状态\u实例;那么
。这会减少对样板语法的强调,从而使代码更易于阅读。这不是一个通用的重新定义推荐,但对于那些想在不适合它的语言上使用K&R缩进样式的人来说,这是一种选择。请注意,与其将
then
放在新行上,通常使用分号并将其放在同一行上:
如果是活动的\u实例;那么
。这会使代码更易于阅读,减少emph值这不是一个一般性的建议,但对于那些想在一种不适合K&R缩进风格的语言上使用这种风格的人来说,这是一个选择。
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