I see a lot of places in my office where ant tasks are used to move files from one place to another and also do some tasks on these files.However all this can be done with shell scripts.
My question is ,
In what cases is ant preferred over shell scripts ?
What are the benefits of using ant over a shell scripts for doing same set of tasks.
One advantage ant has is that it works on all platforms,other than that are there any performance related advantages ?
Your question as to why ANT should be preferred to shell scripts is two-fold:
ANT is a tool that has achieved widespread adoption and is likely to
be already installed on a developer's workstation. Compared to one
of it's main predecessors, make, ANT is a lot more standardized and
cross-platform.
ANT is a tool familiar to developers. Used to build their code so they will often extend the ANT script to deploy their application as
well. Indeed many vendors offer ANT tasks for this purpose.
There are no performance benefits, really. Java is slow for command-line usage.
But..... I would advise against playing the "performance" card. Let's pretend that your application does not support windows (Which is odd considering a Java application should support all plaforms...): I have seen shell script driven deployments decend into chaos attempting to reconcile the various ways different unix operating system commands work. Commands like "tar", "awk", etc can be subtly different which leads to additional platform support logic in your script.
In conclusion I would use neither. I choose a hybrid approach of using groovy for general scripting. It is a java based scripting language and embeds the full power of ANT. Being a java based scripting language means it will work on all platforms. In the interest of fairness it should also be noted that there are other language options. Ruby is certainly worthy of mention since it has spawned a set of configuration management technologies that are well worth evaluation. (See Chef and Puppet)
In practice, it boils down to Windows support. If you're in a Unix shop and don't want to introduce new stuff to devs, there are two alternatives i've used with success:
Plain old shell script with Git Bash. Git Bash comes with the Git windows distribution (http://git-scm.com/). If you're doing automation, you can launch shell scripts like so: "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -- ./BUILD
Node script. Again it's one easy installer, it's javascript, and you can use something like ShellJS (https://github.com/arturadib/shelljs) to get very close to Unix shell script / Makefiles.
Related
I wrote a shell script that uses a few BASH specific commands, such as [[ ... ]]. I know that some BASH features are only available in BASH 4 or newer. How can I find out what version of BASH I need for this script without trying numerous different versions?
Is there some kind of tool that can check the needed minimum version?
The source code is available on GitHub at https://github.com/JEFF-Media-GbR/RandomRound/blob/master/randomround
There are a bunch of resources to check for a) introduction of a specific Bash feature and b) Bashisms in general:
BashFAQ/061 ("Is there a list of which features were added to specific releases (versions) of Bash?")
Bash changes on Bash Hackers
Bash release notes
These can all be used to figure out when a feature was introduced.
For a list of Bashisms, the Bash manual describes differences to the Bourne Shell in Appendix B ("Major differences from the Bourne Shell"), and there is the article How to make bash scripts work in dash on the Wooledge wiki.
I don't know what the distribution of versions "in the wild" is, but if your user base includes macOS users, be aware that they use Bash 3.2 unless they have upgraded using Homebrew or similar.
As for programmatically determining what version of Bash is required to run a specific script, I'm not aware of any tool that does that. Shellcheck comes somewhat close in that it can warn about Bashisms when your script starts with #!/bin/sh.
What you are asking can not be done. This is one of the programming problems which are principally impossible to solve. One (but not the only) reason is that a script may at run time construct a string and evaluate it, and there is no way that you can by automatic means analyze for an arbitrary program, what data it will produce.
TL;DR: Whats the most optimal way to write portable general-purpose automation scripts for Windows, Mac and Linux?
Longer version:
I work with different platforms and often write shell scripts to automate things (run programs and other scripts, manipulate files and directories, etc).
The problem is that sh/bash substitutes on Windows are tricky, complex, often incompatible or lack some native unix tools. And cygwin scares a regular user, in case when I share some of my scripts with the others.
I find that .bat is very limited and ugly. And I didn't use Powershell a lot, but it looks a bit overcomplicated to me (or should I just give it another try?).
What would you recommend to do in such case? Have you had similar challenges, how did you solve them?
I would advice you use some configuration manager as Ansible, Puppet or Chef. Since their sole purpose is to automate things, and some of them are cross platform. Google each one I mentioned, scripts are generally easy to write in them and they will work on all the platforms, but you will need to install the manager itself on each platform, which can be achieved with init.sh or with a simple powershell script.
Facing the programming for the wsadmin scripting client I see that you can use both jacl and jython.
I've the same level of confidence with both two languages.
I'm wondering if, in the specific of wsadmin programming , there are advantages of using one language over the other; such as robustness, availability of examples or libraries for websphere administration, or something like that arisen from your experience in the use and building of this kind of scripts.
Jython is the language to prefer:
Rational Application Developer has tooling support for Jyton (Jython editor, debugger, command completition, and ability to test your scripts against your test server inside IDE).
WebSphere administrative console provides console command assistance; it gives you scripting equivalents of the operations you perform using administrative console in case you want to automate them. This assistance uses Jython as language.
Even if Jacl had been the default language for wsadmin, IBM promotes Jython for future, and even provided a tool to convert Jacl scripts to Jython. Quoting from description of this tool at given link;
When selecting a scripting language, Jython is the strategic direction
as the administration scripting language for WebSphere Application
Server because future enhancements of the scripting language is
focused on the use of Jython.
I'm not trying to question any of previous responses, just add some facts.
Although Jython is the "strategic" directions, Jacl has been there since WAS v4. In WAS v8.5 it is still the default (and deprecated!).
The administrative API was written with Jacl in mind. With Jython one you have to do tricks like this one to get server list as list:
for srv in AdminConfig.list('Server').splitlines():
print srv
whereas in Jacl one can simply do this:
foreach srv [$AdminConfig list Server] {
puts $srv
}
Obviously, many AdminConfig and AdminControl methods return lists as newline-separated string.
IBM has really cornered itself with Jython and Jacl. They're still using Jython 2.1 (released in 2002, even in the latest and greatest WAS v8.5). Jacl isn't actively supported by the community. The API is Jacl-friendly, wasn't rewritten for Jython. Lots of client solutions are based on the tricks you have to do in Jython, there's plenty of Jacl-based solutions. Even IBM internally has a plenty of dependencies on that legacy. This might be the reason why Jacl is deprecated since WAS v5.1 and still default.
In short:
If you're going to write a small script for a specific task, Jacl may be more convenient for you (since you've mentioned that you're comfortable with both Python and Tcl)
Should you however be interested in developing larger framework for managing your WAS infrastructure, then Jython with its object-orientation may be a better option. But don't expect too much from that version of Jython - it's quite buggy and you won't be able to use too many Python libraries simply because they dropped Python2.1 support loooong time ago.
The conclusion is: it's not that simple answer (which means that you've asked a good question).
Whenever I write shell scripts (mostly software development utilities or build tools) I've generally tried to avoid using bash in favor of using plain old sh for portability. However lately I've been running into more and more issues where useful features are not available, or behavior is actually less consistent across systems using sh then it is using bash, since sh is aliased to different shells...
As I understand it, sh is the oldest Unix shell and carefully written sh scripts should in theory run on pretty much any system out there... but it also seems there are about 9000 different variants of every major shell, too. Doesn't using bash as your script interpreter effectively limit your script's portability? Sure, no problems on OS X or pretty much any Linux out there, but what about the BSDs? Solaris, AIX, HP-UX? What do you do if you really want to run on everything?
I know bash can be installed on virtually any OS but it is really a first class citizen on all relevant modern systems? Does it come pre-installed? I'm just not really sure whether it's best to avoid or embrace bash with the intent of having the most consistent and portable overall experience.
What do you do if you really want to run on everything?
You follow the POSIX standard for sh (and the tools you're calling) and hope that the target OS does so too. Any modern product called "UNIX" must follow this standard, and customarily (though not universally), the standard shell will be called /bin/sh. The BSDs and Linux distros tend to aim at POSIX compatibility as well.
Doesn't using bash as your script interpreter effectively limit your script's portability?
Yes, but it depends on your target audience as you noted. If it's a short script, it's worth testing under dash (Ubuntu and Debian's default shell) for POSIX compatibility.
Whenever I start thinking about portability issues in my shell script, I switch to another language. Perl is widely available and generally a good choice for scripts, but if your tools are to be consumed by Python, Ruby, $lang developers, use $lang to its full potential.
bash itself is just a plain C program, does not need special authority to run, can be put in any location. You can easily build it from source. Basically, you can run bash if you need to and doesn't need the administrator of the system to install it.
As long as it is in your path, you can always code your script with the line.
#!/usr/bin/env bash
I'm wondering if there exists a bash shell script interpreter that runs solely in the JVM (i.e., does not rely on "shelling out" to a system bash.) A "Jbash" if you will.
Such interpreters exist for many other languages (JRuby, Jython, etc.) I'd have thought there'd be one for bash scripts as well but I haven't found one yet.
Any recommendations?
There is one in google code http://code.google.com/p/jbash/
There is a new project : https://github.com/crashub/bash but too young at the moment:
Very early prototype built on top of the libbash ANTLR grammar, aims
to implement basic stuff. The grammar provides an AST.