0

How can I finish the script?

Linux version 3.10.0-957.21.3.el7.x86_64 .(Red Hat 4.8.5-36)

#!/bin/bash
echo "Enter the file name"
read x
if [ -f $x ]
then
    echo "This is a regular file"
else
    echo "This is a directory"
fi

Need modify script which will output all files and directory in /etc/ directory and indicate which one is what (e.g.:

dir1 is a directory
fileA is a file
dir2 is a directory

2nd part of the job I did. need help with

Barmar
  • 741,623
  • 53
  • 500
  • 612
Trap
  • 11
  • 3

1 Answers1

-1

Use a for loop instead of getting the filenames from the user.

#!/bin/bash
for file in /etc/*; do
    if [ -f "$file" ]
    then 
        echo "$file is a regular file"
    elif [ -d "$file" ]
    then 
        echo "$file is a directory"
    else 
        echo "$file is something else"
    fi
done

Don't forget to quote variables, in case the value contains a space. And there are other possibilities than just files and directories.

Barmar
  • 741,623
  • 53
  • 500
  • 612