go判断远程文件是否存在RemoteFileExist

func IsFileExist(filepath string) bool {
// if remote file is
// ls -l | grep aa | wc -l
fileName := path.Base(filepath) // aa
fileDirName := path.Dir(filepath)
fileCommand := fmt.Sprintf("ls -l %s | grep %s | wc -l", fileDirName, fileName)
data := strings.Replace(String("/bin/sh", "-c", fileCommand), "\r", "", -1)
data = strings.Replace(data, "\n", "", -1)
count, err := strconv.Atoi(strings.TrimSpace(data))
defer func() {
if r := recover(); r != nil {
logger.Error("[os][%s]RemoteFileExist:%s", filepath, err)
}
}()
if err != nil {
panic(1)
}
return count != 0
}
posted @ 2023-03-18 14:58  技术颜良  阅读(52)  评论(0编辑  收藏  举报