Basic Shell scripting with control structures. Counting the number of directories and files and whether they are readable writeable and executable

青春壹個敷衍的年華 提交于 2019-12-24 16:29:25

问题


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.



来源:https://stackoverflow.com/questions/4340974/basic-shell-scripting-with-control-structures-counting-the-number-of-directorie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!