How to list the content of a directory

Hi

I want to write a script that reads a directory name, checks if the directory exists and lists the content of that directory.

That's what I have at the moment.

function listDirectory 
{    
    echo "Give in a directory name"
    read name
    
    #Here I want to check if the directory exitst and then 
    #print the content of that directory (if exists off course)


    #If directory doesn't exists:
    echo "Directory doesn't exists. Closing the application..."
    exit 0   

}
listDirectory

...
read name

if [ -d "$name" ]; then
  echo yep
  ls "$name"
else
  echo nope
  exit
fi

Read the man page for: test

Hi,

Just some clue to help you find the answers :

Testing if it's a directory : [ -d $name ]
Print the content of a directory : ls $name

Good luck :slight_smile:

Edit: damn i was too slow to answer :slight_smile:

Thanks guys!

There's no need for ls :

 printf "%s\n" "$name/"*