| Author |
howto write a script to echo files with spaces in the name
|
|
| Danish 2006-11-28, 7:29 am |
| 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
| |
|
| 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
| |
| mmoreno80@gmail.com 2006-11-28, 1: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" ] ... "?
| |
| mmoreno80@gmail.com 2006-11-28, 1: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" ] ... "?
| |
|
| 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
| |
| Danish 2006-11-28, 1: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
| |
|
| 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
| |
| Danish 2006-11-28, 1: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
| |
| Janis Papanagnou 2006-11-28, 1: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
>
| |
| Danish 2006-11-29, 7:28 am |
|
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
| |
| mmoreno80@gmail.com 2006-11-29, 1: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
Try with `if [ -d "$file" ] ...'?
|
|
|
|