[英]Bash Shell Script: Using Diff command
谁能帮我弄清楚该程序有什么问题吗?
#!/bin/bash
find teste1 > names.lst
find teste2 > names2.lst
result=$(diff -y -W 72 $names $names2)
if [ $? -eq 0]; then
echo "files are the same"
else
echo "files are different"
echo "$result"
fi
它返回以下错误:
差异:缺少操作数
teste.sh:[:缺少']'
文件不同
(此处出现空白)
空格是变量“结果”,但是为什么它没有保存两个文件之间的差异?
我正在尝试使用diff来找出这两个文件的文字差异。
除了比较未定义变量$ names和$ names2而不是创建的文件(names.lst和names2.lst)之外,还有一些语法错误:需要在方括号中加一个空格以执行条件语句。
#! /bin/bash
find teste1 > names.lst
find teste1 > names2.lst
result=$(diff -y -W 72 names.lst names2.lst)
if [ $? -eq 0 ]
then
echo "files are the same"
else
echo "files are different"
echo "$result"
fi
teste.sh: [: missing ']'
:您在0
后错过了一个空格
变量$name
和$name2
似乎没有填充。
但是,在最近的bash中执行此操作不需要编写脚本:
result="$(diff -y <(find teste1) <(find teste2))" &&
echo files are the same ||
{ echo files differ; echo $result; }
要么
result="$(diff -y <(find teste1) <(find teste2))" &&
echo files are the same || printf "files differ:\n%s" "$result"
其主要优点之一是不需要临时文件 。
当然,这可以被正确地编写并且更具可读性:
#!/bin/bash
files=(
"/path1/teste 1"
"/path2/teste 2"
)
if result="$(
diff -y -W78 <(
find ${files[0]}
) <(
find ${files[1]}
) )"
then
echo "Files are the sames"
else
echo "Files are differents"
echo "$result"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.