The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Shebang lines in Scala scripts

4 replies on 1 page. Most recent reply: Dec 20, 2007 8:12 PM by Justin Forder

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic    
Flat View: This topic has 4 replies on 1 page
Justin Forder

Posts: 9
Nickname: jufo
Registered: May, 2007

Shebang lines in Scala scripts Posted: Dec 15, 2007 1:46 AM
Reply to this message Reply
Advertisement
I was delighted to find Programming in Scala (via Ola Bini's blog post about the Scala session at JavaPolis), and I'm enjoying it.

On page 44, there's advice about using:

#!/bin/sh
exec scala "$0" "$@"
!#

to allow execution of a script as, e.g., ./hello Fred

I haven't seen this kind of multiline shebang construct before, and didn't find anything about it at http://en.wikipedia.org/wiki/Shebang_%28Unix%29

The simpler form

#!/usr/bin/env scala
!#

works for me - it would be nice to get rid of the !# line, but if I omit it I get an exception from scala.tools.nsc.ScriptRunner$.headerLength(ScriptRunner.scala:136)
complaining that "script file does not close its header with !# or ::!#"

Other JVM-based languages (JRuby, Groovy) work with normal shebang lines. Does Scala need to be different?

-- Justin Forder


Rob Lally

Posts: 7
Nickname: roblally
Registered: Dec, 2007

Re: Shebang lines in Scala scripts Posted: Dec 17, 2007 12:42 PM
Reply to this message Reply
I can't shed any light on your problem, I am however having a problem of my own. The book suggests that adding

::\#!
@echo off
call scala \%0 \%*
goto :eof
::!\#

at the start of a file will make it executable on Windows. This doesn't seem to work, Windows pops up a dialog asking me which program to run the file with - the same behaviour as not entering the magic incantation above.

Any suggestions what I'm doing wrong?

Justin Forder

Posts: 9
Nickname: jufo
Registered: May, 2007

Re: Shebang lines in Scala scripts Posted: Dec 19, 2007 1:41 AM
Reply to this message Reply
Rob, it sounds as if you are expecting this to work when double-clicking the file, but these instructions relate to running the Scala program as a command, from the command line.

Having said that, the instructions for Windows are not working for me. It seems that the backslashes have got in by accident. Removing them all solves the problem (also, the use of "call" seems superfluous):

::#!
@echo off
scala %0 %*
goto :eof
::!#

println("Hello, " + args(0) + "!")

This leaves the issue of how to name the file and how to invoke it on the command line. WIndows treats certain suffixes (e.g. .bat, .cmd, .exe) as meaning that a file is executable - and you don't have to give the suffix on the command line. But if you don't give the suffix in this case, Scala won't find the script file. So the above works if I have it in hello_args.bat, and invoke it as:

hello_args.bat fred

but it won't work if I invoke it as

hello_args fred

(Note that I should be able to use hello.scala if I add .scala to my PATHEXT environment variable).

Rob Lally

Posts: 7
Nickname: roblally
Registered: Dec, 2007

Re: Shebang lines in Scala scripts Posted: Dec 19, 2007 4:46 AM
Reply to this message Reply
Thanks for the response Justin.

I was trying to run it from the command line, but in a file named blah.scala rather than blah.bat; which provokes Windows into helpfully popping up a dialog window, even though you are working from from a command prompt.

Changing the file extension and removing the errant slashes solved the problem and got the script running.

That said, this isn't really solving the same problem as making a script executable on a *nix world any more. What we really want here is to make *.scala file executable and the easiest way to do that is to open Windows Explorer -> Tools -> Folder Options -> File Types and then associate files with the extension .scala with the scala.bat file.

After doing that then .scala files are runnable from both the command line and windows.

Justin Forder

Posts: 9
Nickname: jufo
Registered: May, 2007

Re: Shebang lines in Scala scripts Posted: Dec 20, 2007 8:12 PM
Reply to this message Reply
Rob, that's great advice. A little further work is needed (i) to allow command-line parameters to be passed to the Scala script, and (ii) to avoid having to include the .scala suffix in the command.

i) In the Folder Options, File Types tab, with .SCALA selected, hit the Avanced button and in the resulting Edit File Type dialog edit the command for the 'open' action and add %* to it (no quotes)
I removed other unnecesary quotes - I don't have any spaces in my path - and my command for the 'open' action is now
C:\scala-2.6.1-final\bin\scala.bat %1 %*


ii) add ;.SCALA to your PATHEXT environment variable value.

Now if you have a file args.scala containing the script

args.foreach(println)

you should be able to type

args 1 2 3 4 5

on the command line and see

1
2
3
4
5

as output.

Note that I don't know why the %* works - the documentation that I've been able to find says it refers to all the arguments, but this example suggests that if it comes after %1 it refers to the remaining arguments. I initially used a command line containing "%1" "%2" "%3" etc, which works but can't handle more than 8 arguments (the script file name is in position 1). I took a look at what %* would give, expecting to need to write a different scala.bat file to handle it, but then found it Just Worked.

I tried to use my SCALA_HOME environment variable in the 'open' action definition (to make it easier to move between Scala versions), but, for reasons I don't understand, got an "Access is denied" message. I also tried just using scala in the command (scala %1 %*), since scala.bat is on my path, but this gave me "The specified program could not be found" on trying to save that action definition.

Flat View: This topic has 4 replies on 1 page
Topic: Chap2 page 49 speling Previous Topic    

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use