gpt4 book ai didi

linux - Bash 字符串比较不起作用

转载 作者:太空狗 更新时间:2023-10-29 11:10:44 26 4
gpt4 key购买 nike

我有以下 Bash 函数:

checkForUpdates() {
checkLatest
ret=$?
if [ $ret != 0 ]; then
return $ret
fi
count=0
for i in $(ssh $__updateuser@$__updatehost "ls $__updatepath/*${latest}*"); do
file="${i##$__updatepath}"
echo "$file" >> $__debuglog
if [ -f $__pkgpath/$file ]; then
remoteHash=$(ssh $__updateuser@$__updatehost "md5sum -b < $__updatepath/${file}")
localHash=$(md5sum -b < $__pkgpath/$file)
echo "${remoteHash:0:32} = ${localHash:0:32}" >> $__debuglog
if [ "${remoteHash:0:32}" != "${localHash:0:32}" ]; then
files[$count]=$file
count=$(($count + 1))
echo "Hashes not matched, adding $i" >> $__debuglog
fi
else
files[$count]=$file
count=$(($count + 1))
echo "$file missing" >> $__debuglog
fi
done

# Verify that the files array isn't empty.
if [ $count != 0 ]; then
return 0
else
return 33
fi
}

出于某种原因,remoteHash/localHash 比较总是返回 true。我添加了 echo ,这样我就可以看到哈希值,它们肯定是不同的,我不知道我哪里出错了。我尝试了不同的运算符(operator)但没有成功,这让我发疯!

最佳答案

这与您的问题无关,但更多的是一般性建议,首先也是最重要的 you shouldn't parse the output of ls而是使用 find -print0 这是一个示例:http://mywiki.wooledge.org/BashFAQ/001

还可以考虑使用 [[ 而不是 [ 参见:http://mywiki.wooledge.org/BashFAQ/031

现在关于您的代码,这部分:

checkLatest
ret=$?
if [ $ret != 0 ]; then
return $ret
fi

可以简单地写成:

checkLatest || return

而且你不需要在数组的索引上保留一个计数器,如果你将 var 初始化为一个空数组,比如 files=() 然后你可以用 files+=("$file") 你可以通过 "${#files[@]}"

获取计数

关于linux - Bash 字符串比较不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com