Re: bash: syntax error near unexpected token

Discussion in 'Python' started by Benjamin Kaplan, Aug 17, 2010.

  1. On Mon, Aug 16, 2010 at 11:33 PM, kreglet <> wrote:
    > Hello,
    >
    > I started learning python last year. All of this time i have used the
    > terminal and gedit to create, modify, and test my applications and modules.
    > For some reason I can not do this any more.
    > I'll try to do my best to explain whats happening.
    >
    > I have a script modtest.py which has a function to test if an int is even or
    > odd.
    >
    > Before:
    > desktop:~/bin$ modtest.py
    > desktop:~/bin$ evenodd(45)
    > 45 is odd
    >


    That never worked. You can execute Python scripts from the shell, but
    not arbitrary Python functions. Perhaps you were calling Python using
    the -i flag, which runs the interactive interpreter after the script
    is finished executing.

    $ python -i modtest.py
    >>> evenodd(45)

    45 is odd


    > Now:
    > desktop:~/bin$ modtest.py
    > desktop:~/bin$ evenodd(45)
    > bash: syntax error near unexpected token `45'
    >


    And this is what's supposed to happen any time you try this in any
    shell. When you call evenodd, bash looks for a program or shell
    built-in called evenodd. Which doesn't exist.

    > The above is just an example. ALL of my scripts that worked before no longer
    > work in the terminal yet they work in the python interpreter.
    >
    > Does anyone know what the problem might be and how I can fix it?
    >
    >
    Benjamin Kaplan, Aug 17, 2010
    #1
    1. Advertising

  2. Benjamin Kaplan

    Hans Mulder Guest

    Benjamin Kaplan wrote:
    > On Mon, Aug 16, 2010 at 11:33 PM, kreglet <> wrote:


    >> desktop:~/bin$ modtest.py
    >> desktop:~/bin$ evenodd(45)
    >> bash: syntax error near unexpected token `45'
    >>

    >
    > And this is what's supposed to happen any time you try this in any
    > shell. When you call evenodd, bash looks for a program or shell
    > built-in called evenodd. Which doesn't exist.


    That's not what "syntax error means". If the problem were that
    evenodd does not exist as a shell function, the error would have
    been "bash: evenodd: command not found".

    I do not understand why bash says the token `45' is unexpected.
    The '(' token is the problem. It's a syntax error because in
    bash syntax function arguments are not enclosed in parentheses:

    $ function evenodd {
    > if (( $1 % 2 ))
    > then echo $1 is odd
    > else echo $1 is even
    > fi
    > }

    $ evenodd 45
    45 is odd
    $ evenodd(45)
    bash: syntax error near unexpected token `45'
    $

    Of course, this is completely off-topic.

    -- HansM
    Hans Mulder, Aug 17, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aemi
    Replies:
    2
    Views:
    868
    Daniel Pitts
    Mar 2, 2007
  2. Replies:
    5
    Views:
    696
  3. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    This is an unexpected token. The expected token is 'NAME'

    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Jul 13, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    771
    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=
    Jul 13, 2007
  4. ducatired
    Replies:
    2
    Views:
    3,366
    ducatired
    Jan 17, 2008
  5. Replies:
    11
    Views:
    677
    Eric Bohlman
    Jan 19, 2006
Loading...

Share This Page