使用 Ubuntu 20.04.4 LTS (Focal Fossa) 和 diff (GNU diffutils) 3.7
历史:
如何捕获和重用不同的结果?
在测试下面的脚本时...通过 将文件名中的一个字母从 abc.txt 更改为 Abc.txt 大写 A,
使两个目录变得不同。
脚本运行并显示:
diff -rq src1/ dest/ Differences were found. ________________________________________ dirs differ
脚本是:
src1='/media/u3/usb_stick' ; dest='/home/u3/Music/x' ;
diff -rq "$src1/" "$dest/" ;
if [ $? -eq 0 ] ; # testing exit status ( 0 <--> match ) No differences echo $? = exit code = 0 ;
then
match21=true ;
else
match21=false ;
fi ;
echo ;
if $match21 ;
then
tput setaf 12 ; echo "diff -rq "src1/" "dest/" " |tr '\n' ' ' ;
tput sgr0 ; echo "Match. Same inputs. All ok. No difference. Success. " ;
else
echo "diff -rq "src1/" "dest/" Differences were found. ________________________________________ dirs differ" ;
fi ;
但是接下来会出现一个问题...
当 diff 命令被注释掉时#
结果不正确:
diff -rq src1/ dest/ Match. Same inputs. All ok. No difference. Success.
以上结果不正确。
注释掉的#
脚本是
src1='/media/u3/usb_stick' ; dest='/home/u3/Music/x' ;
# diff -rq "$src1/" "$dest/" ;
if [ $? -eq 0 ] ; # testing exit status ( 0 <--> match ) No differences echo $? = exit code = 0 ;
then
match21=true ;
else
match21=false ;
fi ;
echo ;
if $match21 ;
then
tput setaf 12 ; echo "diff -rq "src1/" "dest/" " |tr '\n' ' ' ;
tput sgr0 ; echo "Match. Same inputs. All ok. No difference. Success. " ;
else
echo "diff -rq "src1/" "dest/" Differences were found. ________________________________________ dirs differ" ;
fi ;
如何修复差异脚本周围的副作用?
这意味着当不运行 diff 时,注释掉 #
# diff -rq "$src1/" "$dest/" ;
然后就没有消息或
显示减号,一个填充符:
-
作为一条消息。
答案1
src1='/media/u3/usb_stick' ; 目标='/home/u3/Music/x' ; # diff -rq "$src1/" "$dest/" ; 如果 [ $?-eq 0 ] ...
这里你检查$?
设置后$dest
=> $? 是否为0
,除非出现错误。
所以这不是“副作用”。