LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-28-2006, 07:00 AM   #1
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Rep: Reputation: 31
howto write a script to echo files with spaces in the name


Hi,
I wrote a simple script to ls files in the directory
and echo them on the screen. The condition being

1 if $x is a directory: echo $x is a directory, or echo it is a file...

I have a couple of directorys in my home folder with spaces in their
names because of which I got the following error.

Please advise on how to correct this problem..

#!/bin/sh

ls -1 | while read file
do
if [ -d $file ]
then
echo "$file is Directory"
else
echo "$file is a file"
fi
done
~

./loop.sh: line 5: [: Testing: binary operator expected
Testing Books is a file
thunderbird is Directory
....
....

Thanks
Danish
 
Old 11-28-2006, 07:18 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Try changing the test to the following (note the quotes)

if [ -d "$file" ]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I prevent echo eating spaces? essdeeay Linux - General 3 11-01-2005 03:11 PM
how to write a script to move files from one directory to another myeire Linux - Newbie 7 02-25-2005 10:01 AM
How can I write a shell script to transfer files and keep permissions? LancerNZ Linux - Networking 1 02-24-2005 03:49 AM
How can I write a shell script to transfer files and keep permissions? LancerNZ Linux - Wireless Networking 0 02-24-2005 12:19 AM
mp3 files - replacing blank spaces with _ script? mymojo Linux - Newbie 4 12-08-2003 03:33 AM


All times are GMT -5. The time now is 02:44 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration