我想确保/ etc / file1和/ etc / file2不允许比644更多。我试过了
if [[ `stat /etc/file1 --format=%a | cut -b 1` -le "6" -a `stat /etc/file2 --format=%a|cut -b 1` -le 6 ]]; then echo "good"; else echo "bad"; fi;
但我得到“bash:条件表达式中的语法错误 bash:`-a'“
附近的语法错误我的上述计划只是重复三次(每个字节一次)。
我也不确定两种测试是否正常工作,因为我无法让它们在测试之外运行。
答案 0 :(得分:1)
您可以通过更简单的方式执行此操作:
<击> maxperms =&#34; 666&#34; 击>
<击>if [ `stat /etc/profile --format=%a` -gt $maxperms ]
then
echo "TOO PERMISSIVE"
else
echo "FINE"
fi
击> <击> 撞击>
糟糕,第二次切入:
for d in `stat test.txt --format=0%a | fold -w1`
do
if [ $d -gt "6" ]
then
echo "TOO PERMISSIVE"
exit
else
echo "FINE"
fi
done
答案 1 :(得分:1)
假设通过&#34;更容许比666&#34;你只是意味着&#34;有一个执行位设置&#34;那我相信
find $path -perm /111
做你想做的事。
扩展到644
权限使得find命令类似于:
find $path -perm /100 -o \( -perm /044 -a -perm /033 \)
我想。
我觉得很可能有一种聪明的方法可以从所需的权限中获取查找模式,但我不得不多考虑一下。
答案 2 :(得分:1)
我假设“更宽松”是指file1或file2在644以上的任何额外许可。
一些例子是:
这基本上意味着与644相比,在file1或file2中至少设置了一个额外的位。因此,如果您对644和file1进行了AND操作,则结果是这两者的通用位。
假设file1为640。644 AND 640将得出640。由于结果与file1的权限相同,因此您知道file1的权限至少是644的子集。
假设file2为700。644和700将得出600。600与700不同,因此必须在FILE2中设置644所没有的位(即不同的权限)。
TRANSFORM_VALUES
答案 3 :(得分:0)
你可以一点一点地比较它们:
#!/bin/bash
function has_good_perm {
local FILE STAT X
for FILE; do
STAT=$(exec stat -c '%a' "$FILE")
X=${STAT:0:1}
(( X & 1 )) && return 1 ## False if first number has executable bit e.g. 7, 5, 1
X=${STAT:1:1}
(( (X & 1) || (X & 2) )) && return 1 ## False if second number has executable bit or writable bit e.g. 7, 6, 5, 3, 1
X=${STAT:2:1}
(( (X & 1) || (X & 2) )) && return 1
done
return 0
}
if has_good_perm /etc/file1 /etc/file2; then
echo "All files are good!"
else
echo "Something's bad."
fi
或者
function has_good_perm {
local STAT X
STAT=$(exec stat -c '%a' "$1")
X=${STAT:0:1}
(( X & 1 )) && return 1
X=${STAT:1:1}
(( (X & 1) || (X & 2) )) && return 1
X=${STAT:2:1}
(( (X & 1) || (X & 2) )) && return 1
return 0
}
for FILE in /etc/file1 /etc/file2; do
if has_good_perm "$FILE"; then
echo "Good file: $FILE"
else
echo "Bad file: $FILE"
fi
done
答案 4 :(得分:0)
如果你需要“允许超过644”,你实际上搜索133 8 中的任何位(即:644 8 XOR设置了777 8 。
然后,根据man find
:
-perm /mode **Any** of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify `u', `g' or `o' if you use a symbolic mode. See the EXAMPLES section for some illustrative examples. If no permission bits in mode are set, this test matches any file (the idea here is to be consis‐ tent with the behaviour of -perm -000).
所以,这可能会起到作用:
find $path -perm /133
或者更确切地说是关于你的具体需求:
BADFILES=$(find /etc/file1 /etc/file2 -perm /133)
if [ -n "$BADFILES" ]
then
echo 'Bad!' 1>&2
fi