lundi 29 novembre 2021

Compare string with slice output not working as expected

I have written one program which will execute if both the remote server reachable, otherwise it will terminate the execution. For that i written below login but it did not produce as expected. Below is code snippet.

 str := "false"
 Comd1 := fmt.Sprintf("ping -c 1 %s  > /dev/null && echo true || echo false", serIP)
 op, err := exec.Command( "/bin/sh", "-c", Comd1 ).Output()
 fmt.Println(string(op))
     if err != nil || str  == string(op) {
        fmt.Println(err)
        return
     }

Whenver server ip not rechable it is expected to enter in if loop and terminate the program but it always skip the condition and execute further which leads panic which is expected as remote server is not rechable.

Any suggestion in code to compare string with []byte output would be highly appreciable.

Aucun commentaire:

Enregistrer un commentaire