user2786596
user2786596

Reputation: 145

Unix, displaying message "no such directory"

#!/bin/sh

LOCATION=$1

if [ "$#" -ne "1" ]
then
echo "Usage:./test1 <directory_name>"

else
echo "Number of directories: $(find $LOCATION -type d | wc -l) "
echo "Number of files: $(find $LOCATION -type f | wc -l)"
echo "Number of readable: $(find $LOCATION -type f -readable | wc -l )"
echo "Number of writable: $(find $LOCATION -type f -writable | wc -l )"
echo "Number of executable: $(find $LOCATION -type f -executable | wc -l )"
fi

if [ $Location does not exist? ]
then
echo "This Directory does not exist"
if

I am pretty new to unix, i am not sure how to do the last part? if the the directory does not exist, what should i say inside?

Upvotes: 1

Views: 54

Answers (1)

MattSizzle
MattSizzle

Reputation: 3175

That is an easy one to implement. Bash included File Test Operators that provide such functionality.

Example:

    if [ ! -d $LOCATION ]
    then
        echo "This Directory does not exist"
    fi

EDIT: Also move that to the top so if the directory does not exist you do not waste time and resources checking a non-existant directory. This will also avoid further warnings.

Example:

#!/bin/sh

LOCATION=$1

if [ "$#" -ne "1" ]
then
echo "Usage:./test1 <directory_name>"
else
   if [ -d $LOCATION ]
   then
        echo "Number of directories: $(find $LOCATION -type d | wc -l) "
        echo "Number of files: $(find $LOCATION -type f | wc -l)"
        echo "Number of readable: $(find $LOCATION -type f -readable | wc -l )"
        echo "Number of writable: $(find $LOCATION -type f -writable | wc -l )"
        echo "Number of executable: $(find $LOCATION -type f -executable | wc -l )"
   else
        echo "This Directory does not exist"
   fi
fi

Hope that helps.

Upvotes: 2

Related Questions