Create Linux Bash and Windows batch scripts simultaneously with Batsh
Arrays
Batsh supports arrays, or variables that store a list of arbitrary data:
a = [1, 2, "hello"];
Batsh numbers the elements in the array consecutively, starting with 0. To access an item in the list, you need to note its index in square brackets. The following line stores 2
from array a
in variable b
:
b = a[1];
However, Batsh currently does not have any functions that can be used to supplement the array with other elements. The number of elements in an array can only be retrieved using the undocumented len
function: length = len(a);
. The benefits of arrays are therefore limited.
To Be, or Not To Be
The function exists()
, which checks whether a file exists, has proved to be much more useful than readdir()
or arrays. In the example shown in Listing 7, exists()
first tests whether the file photo1.png
is located in the /home/tim/photos/
directory.
Listing 7
Existential Question
The value returned by exists()
is then checked by the if
query: The script follows the instructions between the upper curly brackets if the file exists and the instructions in the curly brackets after else
otherwise.
If needed, you can leave out the else
. As with while
, you can put any logical expression in the parentheses of if
(e.g., if (a> b) { ... }
.
The problem in Listing 7, however, is that the path specification, /home/tim/photos
, is tailored to a Linux system, and the Batsh compiler does not translate it into the appropriate Windows counterpart, as shown in Figure 5.
![](/var/linux_magazin/storage/images/issues/2015/171/batsh/figure-5/643325-1-eng-US/Figure-5_large.png)
Batsh cannot even tell your script whether it is running in Bash or Windows, so you have no way to change this behavior. The built-in function bash()
can carry out a Bash command assigned to it:
bash("man convert");
The Batsh compiler only translates this function if you can create a Bash script. The selection, man convert
, therefore, only appears in the Bash script, not in the Windows batch file. Similarly, the function batch()
runs a batch instruction passed in. The Batsh compiler only translates this function if you compile the script for Windows.
To the Source
The Batsh website [3] is not the only place you can compile Batsh programs: The developer has released the compiler source code under an MIT license on GitHub [2].
Batsh was written entirely in the functional programming language OCaml. Therefore, to install the compiler you need a corresponding OCaml environment, which can be found in the repositories of most distributions.
The easiest way to install Batsh is through OCaml's own package manager, OPAM, which you can install on Ubuntu using the opam package:
$ sudo apt-get install opam m4
The m4
package allows you to install some of the additional libraries used by Batsh.
The following command is supposed to load Batsh as soon as these conditions are met:
$ opam install batsh
However, I was unable to resolve the Batsh dependencies automatically on the test machine with Ubuntu 14.10.
An attempt to compile Batsh manually from the source code also failed in the lab for the same reason. To install Batsh, it should be sufficient to set up OCaml using the package manager, then download the current Batsh source code package [3], unzip it, and compile and install it using make
and make install
. Additional OCaml dependencies are listed on the GitHub site [4].
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.