简体   繁体   中英

Write a Bash script which will take a single command line argument (a directory) and will print each entry in that directory

Issue : Folders are treated as Files

My Code:

#!/bin/bash

for var in $(ls)
do
    echo $var
    if [ -e $var ]
    then
        echo "This is a file"
    else
        echo "This is not a file"
    fi
done

echo All Done

Current Contents of the Root Folder:

-rw-r--r-- 1 stibo stibo 401 Sep 17 2015 id_rsa.pub -rwxr-xr-x 1 stibo stibo 51 Jul 27 12:51 test.txt -rwxrw-r-- 1 stibo stibo 225 Aug 2 12:32 deletionScript.vi -rw-rw-r-- 1 stibo stibo 235 Aug 2 12:33 logdetails.txt -rw-rw-r-- 1 stibo stibo 123 Aug 2 12:42 path1.txt -rw-rw-r-- 1 stibo stibo 285 Aug 2 16:18 path2.txt -rw-rw-r-- 1 stibo stibo 0 Aug 3 13:42 ls -rw-rw-r-- 1 stibo stibo 164732 Aug 3 14:11 messages -rw-rw-r-- 1 stibo stibo 164732 Aug 3 14:11 wtmp -rwxrwxr-x 1 stibo stibo 160 Aug 4 15:34 newScript.vi -rw-rw-r-- 1 stibo stibo 160 Aug 4 15:41 Code.txt -rw-rw-r-- 1 stibo stibo 0 Aug 4 15:43 Details.txt

Where there are 4 folders and 12 files .

But when I run the script, I could see every thing is considered as a file, even if there are folders in it.

Can you please let me know where I am going wrong?

You've tested if a given entry exists with -e. Use -d for testing for directories and -f for files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM