我正在检查golang中的一些shell命令输出。我需要检查输出是否包含我的输入字符串。
//myinput is a valid string
stdoutput2,stderr2,err2 := Shellout("some valid shell command | grep "+myinput)
if(err2!=nil){
glg.Error("Not able to retrive information from system")
glg.Error(err2)
return false
} else if strings.Contains(strings.TrimSpace(stdoutput2),myinput){
glg.Info("able to retrive information from system")
return true
} else {
glg.Error("Not able to retrive information from system")
glg.Error("stdoutput2 after trimming is-->"+strings.TrimSpace(stdoutput2))
glg.Error("myinput is :"+myinput)
glg.Error("std error is :"+stderr2)
return false
}
尽管stdoutput2包含myinput的值,但如果条件失败,则为else。
最终输出是
无法从系统检索信息。
修剪后的stdoutput2是->名称= customcsi-b35f3ea733
myinput是:customcsi-b35f3ea733
请注意名称和=符号之间的空格。那是输出的一部分。
请帮助解决此问题。我的golang版本是1.13.5
任何帮助将非常感激。
最佳答案
myinput
的结尾是否有换行符?
你可以试试
stdoutput2, stderr2, err2 := Shellout("some valid shell command | grep " + myinput)
myinput = strings.TrimSpace(myinput)
关于string - string.Contains函数无法在我的golang代码中正常工作。尽管第一个字符串包含第二个字符串,但条件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59674872/