具有控制结构的基本 Shell 脚本.计算目录和文件的数量以及它们是否可读可写和可执行 [英] Basic Shell scripting with control structures. Counting the number of directories and files and whether they are readable writeable and executable
问题描述
编写一个 shell 脚本,计算给定目录内容的以下信息.该目录将被指定为脚本的单个命令行参数.您不需要考虑嵌套子目录的任何内容——只需考虑指定目录的直接内容.
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.
我试过了:
if [ -d $file ]然后counter=expr $counter + 1
echo "目录数:" $counter菲
if [ -d $file ]
then
counter=expr $counter + 1
echo "Number of directories :" $counter
fi
推荐答案
我不会为您编写脚本,但获得答案的一种方法是使用 ls -l
与grep -c
,使用 grep 解析出 ls -l
会给你的适当文件系统标志(drwxrwxrwx
列).关键是知道如何用 grep
解析这一列.
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
.
它可能不是最有效的(因为我们多次执行 ls
,但它会给你你想要的.
It may not be the most efficient (as we're doing ls
multiple times, but it will give you what you're looking for.
这篇关于具有控制结构的基本 Shell 脚本.计算目录和文件的数量以及它们是否可读可写和可执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!