LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-26-2006, 09:28 AM   #1
haxcess
LQ Newbie
 
Registered: Apr 2004
Location: Peterborough, Ontario, Canada
Distribution: Slackware 9 & 10
Posts: 13

Rep: Reputation: 0
bash file test - newline is a directory?


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.
 
Old 03-26-2006, 11:09 AM   #2
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
you should write
if [ -e "$foundfile" ]
and
if [ -d "$foundfile" ]
 
Old 03-26-2006, 11:34 AM   #3
haxcess
LQ Newbie
 
Registered: Apr 2004
Location: Peterborough, Ontario, Canada
Distribution: Slackware 9 & 10
Posts: 13

Original Poster
Rep: Reputation: 0
of course

It's the little nuances that always get you in trouble..
Thank you so much!

haxcess.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mkdir: cannot create directory `test': Read-only file system punt Linux - General 2 04-16-2005 09:58 PM
Bash Script for a newline olefemmy Programming 1 11-11-2004 11:16 PM
Bash, how do I test for *.txt files in a directory with IF? severian23 Programming 7 11-04-2004 09:47 AM
test.c:1:19: Stdio.h: No such file or directory :( wogga Linux - Software 1 06-11-2004 02:57 PM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration