linux中如何用脚本判定群组权限

用脚本判定一个叫test.txt的文件的群组权限,首先要了解ls的输出格式。在用ls -l test.txt 后,输出行从最左面开始,第4,5,6三个字符表示该文件的群组权限。比如

-rwxr-x--x

中的 r-x 表示群组有读和运行权。

var=$(ls -l test.txt)

echo ${var:4:3}

r=${var:4:1}

w=${var:5:1}

x=${var:6:1}

if [ $r == "-" ]

then echo "group can not read it"

else echo "group can read it"

fi

if [ $w == "-" ]

then echo "group can not write it"

else echo "group can write it"

fi

if [ $x == "-" ]

then echo "group can not run it"

else echo "group can run it"

fi