8

The programming language I know most about is Java. Currently I have to write quite a lot of "shell scripts" to automate my servers. Is there something like a "Java Shell" so I can write the shell scripts in Java?

5
  • 16
    If all you have is a hammer, everything looks like a nail. Perhaps it is time for you to learn some new programming languages, specially the ones better suited for your current job. – Juliano Feb 26 '11 at 15:27
  • Did you ever notice that hammers come in all shapes and sizes, so that you can choose one suitable for the job at hand? – John Gardeniers Feb 26 '11 at 20:17
  • I think the point is that, sometimes, we have to fit screws. – DutchUncle Feb 26 '11 at 23:28
  • Well, regarding the hammer: Standardization is/can be one of the key "success principles". Of course there's always something more specialized and more appropriate, but this adds a lot of overhead in most cases... My problem is, I actually do only need to prototype something. I am NO programmer I am more a "business economics" guys... So a java shell (with loose typing...) would have been great... Thanks for all your answers... – jens Mar 4 '11 at 13:28
  • 2
    There's a mighty big difference between using java as your command shell and using java as a scripting language. – tylerl Sep 8 '11 at 4:21
4

You can get groovy and use groovysh. I've done some things with that, but I find perl or bash/sh the most useful for doing admin scripts. An application language like Java adds a lot of overhead when all you need to do is execute OS commands and search text.

2
  • +1 for Groovy. It allows you to access the JVM and the full Java API, albeit with Groovy syntax. But if you're a Java developer then it's not that hard to get into. One of the big pushes for Groovy is the ability to write system independent scripts - for deployment, for testing, for anything. Best part - works on Windows, Linux and OS X without changing source. Oh, and yes you could use Cygwin on Windows to run nix based scripts but then you'd have to deal with Cygwin (not fun). – Mike Feb 28 '11 at 15:42
  • Actually it's true that it is very hard to replace bash scripting with groovysh. But its the step in the right direction and it seems there are some (although discontinued attemepts) to make it work more like a real shell (for example "grash")... In the End I will have to learn bash. But I hadnt heard of groovysh and I think its the best attempt to having a "java shell"... – jens Mar 4 '11 at 13:23
11

There is no real requirement that "shell scripts" have to be written in a shell language, you could do everything with C or even Assembler, if you like that.

In the context of shell scripts, the shell is nothing more than an interpreter for a programming language, just like Perl or PHP, but heavily optimized for the kind of work you mostly do with a shell script.

In the long term, I would suggest that you learn bash or something, which isn't really difficult because shells are very limited in their abilities so that there isn't too much to learn.

1
  • 1
    The main issue with writing administrative programs in a language other than c or a shell is making sure the runtime is available for your language. Most Linux systems come with bash, csh, perl, and sometimes python at install time, but they won't have java. – Jeff Strunk Feb 26 '11 at 20:14
3

Some of the lighter jvm languages have shells, but those are front-ends to the interpreter, not custom dialects for sysadmining. A good system shell needs to have excellent process and file descriptor support and have concise syntax and builtins, and I don't think that exists in Java.

You can mix programming with scripting with things like chef, puppet, vlad, and the like, but that means learning bash scripting first, because those languages, quite sensibly, delegate commands to the shell. Languages like Ruby, Perl and Python are less verbose and you'll see them used a lot. Java's verbosity and some of its core values (do everything in the jvm, don't leave the ide) make it very ill-suited to the task.

3

Yes, there is. I have been using BeanShell from time to time, even though it is not under active development at the moment. It is an interpreter for standard Java syntax and also comes with a graphical interpreter / environment, if required.

You could write your shell scripts as described in the BeanShell docs, but you would have to make the Beanshell jar file available to your Java installation for this (e. g. put it into the /ext/lib folder of your JRE installation), otherwise java will not find the bsh.Interpreter class.

However, I would also recommend to take a closer look at your shell scipting options (you did not mention your environment, but TiZon's recommendation with bash is cool when using Linux). Using a full-blown general-purpose programming language like Java will only get in the way when trying to be productive with shell-like tasks, e. g. moving files around, invoking commands and the likes.

2

You should probably take a look at Bash Scripting.

2

I wrote jpad.io to allow running java "shell scripts" and interactive snippets while also providing nice visualization of the results. This shows what I mean:

JPad Java REPL

To copy a file using this as a scripting language you would use: http://jpad.io/example/1I/fileio

final File file = new File("outt.csv");
Object o = new int[] {1,2,3};
JPad.writeCsv(o, file);
java.nio.file.Files.copy(file.toPath(), Paths.get("copy.csv"));

Which you can also run from the command line as:

 > jpad fileio.jpad
 > dir
 29/04/2017  13:30    <DIR>          .
 29/04/2017  13:30    <DIR>          ..
 29/04/2017  13:30                16 copy.csv
 29/04/2017  13:30               160 fileio.jpad
 29/04/2017  13:30                16 outt.csv
0

Yes, add the beanshell .jar into your Javahome/jre/lib/ext directory and then you can launch scripts like so:

java bsh.Interpreter myscript.bsh
0

PowerShell, an object oriented shell which can use C# libraries, has been opensourced and is available for Linux.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.