简体   繁体   English

如何在条件语句和循环中使用diff命令

[英]how to use diff command in conditional statements and in loops

I've created a script which recursively reads a path and prints its directory, and subdirectory into a csv file to write into excel columns. 我创建了一个脚本,该脚本以递归方式读取路径并将其目录和子目录打印到一个csv文件中,以写入excel列。 Now I want to modify this script such that first it will recursively read a path and print its content and then find the recursive difference of this path along with another path and I want to print if any directory is changed or not. 现在,我想修改此脚本,以使其首先以递归方式读取路径并打印其内容,然后找到该路径与另一个路径的递归差异,并且我想打印是否更改了任何目录。

First code: 第一个代码:

find path1 | while read file; do
  if [[ -d "$file" ]]; then
    echo $file > out.csv
  else
    echo...
  fi
done

and second code is: 第二个代码是:

diff -rq $path1 $path2 | while read file1; do
  if [[ "$file" != ]]; then
    echo Changed >> out.csv
  else
    echo no >> out.csv
  fi
done

Now I want to merge these codes such that fist it will recursively prints all content of path1 in csv file, then compare both paths and print changed if difference occur. 现在,我想合并这些代码,以便首先将其递归地打印csv文件中path1的所有内容,然后比较两个路径并在发生差异时打印更改。

It's not overly clear what you're trying to accomplish. 尚不清楚您要完成什么。 Your first part can be simplified to a single line: 您的第一部分可以简化为一行:

find path1 -type d > out.csv

which only finds directories. 仅查找目录。 (Note that the use of > overwrites out.csv , so previous contents don't matter (you made a mistake with that in your example)). (请注意,使用>覆盖out.csv ,因此之前的内容无关紧要(在示例中您对此有误)。

For you second part, you could do: 对于第二部分,您可以执行以下操作:

diff <( cd path1 && find . -type d ) <( cd path2 && find . -type d )

This grabs all the directories in path1, and all the directories in path2, and would output the difference in standard diff format... 这将获取path1中的所有目录以及path2中的所有目录,并以标准diff格式输出差异...

I'm using process substition <( ) so bash treats the output as files. 我正在使用进程替换 <( )因此bash将输出视为文件。 Also the cd to path1 or path2 before the find prevents find from outputting path1/ or path2/ in its results (otherwise every line would be different). 另外,在find之前,到path1path2的cd find阻止查找在其结果中输出path1/path2/ (否则每行都会不同)。 You could do something similar with sed to strip off the path names. 您可以使用sed进行类似操作以删除路径名。


If on the otherhand, your goal is to learn bash loops, you would do this as follows: 另一方面,如果您的目标是学习bash循环,则可以执行以下操作:

rm out.csv
find path | while read file; do 
    [[ -d $file ]] && echo $file >> out.csv
done

cat out.csv | while read file; do 
    [[ -e path2/${file#path1/} ]] || echo "file changed: $file"
done

Note that unlike the former example, this does not print any paths in path2 that are not in path1. 请注意,与前面的示例不同,此操​​作不会打印path2中不在path1中的任何路径。

Here, I used ${file#path1/} , which strips path1/ from the front of each filename (as you're now looking in path2 ). 在这里,我使用了${file#path1/} ,它从每个文件名的${file#path1/}剥离path1/ (就像您现在在path2查找的那样)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM