如何:使用 bash 将命令输出与文本文件进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21889107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:37:56  来源:igfitidea点击:

How to: compare command output to text file with bash

linuxbashdiffls

提问by jlconlin

I'm trying to compare the output of the command

我正在尝试比较命令的输出

`ls -l directory` 

with a file that I've created using the same command. (I'm trying a poor man's way of making sure no files have been modified.)

使用我使用相同命令创建的文件。(我正在尝试一个穷人的方式来确保没有文件被修改。)

The trouble is, I don't know how to diffthe output of the lscommand with the file that I've created. I've tried the following and each time it doesn't work

问题是,我不知道如何使用我创建的文件diff输出ls命令。我尝试了以下方法,但每次都不起作用

diff file.ls <(ls -l directory)
ls -l directory | xargs diff file.ls
ls -l directory | diff file.ls
diff file.ls < `ls -l directory`

What is the magic command to compare the output of lsto a file that I've already saved?

将输出与ls我已经保存的文件进行比较的神奇命令是什么?

回答by jlconlin

The answer (for posterity) is to do the following

答案(对于后代)是执行以下操作

diff file.ls <(ls -l directory)

When I did this previously, the output was blank. I thought I had done it wrong; in actuality there was no difference between the contents of the directory and my file.

当我以前这样做时,输出是空白的。我以为我做错了;实际上,目录的内容和我的文件之间没有区别。

<\facepalm>

<\面掌>

回答by robert

diffis easiest when you compare files.

diff比较文件时最简单。

$ ls $DIR > original.ls
do some stuff
$ ls $DIR > new.ls
$ diff original.ls new.ls