Some unrecognized syntax error

test.rb:1: syntax error, unexpected kDEF, expecting $end
usagesagechecker.rb’

I don’t understand what does this error really means.
Some said its all about whitespaces.

Problem happens when I launch test.rb script: ruby test.rb

root@localhost# dos2unix test.rb
dos2unix: converting file test.rb to UNIX format …
root@localhost# ruby -c test.rb
test.rb:1: syntax error, unexpected kDEF, expecting $end
usagesagechecker.rb’
^

I don’t understand what’s wrong? Where is my fault?

On Wed, Mar 2, 2011 at 3:33 PM, Ruby F.
[email protected] wrote:

http://www.ruby-forum.com/attachment/5988/checker.rb
15:38:10 Temp$ cat test.rb
usage15:38:17 Temp$ od -tx1c test.rb
0000000 72 65 71 75 69 72 65 20 27 63 68 65 63 6b 65 72
r e q u i r e ’ c h e c k e r
0000020 2e 72 62 27 0d 0d 64 65 66 20 75 73 61 67 65 0d
. r b ’ \r \r d e f u s a g e \r
0000040 65 6e 64 0d 0d 75 73 61 67 65
e n d \r \r u s a g e
0000052
15:38:29 Temp$ dos2unix test.rb
test.rb: done.
15:38:51 Temp$ ruby19 -c test.rb
Syntax OK
15:38:54 Temp$

robert

Ah, seems to be understood, I need mac2unix :slight_smile:

On Wed, Mar 2, 2011 at 3:50 PM, Ruby F.
[email protected] wrote:

root@localhost# dos2unix test.rb
dos2unix: converting file test.rb to UNIX format …
root@localhost# ruby -c test.rb
test.rb:1: syntax error, unexpected kDEF, expecting $end
usagesagechecker.rb’
^

I don’t understand what’s wrong? Where is my fault?

What does od tell you now?

robert

problem solved, thank you, robert!

Mmm, on 1.8.7 your code works with no problem. Have you tried ruby -c
checker.rb?

On 03/02/2011 06:34 PM, Eric C. wrote:

I’ve noticed recently that I can run Ruby files with DOS or Unix line
endings with no problem on either platform, but as soon as I put a
shebang line in the file, and it has DOS line endings, I can’t run it
directly from the command line on OS X (e.g. ./test.rb). I have to
run it specifically with ‘ruby’ instead (e.g. ruby ./test.rb). Is
that just a built-in problem with OS X or bash’s reading of shebang
lines?

You didn’t mention exactly what error you received when trying to run
the file with DOS line endings but it was probably something like this:

bash: ./test.rb: /bin/ruby^M: bad interpreter: No such file or directory

The problem is actually within the lower levels of the operating system,
below bash and ruby. When a non-binary file is executed, the part of
the kernel that handles running programs looks to see if there is a
shebang line. If there is, it reads until it encounters a LF (newline)
character. Everything between the shebang and the LF is the command
line to actually run, to which the path to the file itself will be
appended.

In the case above, the CR (carriage return) part of the DOS line ending
is being consumed as part of the command line. Obviously, there is no
program named /bin/ruby^M. Note that the ^M is actually bash being
helpful by converting the CR character into something readable. When
that isn’t done, you’ll likely see the following on your terminal:

: No such file or directory

If you redirect stderr to a file or pipe it to less, you’ll actually see
something like this:

/usr/bin/env: ruby^M: No such file or directory

Which is pretty much the same problem as the first example, just harder
to diagnose. The moral of the story is to avoid DOS line endings in
your scripts if you expect to run them directly. There are probably
many other ways to hack around the issue, but simply using Unix line
endings is going to be among the easiest.

You can read up more about shebang on Wikipedia. Pay particular
attention to the portability section:

-Jeremy

I’ve noticed recently that I can run Ruby files with DOS or Unix line
endings with no problem on either platform, but as soon as I put a
shebang line in the file, and it has DOS line endings, I can’t run it
directly from the command line on OS X (e.g. ./test.rb). I have to
run it specifically with ‘ruby’ instead (e.g. ruby ./test.rb). Is
that just a built-in problem with OS X or bash’s reading of shebang
lines?