修改 bash 脚本,使其在检查实体类型后,还列出输入实体的详细信息

Modify the bash script so that after checking the type of the entity, it also lists the details of the input entity

PASSED=

if [ -f $PASSED ]; then
        echo "$PASSED is a file"
        ls -l $PASSED
elif [ -d $PASSED ]; then
        echo "$PASSED is a directory"
        ls -l $PASSED
else
        "$PASSED is invalid"
fi

当我在终端输入文件时,比如 demo.sh,输出正确打印为: “demo.sh 是一个文件” rwxr-xr-x 1 system system 12 Jan 16 03:12 26 14:47 demo.sh

但是对于一个目录,比如云,它给出: 云是一个目录 总计 0

我应该怎么做才能纠正这个问题?

enter image description here

如 ls 的手册页所示:

   -d, --directory
          list directories themselves, not their contents

在您当前的实施中,因为您只对目录使用 -l,ls 将显示目录的内容。要仅列出目录的属性,除了 -l 之外还使用 -d 等等:

ls -ld $PASSED