I need to show for a folder (lab_3il) and its 4 subfolders (aa bb cc dd) whether the user has write permissions or not and output it in 2 files
-dir_with_write_perm.rep
-dir_without_write_perm.rep
The folder should be passed as an argument (ex. exe_3il.ksh lab_3il) And also create a log file.
I've tried with the while getopts but it didn't work.
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: $1" | tee -a $LOG
# find out if folder has write permission or not
[ -w $1 ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG
echo "Subfolder: $2" | tee -a $LOG
[ -w $2 ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG
echo "Subfolder: $3" | tee -a $LOG
[ -w $3 ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG
echo "Subfolder: $4" | tee -a $LOG
[ -w $4 ] && W="Write = yes" || W="Write = No"
echo "$W" | tee -a $LOG
I expect to output whether a file can be written in a given subfolder or not (by the user).
Some implementation without getopts
that may not be xactly what you want but can show you off how you could achieve similar result:
#!/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 "$1" ]; do
subfolder="$1" && 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