Basic Shell scripting with control structures. Cou

2019-08-10 19:58发布

问题:

write a shell script that calculates the following information for the contents of a given directory. The directory is to be specified as a single command line argument to your script. You do not need to account for any contents of nested subdirectories -- just the direct contents of the specified directory.

The total number of directories that are in the given directory (note that these are subdirectories). The total number of files in the given directory. The number of items (files/directories) in the current directory that are readable. The number of items (files/directories) in the current directory that are writable. The number of items (files/directories) in the current directory that are executable.

I tried :

if [ -d $file ] then counter=expr $counter + 1 echo "Number of directories :" $counter fi

回答1:

I won't write the script for you, but one way to obtain the answer is to use ls -l an association with grep -c, using grep to parse out the appropriate filesystem flags (the drwxrwxrwx column) that ls -l will give you. The key is knowing how to parse this column with grep.

It may not be the most efficient (as we're doing ls multiple times, but it will give you what you're looking for.