Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较bash中2个文件之间的文件权限_Bash_Syntax - Fatal编程技术网

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

比较bash中2个文件之间的文件权限,bash,syntax,Bash,Syntax,我试图比较两个文件的权限,但似乎无法得到正确的结果。有人能帮忙吗 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

我试图比较两个文件的权限,但似乎无法得到正确的结果。有人能帮忙吗

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
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

属性相同

将if写为
if[“$act”=“$bac”](推荐)或<代码>如果[“$act”==“$bac”]
(不推荐)

sandbox-computer work # chmod 222 a
sandbox-computer work # ./compareFiles_02.sh
222
777
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

参考资料:

将if写成
if[“$act”=“$bac”](推荐)或<代码>如果[“$act”==“$bac”]
(不推荐)


参考资料:

请看一看:请看一看:为什么要提到不推荐的内容?没有解释原因?tldp指南已经过时,在某些情况下完全是错误的。强烈建议改为使用Greg的Bash指南。@andlrc==比较运算符在双括号测试中的行为与在单括号中的行为不同。答案中的链接使用
=
之间的空格应如答案所示。也要参考链接。为什么要提到不推荐的东西?没有解释原因?tldp指南已经过时,在某些情况下完全是错误的。强烈建议改为使用Greg的Bash指南。@andlrc==比较运算符在双括号测试中的行为与在单括号中的行为不同。答案中的链接使用
=
之间的空格应如答案所示。也请参考链接。#/bin/bash active=stat-c“%a”a backup=stat-c“%a”b echo“$active”echo“$backup”,如果[[“$active”=“$backup”]];然后echo“属性相同”或者echo“属性不同”fi这是我得到的沙盒计算机工作的输出。#/compareFiles_02.sh 777 777属性是相同的沙盒计算机工作。#chmod 222 a沙盒计算机工作。#沙盒计算机工作。#/compareFiles_02.sh 222 777属性是Same@MMouse您需要命令替换:
active=$(stat-c“%a”a)
。。。非
active=stat-c“%a”a
#/bin/bash active=stat-c“%a”a backup=stat-c“%a”b echo“$active”echo“$backup”,如果[[“$active”=“$backup”]];然后echo“属性相同”或者echo“属性不同”fi这是我得到的沙盒计算机工作的输出。#/compareFiles_02.sh 777 777属性是相同的沙盒计算机工作。#chmod 222 a沙盒计算机工作。#沙盒计算机工作。#/compareFiles_02.sh 222 777属性是Same@MMouse您需要命令替换:
active=$(stat-c“%a”a)
。。。非
active=stat-c“%a”a