首页 > 解决方案 > 显示路径中目录的详细信息 - bash 脚本分配

问题描述

我有一个需要创建脚本的任务。我想要一些帮助。我一直在挣扎,此时我迷路了,不知道如何继续。我必须创建一个脚本,该脚本将确保路径中的所有项目都是目录,然后继续显示这样的目录详细信息。

如果./pathdisplay ~unx510/sample.dir1/testdir1输入,则显示:

Owner   Group   Other   Filename 
-----   -----   -----   -------- 

r w x   r - x   r - x   / 

r w x   r - x   r - x   home 

r w x   - - x   - - x   unx510 

r w x   r - x   r - x   sample.dir1 

r w x   r - x   r - x   testdir1 
  Links: 4  Owner: unx510  Group: users  Size: 229  Modified: Feb 22 2015



Valid commands:  u(p)  d(own)  q(uit) 

这是我到目前为止所拥有的:

if [ $# -ne 1 ]; then
    echo "Usage: ./pathdisplay [ dir-name ]" >&2
    exit 1

elif [ ! -d "$1" ]; then
    echo "$1 is not a valid directory name" >&2
    exit 1
else 
    allDirectories=$(tr '/.' '\n' < <(printf "%s" $1))
    numOfDirectories=$(tr '/.' '\n' < <(printf "%s" $1) | wc -l)

    SAVEIFS=$IFS   
    IFS=$'\n'      
    directories=($allDirectories) 
    IFS=$SAVEIFS

    for (( i=0; i<${#directories[@]}; i++ ))
    do
        if [ $i -le 0 ]; then
            $(ls  ${directories[$i]})
        else
               tempIndex=$i 
        previousDirectory=${directories[i--]}
            $(ls '$previousDirectory/'${directories[$i]})
        fi
    done


    exit 0
fi

我不太明白如何遍历目录。因为我需要指定上一个目录。然后使用命令来显示屏幕,以显示权限和详细信息。任何帮助,将不胜感激。

https://ideone.com/Dhj7Sq - 到目前为止的 bash 脚本链接

http://czegel.com/seneca/unx510-dps918/assign1a/Assign1a.html - 分配规范

http://czegel.com/seneca/unx510-dps918/assign1a/Assign1b.html - 作业截图

标签: linuxbashscripting

解决方案


使用 'find' 命令列出你想要的所有目录,你可以使用 stat 生成输出

find . -maxdepth 1 -type d|xargs stat -c "%A %n, links:%h, owner:%U, group:%G, size:%B, modified:%y"

或者您可以只使用 find 命令循环迭代元素并生成更多格式化输出


推荐阅读