I think before I go too far, we need to clearly define the problem that you have been set.
Imagine we have this structure in our file-system:
Code:
/path/to/directory/
file1
file2
file3
/subdirectory1/
file4
/subdirectory2/
file5
file6
So the files: file1, file2, file3 and sub-directories subdirectory1 and subdirectory2 are all in a directory at /path/to/directory/
If the user enters /path/to/directory/file1, your script should output that the target was a file and the permissions for file1 and that is that. I get that much.
But if the user enters a directory -
e.g. /path/to/directory/
- how far down the rabbit hole is your script supposed to go?
Should it just output the permissions for the main directory (/path/to/directory) and the permissions and types of it's contents:
e.g.
files 1,2,3 AND subdirectories 1 and 2?
Or does it also need to recurse into the subdirectories and list the content/permissions for files/directories they contain?
I just want to get a firm grasp of the problem you are trying to solve so I don't end up suggesting unnecessary things.
One thing that might help would be putting the permissions checking code into a function.
That way, whenever you want to check the permissions of a file or directory, you can simply call the function and pass it the file/directory to check.
So perhaps, you get the name of the file/directory to check. If it is a file, call your checkpermissions function on the file and end.
If the target is a directory output the permissions for the directory:
Code:
checkpermissions "$checkdir"
Then get each file-system object in the directory and pass it to the checkpermissions function:
Code:
for fsObj in "$checkdir"/*; do
checkpermissions "$fsObj"
done
That would work with the top-level of things.
e.g. If you were only interested in the directory's immediate, top-level contents.
If full recursion is required, it would require a slightly different approach.
With the full-recursion, you'd do something like:
1. Get the name of the file/directory to check
2. Pass the object to the checkpermissions function
The checkpermissions function would deal with determining if the passed-in object is a file or a directory.
If it is a file - it outputs the permissions and returns.
If it is a directory, it uses something like my "for fsObj.." example to list the content of the directory and call checkpermissions on each object - so in other words - checkpermissions recursively calls itself each time it is passed a directory.
I hope that makes some sense!
[EDIT]
Also, what if a directory contains something that is NOT a file or a directory - like a symbolic link (which could point to a file or a directory)? Should your script ignore it? or report it?
[/EDIT]