【Share|Linux】个人权限查看check_permiss

2023-07-05  本文已影响0人  caokai001

⏰ 阅读时长:10 分钟
👀 面向人群:Linux用户
📒 适用场景:需要查询单个或多个文件or目录的权限

1.背景

image.png

但对于你来说 看不出自己对这个文件是否有rwx权限。

-rwxrwx---+ 1xxx xxx 655360 Jun 26 15:11 test.sas7bdat

⚙2.Usage

已经加载到env_setting里面,重新打开MobaXterm tab或source ~/.bashrc 即可使用;

$ check_permission <dirname_or_filename>

Step1: 进入指定的Folder下面
$ cdwin "Z:\xxx\current"

 Step2: 查看帮助信息及运行
$ check_permission -h
Usage:
cdwin "Z:\xxx\current"
check_permission ae.sas7bdat

$ check_permission
filename or dirname does not exist.

$ check_permission ae.sas7bdat
ae.sas7bdat has read permission.
ae.sas7bdat has write permission.
ae.sas7bdat has execute permission.</pre>

3.代码逻辑

Code

#!/bin/bash

#######################################  check_permission  ##########################################

function check_permission(){
 file_or_dir=$1
 ## 判断参数是否为空
 if [[ "$file_or_dir" = "--help" ]] || [[ "$file_or_dir" = "-h" ]]; then
 echo "Usage: "
 echo 'cdwin "Z:\xxx\current" '
 echo 'check_permission ae.sas7bdat'
 return 1
 fi

 # 判断目录或文件是否存在
 if [ ! -e "$file_or_dir" ];then
 echo "filename or dirname does not exist."
 return 1
 fi

 # 判断是否有读权限
 if [ -r "$file_or_dir" ];then
 echo "$file_or_dir has read permission."
 else
 echo "$file_or_dir does not have read permission."
 fi

 # 判断是否有写权限
 if [ -w "$file_or_dir" ];then
 echo "$file_or_dir has write permission."
 else
 echo "$file_or_dir does not have write permission."
 fi

 # 判断是否有执行权限
 if [ -x "$file_or_dir" ]; then
 echo "$file_or_dir has execute permission."
 else
 echo "$file_or_dir does not have execute permission."
 fi
}</pre>

# 4.Notes:

*   只支持单个文件或目录权限查询,如果需要批量查询可以使用循环(可优化)。如下:

### 查看全部权限列表
$ cdwin "Z:\xxx\current"
$ ls | while read id; do check_permission $id; done;
aecm.sas7bdat has read permission.
aecm.sas7bdat has write permission.
aecm.sas7bdat has execute permission.
ae.sas7bdat has read permission.
ae.sas7bdat has write permission.
ae.sas7bdat has execute permission.

### 查看目录下没有可读权限的文件
$ cdwin "Z:\xxx\current"
$ ls | while read id; do check_permission $id | grep "does not have read permission" ;done

😀欢迎评论交流~

上一篇下一篇

猜你喜欢

热点阅读