howto write a script to echo files with spaces in the name
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > howto write a script to echo files with spaces in the name




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    howto write a script to echo files with spaces in the name  
Danish


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 12:29 PM

Hi,
Being a beginner..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






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
Janis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 12:29 PM

Danish wrote:
>
> Hi,
> Being a beginner..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 ]

Quote the variable...

if [ -d "$file" ]

>         then
>                 echo "$file is Directory"
>         else
>                 echo "$file is a file"
>         fi
> done

Another option is to use -p option for ls and a case statement

ls -1p | while read file
do
case "$file" in
*/) echo "$file is Directory";;
*)  echo "$file is a file";;
esac
done


Janis

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






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
mmoreno80@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:17 PM


Danish wrote:
> Hi,
> Being a beginner..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

I if you try with 'if [ -d "$file" ] ... "?






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
mmoreno80@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:17 PM


Danish wrote:
> Hi,
> Being a beginner..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

And if you try with 'if [ -d "$file" ] ... "?






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
Janis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:17 PM

Danish wrote:
> Hi,
> Being a beginner..[...]
>
> ls -1 | while read file

BTW, you don't need the -1 option with 'ls' if you use it as above.

The command 'ls' will test whether it's stdout is connected with a
terminal; if so, it will display data multi-column wise. The -1 is
to let 'ls' display all data in a single column on a terminal.

Connected to a pipe no display adjustments are made, you'll always
get the files one by one.

Janis






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
Danish


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:17 PM


Janis wrote:
> Danish wrote: 
>
> BTW, you don't need the -1 option with 'ls' if you use it as above.
>
> The command 'ls' will test whether it's stdout is connected with a
> terminal; if so, it will display data multi-column wise. The -1 is
> to let 'ls' display all data in a single column on a terminal.
>
> Connected to a pipe no display adjustments are made, you'll always
> get the files one by one.
>
> Janis

Thanks a lot. BTW..what does -p option stand for. I read the man
pages..Didnt understand much

Thanks
Danish






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
Janis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:17 PM

Danish wrote:
> Janis wrote: 
>
> Thanks a lot. BTW..what does -p option stand for. I read the man
> pages..Didnt understand much

My man page says: "append indicator (one of /=@|) to entries"
(so the "p" in -p might come from aPpend; not sure, though).
It means, depending on the type of the file different symbols
will be appended (I think '/' for directories, '@' for soft
links, '|' for named pipes, but don't remember what '=' was).

Janis

>
> Thanks
> Danish






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
Danish


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:17 PM


Janis wrote:[vbcol=seagreen]
> Danish wrote: 
>
> My man page says: "append indicator (one of /=@|) to entries"
> (so the "p" in -p might come from aPpend; not sure, though).
> It means, depending on the type of the file different symbols
> will be appended (I think '/' for directories, '@' for soft
> links, '|' for named pipes, but don't remember what '=' was).
>
> Janis
> 

Thanks a lot. Hope to catch u later

Thanks
Danish






[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-28-06 06:17 PM

Danish wrote:
> Janis wrote: 
>
> Thanks a lot. Hope to catch u later

By "catch you later" you mean my explanation was not clear? Sorry.

An example... Perform the following steps, create a directory...

mkdir ~/tmp
cd ~/tmp
ls

Create an ordinary file, a directory, and a symbolic link...

touch abc
mkdir def
ln -s abc link_to_abc

file *

ls
ls -p

Hope that clarifies.

Its purpose is to easily distinguish some common filetypes without
issuing a display-space consuming ls -l or an extra file command.

Janis


>
> Thanks
> Danish
>





[ Post a follow-up to this message ]



    Re: howto write a script to echo files with spaces in the name  
Danish


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-29-06 12:28 PM


Janis Papanagnou wrote:[vbcol=seagreen]
> Danish wrote: 
>
> By "catch you later" you mean my explanation was not clear? Sorry.
>
> An example... Perform the following steps, create a directory...
>
> mkdir ~/tmp
> cd ~/tmp
> ls
>
> Create an ordinary file, a directory, and a symbolic link...
>
> touch abc
> mkdir def
> ln -s abc link_to_abc
>
> file *
>
> ls
> ls -p
>
> Hope that clarifies.
>
> Its purpose is to easily distinguish some common filetypes without
> issuing a display-space consuming ls -l or an extra file command.
>
> Janis
>
> 

You absolutely misunderstood me. I apologise for that. What i meant
was: i hope that I can have your advise on some other topic later in
the future..

Thanks
Danish






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:37 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register