首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Unix shell脚本中检查diff命令是否成功

如何在Unix shell脚本中检查diff命令是否成功
EN

Stack Overflow用户
提问于 2022-07-23 10:57:40
回答 3查看 111关注 0票数 0

在Unix脚本中,我希望通过if语句检查diff命令的输出是否等于0。这样我就可以相应地给出if case语句。

例如: Abc.sh

代码语言:javascript
复制
#!/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

输出:

代码语言:javascript
复制
value of a is
0
[: ==: unary operator expected
checksums do not match
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-07-23 11:10:37

修复脚本的方法很多,但这可能是最起码的方法:

代码语言:javascript
复制
a=$(cat a)
if [ "$a" = "" ]; then

created.

  • Variables变量与文件不同,因此您必须读取可能包含空格的文件的输出,并且必须将字符串与空的而不是0.

进行比较。

(正如注释中所建议的那样,如果不需要diff输出,则只需使用退出代码即可。)

票数 0
EN

Stack Overflow用户

发布于 2022-07-23 11:19:19

检查一下迪夫。

代码语言:javascript
复制
if diff s1 d1 ; then
   echo "checksums match"
else
   echo "checksums do not match"
fi

如果不希望终端上有输出,可以使用cmp

代码语言:javascript
复制
if cmp -s s1 d2 ; then ...
票数 1
EN

Stack Overflow用户

发布于 2022-07-24 01:25:59

更新1:关于短路的快速记录

在您开始考虑哪些比较工具之前,请通过stat命令检查它们的文件大小。

如果连这些都不匹配,那么任何哈希算法在统计上都不可能报告匹配的散列。

我自己用的一种方法是

检查文件大小的xxhash.

  • then
  1. 检查文件大小
  2. ,在上进行高速批处理运行--最后,只对步骤2中建议的副本执行,通过密码上可接受的散列(如SHA3系列的KeccakShake )再次运行它们,以确认它们确实是绝对重复的H 215G 216

==============================

如果您只有一个awk块,但是没有从文件或管道读取任何内容,那么NR = 0就有了一个有用的特性。

还可以使用三元操作符… ? … : …对这两种场景重用相同的字符串,方法是利用0th-power of any base

如果你没有太多的文件,这里有一个相当野蛮的力量,但高速的方式来做到这一点。

代码语言:javascript
复制
--just remove the bckgrnd placing bit "... & )"  if u wanna do it sequentially 

|

代码语言:javascript
复制
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 match
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73090135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档