I'm trying to write a script that will look for a file and then tell me if the file exists, and if it is a directory or just a file.
Here's my script:
Code:
echo "What is the file name you are looking for?"
read filename
echo "Searching for $filename..."
# We will only be working on the first file found
foundfile=`find /home/alex/ -name "$filename" | head -n1`
if [ -e $foundfile ]; then # If the file exists
if [ -d $foundfile ]; then # If it is a directory
echo "$foundfile is a directory"
else
echo "$foundfile is a file"
fi
else
echo "No file named $filename could be found."
echo "Try modifying your search criteria and run again."
fi
The problem: If the file doesn't exist, I'm told that it's a directory.
Code:
alex@leet:~/school/OpSysII/lab6$ ./ifthen.sh
What is the file name you are looking for?
sfdasfdasfdasfdkljoiux8o88
Searching for sfdasfdasfdasfdkljoiux8o88...
is a directory
In this special instance, $foundfile contains the single hex character 0A, so why does bash think newlines are directories? Can anybody tell me why my script is broken?
Thanks for any help!
haxcess.