Linux系统shell脚本for循环实战之目录权限
Posted 江湖有缘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统shell脚本for循环实战之目录权限相关的知识,希望对你有一定的参考价值。
Linux系统shell脚本for循环实战之目录权限
一、脚本要求
1.要求输入任意目录:
若目录不在,输出该目录不存在
若目录存在,输出该目录下的文件或目录,且列出其权限
二、编写脚本
[root@192 scripts]# cat ./found_file.sh
#!/bin/bash
########################################
#Author:jeven
#time:Sun 15 May 2022 09:26:22 PM CST
#filename:found_file.sh
#Script description:
########################################
read -p "please input a dir :" dir
if [ "$dir" == "" -o ! -d "$dir" ];then
echo "the $dir is not exist!"
exit 1
fi
FILE=$( ls "$dir")
for filename in $FILE
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "the file $dir /$filename 's permission is $perm "
done
三、执行脚本
[root@192 scripts]# ./found_file.sh
please input a dir :/data/mysql
the file /data/mysql /file0 's permission is readable writable
the file /data/mysql /file1 's permission is readable writable
the file /data/mysql /file10 's permission is readable writable
the file /data/mysql /file2 's permission is readable writable
the file /data/mysql /file3 's permission is readable writable
the file /data/mysql /file4 's permission is readable writable
the file /data/mysql /file5 's permission is readable writable
the file /data/mysql /file6 's permission is readable writable
the file /data/mysql /file7 's permission is readable writable
the file /data/mysql /file8 's permission is readable writable
the file /data/mysql /file9 's permission is readable writable
以上是关于Linux系统shell脚本for循环实战之目录权限的主要内容,如果未能解决你的问题,请参考以下文章