比较bash中2个文件之间的文件权限

时间:2016-06-05 18:16:06

标签: bash syntax

我正在尝试比较2个文件的权限,但似乎无法获得正确的结果。任何人都可以提供帮助。

act= stat -c "%a" a
bac= stat -c "%a" b

echo "$act"  # returns correctly eg 777, not sure if this is a string or no.
echo "$bac"  # returns correctly as above


if [[ "$act"="$ghi" ]];
then 
   echo "Correct"
else
   echo "Difference"
fi

目前这是我得到的 它说属性是一样的 1.第一次a = 777,b = 777 2.第二次a = 222且b = 777

#!/bin/bash

active= stat -c "%a" a
backup= stat -c "%a" b


echo "$active"
echo "$backup"

if [[ "$active" = "$backup" ]];
then 
        echo "Properties are The Same"
else
        echo "Properties are Different"
fi

这是我得到的输出

sandbox-computer work # ./compareFiles_02.sh
777
777

属性是相同的

sandbox-computer work # chmod 222 a
sandbox-computer work # ./compareFiles_02.sh
222
777

属性是相同的

2 个答案:

答案 0 :(得分:4)

act=$(stat -c "%a" file1)
bac=$(stat -c "%a" file2)

if [[ "$act" = "$bac" ]] # whitespaces added, $ghi replaced by $bac
then 
   echo "Correct"
else
   echo "Difference"
fi

答案 1 :(得分:0)

将if写为if [ "$act" = "$bac" ];(推荐)或if [ "$act" == "$bac" ](不推荐)。

参考文献:http://tldp.org/LDP/abs/html/comparison-ops.html