Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arguments to a jshell script?

Tags:

java-9

jshell

Question

I am willing to pass arguments to a jshell script. For instance, I would have liked something like this:

jshell myscript.jsh "some text"

and then to have the string "some text" available in some variable inside the script.

However, jshell only expects a list of files, therefore the answer is:

File 'some text' for 'jshell' is not found.

Is there any way to properly pass arguments to a jshell script?

Workaround so far

My only solution so far is to use an environment variable when calling the script:

ARG="some test" jshell myscript.jsh

And then I can access it in the script with:

System.getenv().get("ARG")
like image 768
Gwendal Avatar asked Oct 15 '17 14:10

Gwendal


People also ask

How do you pass arguments in a script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

How do you pass arguments to Korn shell script?

In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left.

Can shell script take parameter?

A bash shell script have parameters. These parameters start from $1 to $9. When we pass arguments into the command line interface, a positional parameter is assigned to these arguments through the shell. The first argument is assigned as $1, second argument is assigned as $2 and so on...


2 Answers

And what about option -R

> jshell -v -R-Da=b ./file.jsh

for script

{
  String value = System.getProperty("a");
  System.out.println("a="+value);
}
/exit

will give you

> jshell -v -R-Da=b ./file.jsh
a=b

Another way, would be following:

{
  class A {
    public void main(String args[])
    {
        for(String arg : args) {
          System.out.println(arg);
        }
    }
  }

  new A().main(System.getProperty("args").split(" "));
}

and execution

> jshell -R-Dargs="aaa bbb ccc" ./file_2.jsh

Update

Previous solution will fail with more complex args. E.g. 'This is my arg'.

But we can benefit from ant and it's CommandLine class

import org.apache.tools.ant.types.Commandline;
{
  class A {
    public void main(String args[])
    {
      for(String arg : args) {
        System.out.println(arg);
      }
    }
  }

  new A().main(Commandline.translateCommandline(System.getProperty("args")));
}

and then, we can call it like this:

jshell --class-path ./ant.jar -R-Dargs="aaa 'Some args with spaces' bbb ccc" ./file_2.jsh
aaa
Some args with spaces
bbb
ccc

Of course, ant.jar must be in the path that is passed via --class-path

like image 131
Oo.oO Avatar answered Sep 20 '22 15:09

Oo.oO


Oracle really screwed this up, there is no good way to do this. In addition to @mko's answer and if you use Linux(probably will work on Mac too) you can use process substitution.

jshell <(echo 'String arg="some text"') myscript.jsh

And then you can just use arg in myscript.jsh for example:

System.out.println(arg) // will print "some text"

You can simplify it with some bash function and probably write a batch file that will write to a temp file and do the same on windows.

like image 30
Oleg Avatar answered Sep 18 '22 15:09

Oleg