lundi 30 mars 2020

False positive in Bash condition

I am writing a bash script to check on multiple servers if Tomcat 5 is installed. When running the script, even if the find command stored in the variable command returns nothing, the tomcat variable is still set to '1' in the if statement.

I know there must be something wrong with my if statement but I'm not sure what. Below is the function that runs the command on the remote server

function search() {

        local login=$1
        local server_fqdn=$2

        local command="sudo find / -name \"tomcat-5*\""
        ssh -oBatchMode=yes -o ConnectTimeout=6 $login@$server_fqdn "$command"
        if [ "$?" = 0 ];
        then
                echo "Tomcat found"
                tomcat=1
        else
                echo "No tomcat here!"
        fi
}

How can I get the function to enter the 'else' condition if command returns nothing?

Aucun commentaire:

Enregistrer un commentaire