| Author |
Problem with bash script
|
|
| saneman 2008-03-12, 8:12 am |
| In the below loop I check if pdf files exist in the current dir:
for i in *pdf;do
if[-e $i];then
echo pdf exists!
fi
done;
But I get the error:
../test: line 31: syntax error near unexpected token `then'
../test: line 31: ` if[-e $i];then'
What am I doing wrong?
| |
| Robert Harris 2008-03-12, 8:12 am |
| saneman wrote:
> In the below loop I check if pdf files exist in the current dir:
>
> for i in *pdf;do
>
> if[-e $i];then
You need to separate your tokens with spaces, viz:
if [ -e $i ]
Robert
> echo pdf exists!
>
> fi
>
> done;
>
> But I get the error:
>
> ./test: line 31: syntax error near unexpected token `then'
> ./test: line 31: ` if[-e $i];then'
>
>
> What am I doing wrong?
| |
| Stephane CHAZELAS 2008-03-12, 8:12 am |
| 2008-03-12, 10:30(+00), Robert Harris:
> saneman wrote:
>
> You need to separate your tokens with spaces, viz:
>
> if [ -e $i ]
[...]
and quote variables:
if [ -e "$i" ]
--
Stéphane
| |
| Bit Twister 2008-03-12, 7:31 pm |
| On Wed, 12 Mar 2008 11:25:32 +0100, saneman wrote:
> But I get the error:
>
> ./test: line 31: syntax error near unexpected token `then'
> ./test: line 31: ` if[-e $i];then'
>
>
> What am I doing wrong?
You might want to spend some time reading
http://tldp.org/LDP/abs/html/index.html
| |
| saneman 2008-03-12, 7:31 pm |
| Stephane CHAZELAS wrote:
> 2008-03-12, 10:30(+00), Robert Harris:
> [...]
>
> and quote variables:
>
> if [ -e "$i" ]
>
Why the quotes? It seems to work fine without.
| |
| Rainer Weikusat 2008-03-12, 7:31 pm |
| saneman <asdfsdf@asd.com> writes:
> Stephane CHAZELAS wrote:
>
> Why the quotes? It seems to work fine without.
It will work fine without as long as $i doesn't expand to something
wich the shell considers to be a token separator.
An example:
# touch "a b"
# i="a b"
# test -e $i && printf 5
test: b: unknown operand
# test -e "$i" && printf 5
5#
It is, of course, up to you to determine if you care and/or if this
can actually happen.
| |
| Måns Rullgård 2008-03-12, 7:31 pm |
| saneman <asdfsdf@asd.com> writes:
> In the below loop I check if pdf files exist in the current dir:
>
> for i in *pdf;do
>
> if[-e $i];then
> echo pdf exists!
>
> fi
>
> done;
This seems like a pointless thing to do. Since *pdf expands to all
filenames in the current dir matching that pattern, of course the -e
test will succeed. Others have already pointed out the syntax errors.
--
Måns Rullgård
mans@mansr.com
| |
| Robert Harris 2008-03-12, 7:31 pm |
| Måns Rullgård wrote:
> saneman <asdfsdf@asd.com> writes:
>
>
> This seems like a pointless thing to do. Since *pdf expands to all
> filenames in the current dir matching that pattern, of course the -e
> test will succeed. Others have already pointed out the syntax errors.
>
.... unless there aren't any filenames matching that pattern!
Robert
| |
| Stephane CHAZELAS 2008-03-13, 8:16 am |
| 2008-03-12, 16:10(+01), Rainer Weikusat:
[...]
>
> It will work fine without as long as $i doesn't expand to something
> wich the shell considers to be a token separator.
[...]
Not only that. Most people ignore that quotes in shells have a
completely different meaning as in most other languages.
In shells, quotes are used to prevent special behaviors of the
shell, not to identify string constants. In shells, you're more
likely to put quotes around string variables than string
constants.
Word splitting above is one behavior that the double quotes
prevent. Filename generation is another one. Removal of empty
arguments yet another (though that one could be considered part
of word splitting).
So:
i=
i='*'
i='a?'
IFS=a
i=abc
would be problems.
set -f
disables filename generation
IFS=
disables word splitting.
But once you've done that, you'll still have problems with empty
strings, so you should always quote variables unless you know
what you're doing and why you're doing it.
--
Stéphane
| |
| Stephane CHAZELAS 2008-03-13, 8:16 am |
| 2008-03-12, 19:26(+00), Måns Rullgård:
> saneman <asdfsdf@asd.com> writes:
>
>
> This seems like a pointless thing to do. Since *pdf expands to all
> filenames in the current dir matching that pattern, of course the -e
> test will succeed. Others have already pointed out the syntax errors.
Beside the fact that in most shells *pdf expands to "*pdf" if
there's no matching file.
[ -e "$i" ]
does a stat(2) underneath. So it tests wether the stat(2) system
call succeeds. While *pdf uses getdents(2) underneath.
stat(2) may fail if you don't have the search permission on the
directory (in this case, it would have had to lose it in the
meantime, as *pdf needs an open(".") which means a search in the
current directory), if the file is a symlink to a file that
doesn't exist or isn't accessible to you, or if it has been
removed since the shell expanded *pdf.
--
Stéphane
|
|
|
|