Problem with bash script - Unix

This is a discussion on Problem with bash script - Unix ; 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: ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: Problem with bash script

  1. Problem with bash script

    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?

  2. Re: Problem with bash script

    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?


  3. Re: Problem with bash script

    2008-03-12, 10:30(+00), Robert Harris:
    > 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 ]

    [...]

    and quote variables:

    if [ -e "$i" ]

    --
    Stéphane

  4. Re: Problem with bash script

    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

  5. Re: Problem with bash script

    Stephane CHAZELAS wrote:
    > 2008-03-12, 10:30(+00), Robert Harris:
    >> 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 ]

    > [...]
    >
    > and quote variables:
    >
    > if [ -e "$i" ]
    >


    Why the quotes? It seems to work fine without.

  6. Re: Problem with bash script

    saneman writes:
    > Stephane CHAZELAS wrote:
    >> 2008-03-12, 10:30(+00), Robert Harris:
    >>> 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 ]

    >> [...]
    >> and quote variables:
    >> if [ -e "$i" ]
    >>

    >
    > 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.

  7. Re: Problem with bash script

    saneman 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

  8. Re: Problem with bash script

    Måns Rullgård wrote:
    > saneman 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.
    >


    .... unless there aren't any filenames matching that pattern!

    Robert

  9. Re: Problem with bash script

    2008-03-12, 16:10(+01), Rainer Weikusat:
    [...]
    >>> and quote variables:
    >>> if [ -e "$i" ]
    >>>

    >>
    >> 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.

    [...]

    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

  10. Re: Problem with bash script

    2008-03-12, 19:26(+00), Måns Rullgård:
    > saneman 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.


    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

+ Reply to Thread