在Unix脚本中,我希望通过if语句检查diff命令的输出是否等于0。这样我就可以相应地给出if case语句。
例如: Abc.sh
#!/bin/bash
cd users
diff s1 d1 > a.txt
wc -l a.txt | awk '{print f1}' > a
echo "value of a is"
cat a
if [ $a == 0 ]
then
echo "checksums match"
else
echo "checksums do not match"
fi输出:
value of a is
0
[: ==: unary operator expected
checksums do not match发布于 2022-07-23 11:10:37
修复脚本的方法很多,但这可能是最起码的方法:
a=$(cat a)
if [ "$a" = "" ]; thencreated.
进行比较。
(正如注释中所建议的那样,如果不需要diff输出,则只需使用退出代码即可。)
发布于 2022-07-23 11:19:19
检查一下迪夫。
if diff s1 d1 ; then
echo "checksums match"
else
echo "checksums do not match"
fi如果不希望终端上有输出,可以使用cmp。
if cmp -s s1 d2 ; then ...发布于 2022-07-24 01:25:59
更新1:关于短路的快速记录
在您开始考虑哪些比较工具之前,请通过stat命令检查它们的文件大小。
如果连这些都不匹配,那么任何哈希算法在统计上都不可能报告匹配的散列。
我自己用的一种方法是
检查文件大小的xxhash.
SHA3系列的Keccak或Shake )再次运行它们,以确认它们确实是绝对重复的H 215G 216==============================
如果您只有一个awk块,但是没有从文件或管道读取任何内容,那么NR = 0就有了一个有用的特性。
还可以使用三元操作符… ? … : …对这两种场景重用相同的字符串,方法是利用0th-power of any base。
如果你没有太多的文件,这里有一个相当野蛮的力量,但高速的方式来做到这一点。
--just remove the bckgrnd placing bit "... & )" if u wanna do it sequentially |
for f1 in "${m2p}" "${m3l}" "${m3m}"; do
for f2 in "${m3m}" "${m2a}" "${m2p}" "${m3supp}" ; do
( diff "${f1}" "${f2}" |
{m,g}awk -F'^$' -v __="${f1}" -v ___="${f2}" '
END {
____=ENVIRON["HOME"]
sub(____,"~",__)
sub(____,"~",___)
print "checksums for \n\t\42"(__)"\42\n\t\b\b\b\band \42"\
(___)"\42 ===> " \
substr(" DO NOT match", ((_+= ++_)^++_+!!_)^!NR)"\n" }' &)
done ; done | gcat -b
1 checksums for
2 "~/m2map_main.txt"
3 and "~/m2map_main.txt" ===> match
4 checksums for
5 "~/m3vid_genie26.txt"
6 and "~/m3vid_genie26.txt" ===> match
7 checksums for
8 "~/m3vid_genie26.txt"
9 and "~/m2art_main_03.txt" ===> DO NOT match
10 checksums for
11 "~/m3vid_genie26.txt"
12 and "~/m3vid_genie25_supp.txt" ===> DO NOT match
13 checksums for
14 "~/m23lyricsFLT_05.txt"
15 and "~/m3vid_genie26.txt" ===> DO NOT match
16 checksums for
17 "~/m23lyricsFLT_05.txt"
18 and "~/m2art_main_03.txt" ===> DO NOT match
19 checksums for
20 "~/m23lyricsFLT_05.txt"
21 and "~/m3vid_genie25_supp.txt" ===> DO NOT match
22 checksums for
23 "~/m3vid_genie26.txt"
24 and "~/m2map_main.txt" ===> DO NOT match
25 checksums for
26 "~/m2map_main.txt"
27 and "~/m3vid_genie26.txt" ===> DO NOT match
28 checksums for
29 "~/m2map_main.txt"
30 and "~/m2art_main_03.txt" ===> DO NOT match
31 checksums for
32 "~/m2map_main.txt"
33 and "~/m3vid_genie25_supp.txt" ===> DO NOT match
34 checksums for
35 "~/m23lyricsFLT_05.txt"
36 and "~/m2map_main.txt" ===> DO NOT matchhttps://stackoverflow.com/questions/73090135
复制相似问题