shell 判断4份文件内容是否两两一样 用 if 语句
时间: 2024-02-12 19:05:00 浏览: 18
你可以使用 `cmp` 命令和条件判断语句 `if` 结合使用,来判断 4 个文件内容是否两两相同。可以使用嵌套的 `if` 语句来完成此任务,例如:
```shell
if cmp -s file1.txt file2.txt
then
if cmp -s file1.txt file3.txt && cmp -s file2.txt file3.txt
then
echo "The contents of file1.txt, file2.txt, and file3.txt are the same."
# do something
else
echo "The contents of file1.txt, file2.txt, and file3.txt are different."
# do something else
fi
else
echo "The contents of file1.txt and file2.txt are different."
# do something else
fi
```
在这个例子中,首先使用 `cmp` 命令比较 `file1.txt` 和 `file2.txt` 两个文件的内容是否一致,如果一致,则继续比较 `file1.txt` 和 `file3.txt`、`file2.txt` 和 `file3.txt` 两个文件的内容是否一致。如果三个文件的内容都一致,则条件判断为真,执行第一个 `echo` 命令和后续的操作;否则条件判断为假,执行第二个 `echo` 命令和后续的操作。
你可以根据实际需求替换 `echo` 命令和后续的操作。如果需要判断更多的文件是否相同,可以在 `if` 语句中添加更多的 `cmp` 命令。