检查目录权限用户是否可以在其中创建文件

Check directory permissions whether the user can create files in or not

我需要显示一个文件夹 (lab_3il) 及其 4 个子文件夹 (aa bb cc dd) 用户是否具有写入权限,并将其输出到 2 个文件中

  -dir_with_write_perm.rep
  -dir_without_write_perm.rep

文件夹应作为参数传递(例如 exe_3il.ksh lab_3il) 并创建一个日志文件。

我试过 while getopts 但没用。

export LOG=storage_lab3il.log
>$LOG
while getopts ":aa:bb:cc:dd:" opt; do
case $opt in
aa)a="$OPTARG" ;;
bb)b="$OPTARG" ;;
cc)c="$OPTARG" ;;
dd)d="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" | tee -a $LOG
esac
done
echo "Subfolder: " | tee -a $LOG
# find out if folder has write permission or not
[ -w  ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG
echo  "Subfolder: " | tee -a $LOG
[ -w  ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG
echo "Subfolder: " | tee -a $LOG
[ -w  ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG
echo "Subfolder: " | tee -a $LOG
[ -w  ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG

我希望输出文件是否可以(由用户)写入给定的子文件夹。

一些没有 getopts 的实现可能不是您真正想要的,但可以向您展示如何实现类似的结果:

#!/usr/bin/env sh

# Logfile
LOG=storage_lab3il.log

# Main folder
folder=./lab_3il

# Erase log file
true >"$LOG"

# The permission files to write to depending if writable
permfile_writable="$folder/-dir_with_write_perm.rep"
permfile_readonly="$folder/-dir_without_write_perm.rep"

# Delete the permission files
rm -f -- "$permfile_writable" "$permfile_readonly" || true

# While there is a subfolder argument
while [ -n "" ]; do
  subfolder="" && shift # pull subfolder argument

  # Continue to next if subfolder is not a directory
  [ ! -d "$folder/$subfolder" ] && continue

  # Test if sub-folder argument is writable
  if [ -w "$folder/$subfolder" ]; then
    permfile="$permfile_writable"
    perm=yes
  else
    permfile="$permfile_readonly"
    perm=no
  fi

  # Append the sub-folder name
  # in its corresponding permission file
  echo "$subfolder" >>"$permfile"

  # Log: Writable = yes|no sub-folder argument name
  printf 'Writable = %s: %s\n' "$perm" "$subfolder" >>"$LOG"
done