using jshell in executable UNIX scripts
Som Snytt
som.snytt at gmail.com
Wed Oct 12 05:12:41 UTC 2016
The -save option does that trick. I think it's an old one.
apm at mara:~/tmp$ cat mytool.scala
#!/bin/sh
exec /home/apm/scala-2.12.0-RC1/bin/scala -nc -save "$0" "$@"
!#
Console println s"hello, ${util.Properties.userName}"
apm at mara:~/tmp$ sh -x mytool.scala
+ exec /home/apm/scala-2.12.0-RC1/bin/scala -nc -save mytool.scala
hello, apm
apm at mara:~/tmp$ ls mytool.*
mytool.jar mytool.scala
apm at mara:~/tmp$ sh -x mytool.scala
+ exec /home/apm/scala-2.12.0-RC1/bin/scala -nc -save mytool.scala
hello, apm
The subsequent run uses the jar, unless the source is touched.
There's not a great difference between embedding a snippet for scripting
and for REPL.
Aside from actual tooling, sometimes it's easier to just edit a file than
fiddle in the REPL, especially when more than a few expressions are
involved.
On Tue, Oct 11, 2016 at 1:11 PM, Cay Horstmann <cay at horstmann.com> wrote:
> You may know that the Scala REPL and Nashorn let you use a shebang in the
> first line. (Nashorn doesn't do it very well: command line arguments must
> be preceded by -- because jjs can take multiple scripts.)
>
> I have used the feature for Scala quite a few times. But it's a very minor
> convenience. Everytime the script turned into anything of any value, I
> wanted it compiled, and then I had to add the Scala equivalent of "public
> static void main" anyway.
>
> If I want to do the same in Java, the // trick is good enough.
>
> Cheers,
>
> Cay
>
>
> Le 10/11/2016 à 12:52 PM, Brian Goetz a écrit :
>
>> I understand why you'd like to coopt jshell into this -- after all, it
>> is convenient and it seems "so close" -- but this is trying to turn
>> jshell into something it wasn't designed for. But this wasn't an
>> oversight; we deliberately chose to leave this *outside* of the jshell
>> requirements, because it feels like an entirely different feature.
>>
>> More generally, "hacks" like this always come back to bite you. What you
>> want is a "no main, no compile java runner". Jshell looks like that,
>> but it isn't. When you have nothing, a little of something seems like a
>> really good idea; but the warts will become apparent almost
>> immediately. I'd much rather do nothing for this now, and consider
>> doing something better in the future, than doing something bad now --
>> which is what this would be.
>>
>>
>> On 10/10/2016 9:55 AM, Peter Levart wrote:
>>
>>> Hi,
>>>
>>> "jshell" command is a very nice interactive Java shell, but it could
>>> also be used for scripting. An executable script in any major UNIX OS
>>> is a textual file with executable permissions that starts with the
>>> following two characters: #!
>>> The rest of the 1st line is the path to the interpreter executable and
>>> any arguments passed to it. The last argument passed to the
>>> interpreter is the path to the executable script. In case of jshell,
>>> one would want such script to be written like:
>>>
>>> #!/home/peter/Apps64/jdk9/bin/jshell
>>>
>>> System.out.println("Hello World!");
>>>
>>> /exit
>>>
>>>
>>> The problem is that jshell tries to parse the 1st line using jshell
>>> syntax and the result of running above executable script is:
>>>
>>> | Error:
>>> | illegal character: '#'
>>> | #!/home/peter/Apps64/jdk9/bin/jshell
>>> | ^
>>> | Error:
>>> | illegal start of expression
>>> | #!/home/peter/Apps64/jdk9/bin/jshell
>>> | ^
>>> Hello World!
>>>
>>>
>>> The script is actually executed, but the syntax error encountered in
>>> the 1st line is printed too.
>>>
>>> Would it be possible for jshell to skip 1st line if it starts with
>>> characters #! like other shells do?
>>>
>>>
>>> Regards, Peter
>>>
>>>
>>
>
> --
>
> Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com
>
More information about the kulla-dev
mailing list