需要帮助以明文显示文件/文件夹权限

需要帮助以明文显示文件/文件夹权限

我需要帮助以清晰易懂的文本显示权限。

rwx-wx---

User permission: read, write, execute
Group permission: write, execute 
Other permission: No permission

我需要这个脚本让非技术用户更容易理解。

答案1

这个脚本可以解决问题(我调用了该脚本filestat- 将它放在你的路径中):

#!/bin/bash

# Iterate over each argument
for file in "$@"; do
  perm_type=('User' 'Group' 'Other')
  (( j = 0 ))

  # Check if file exists
  if [[ -e "$file" ]]; then

    # Print filename
    echo -e "\nFilename: $file"

    # Isolate permission octet
    perm_octet=$( stat -c "%a %n" "$file" | cut -d ' ' -f 1 )

    # Add each value of octet to array
    perm_array=()
    for (( i = 0; i < "${#perm_octet}"; i++ )); do
      perm_array+=("${perm_octet:$i:1}")
    done

    # Iterate over array
    for x in "${perm_array[@]}"; do

      # Print permission type and increase counter
      echo -n "${perm_type[$j]} permission: "
      (( j++ ))

      # Check if permission is zero (none), print and start next iteration
      if (( "$x" == 0 )); then
        echo "NONE "
        continue
      fi

      # Check if permission has "read", print and subtract 4
      if (( "$x" > 3 )); then
        echo -n "read "
        (( x = x - 4 ))
      fi

      # Check if permission has "write", print and subtract 2
      if (( "$x" > 1 )); then
        echo -n "write "
        (( x = x - 2 ))
      fi

      # Check if permission has "execute", print and subtract 1
      if (( "$x" > 0 )); then
        echo -n "execute "
        (( x = x - 1 ))
      fi

      echo ""
    done

  fi

done


编辑:接受任意数量的文件作为输入,并检查文件是否存在。示例输出:

$ filestat ~/.bashrc ~/.config

Filename: /home/am/.bashrc
User permission: read write
Group permission: read write
Other permission: read

Filename: /home/am/.config
User permission: read write execute
Group permission: NONE
Other permission: NONE

相关内容