我需要为具有类似u=rwx,g=srwx,o-rwx
(可能是粘性位)的符号符号符号的文件或文件夹获取Access rights in
更多human readable
格式
- 使用
stat --format '%a'
,我获得了格式为2770
、八进制格式的结果 - 使用
stat --format '%A'
,我获得了drwxrws---
格式的结果,人类可读
我需要一个命令来获得类似u=rwx,g=srwx,o-rwx
(与chmod symbolic modes
兼容)的格式
[u|g|o]
:适用于user
/group
/other
或适用于所有a
[=]
:授予的权利[rwxst]
:授予无订单重要性的权利列表[-rwx]
:用于撤销的权利(如果未授予权利)
我试过这个(但它不能处理所有情况,特别是粘性部分):
stat --format '%A' temp |
sed -E 's/^.(...)(...)(...)/u=1,g=2,o=3/g' | # split by triplet
sed 's/=---/-rwx/g' | # revoker grants
sed 's/rws/srwx/g' | # setgid with x ...
sed 's/--S/s/g' | # setgid without x ...
sed ... nead more transormation... # manage sticky bit setuid setgid
我寻求一种更优雅的方式。
示例输入===>输出
drwxrws---
=>u=rwx,g=srwx,o-rwx
(从d==>目录开始)drwxrwxrwx
=>u=rwx,g=rwx,o=rwx
或ugo=rwx
或a=rwx
-r-xrw-r--
===>u=rx,g=rw,o=r
(以-===>常规文件开头)-rwx--S---
=>u=rwx,g=s,o-rwx
(大写S)------s--t
==>u=-srwx,g=sx,o=xt
(Stickybit)
输入格式==>类似命令stat
或ls -al
输出格式==>必须与chmod
兼容
这个完整的版本似乎有效,但我相信我们可以简化它,(即没有多个sed)
stat --format '%A' a |
sed -E 's/^.(...)(...)(...)/u=1,g=2,o=3/g' | # split by triplet
sed -E 's/s/xs/g' | # setgid ou setuid with x ...
sed -E 's/t/xt/g' | # sticky bit with x ...
sed -E 's/S/s/g' | # setgid ou setuid without x ...
sed -E 's/T/t/g' | # sticky bit alone
sed -E 's/-//g' | # remove -
sed -E 's/=(,|$)/-rwx1/g' # revoker grants
sed
命令采用多个-e 'sed-command'
选项,因此修复代码以使其使用一次sed
:非常简单
stat --format '%A' a |
sed -E -e 's/^.(...)(...)(...)/u=1,g=2,o=3/g'
-e 's/s/xs/g'
-e 's/t/xt/g'
-e 's/S/s/g'
-e 's/T/t/g'
-e 's/-//g'
-e 's/=(,|$)/-rwx1/g'
你不能使用尾随评论,但这与你展示的内容相同。您的s/s/xs/g
操作与您所描述的所需输出不匹配(对于具有组执行权限的SGID,您显示的是sx
,而不是xs
)。有些人会把所有这些选项组合成一个-e
选项,用分号分隔表达式。我更喜欢使用-e
来分隔单独的选项。从文件中读取脚本的-f script.sed
选项有时是合理的。我认为这个脚本还没有达到这个阈值,但不要忘记这个选项是存在的。
情人眼里出西施。我不相信权限的替代表示法比普通表示法好得多,但也许我只是使用Unix太久了。
最后,我发现的更简洁的命令是:
stat --format '%A' myFile | sed -E -e 's/(...)(...)(...)$/u=1,g=2,o=3/;s/(s|t)/x1/g;s/(S|T)/l1/g;s/-//g;s/(u|g)=,/1-rwxs,/g;s/o=$/o-rwxt/g'
我更喜欢找到本机命令
可以肯定的是,我写了一个详尽的bash来测试所有权利的组合(8*8*8=8=4096种情况!)
test_file=/tmp/a
return_code=0
echo -e "from itertools import product;nfor mod in product('01234567', repeat=4) : print ''.join(mod)" | python | shuf |
while read octalMod
do
chmod $octalMod $test_file # apply octal mod
# get symbolic mod
symbolicMod=$(stat --format '%A' $test_file | sed -E -e 's/(...)(...)(...)$/u=1,g=2,o=3/;s/(s|t)/x1/g;s/(S|T)/l1/g;s/-//g;s/(u|g)=,/1-rwxs,/g;s/o=$/o-rwxt/g')
chmod 0000 $test_file # reset mod
chmod $symbolicMod $test_file # apply symbolic mod
modActual=$(stat --format '%a' $test_file) # get actual mod in octal format to compare it with the original
if [ $octalMod -ne $modActual ]
then
echo "$octalMod ($symbolicMod) !!! octalMod=$octalMod <> modActual=$modActual" >&2
return_code=255
else
echo "$octalMod ($symbolicMod) octalMod=$octalMod == modActual=$modActual" >&1
fi
done
exit $return_code
完整列表:
test_file=/tmp/a
echo -e "octalModthumanModtsymbolicMod"
echo -e "from itertools import product;nfor mod in product('01234567', repeat=4) : print ''.join(mod)" | python |
while read octalMod
do
chmod $octalMod $test_file # apply octal mod
# get symbolic mod
symbolicMod=$(stat --format '%A' $test_file | sed -E -e 's/(...)(...)(...)$/u=1,g=2,o=3/;s/(s|t)/x1/g;s/(S|T)/l1/g;s/-//g;s/(u|g)=,/1-rwxs,/g;s/o=$/o-rwxt/g')
humanMod=$(stat --format '%A' $test_file)
echo -e "$octalModt$humanModt$symbolicMod"
done
echo -e "octalModthumanModtsymbolicMod"
样本结果:
octalMod humanMod symbolicMod
0001 ---------x u-rwxs,g-rwxs,o=x
0354 --wxr-xr-- u=wx,g=rx,o=r
1210 --w---x--T u=w,g=x,o=t
3337 --wx-wsrwt u=wx,g=wxs,o=rwxt
3566 -r-xrwSrwT u=rx,g=rws,o=rwt
3734 -rwx-wsr-T u=rwx,g=wxs,o=rt
6734 -rws-wsr-- u=rwxs,g=wxs,o=r
7121 ---s-wS--t u=xs,g=ws,o=xt
7430 -r-S-ws--T u=rs,g=wxs,o=t
7611 -rwS--s--t u=rws,g=xs,o=xt