Create Linux Bash and Windows batch scripts simultaneously with Batsh
Pitfalls
Unlike other programming languages, Batsh does not type-check, allowing you to assign strings to variables meant to hold integers, for example:
dn = filename("hmpf", 9876);
Listing 2 would then output the file name imagehmpf9876
, which is relatively harmless in this case. However, incorrect parameters or parameters with typos can sometimes have drastic effects in more complex functions.
If you define a new variable in a function, it is only visible in that function. Consequently, you cannot access the variable b
in Listing 3 outside function goodday
. If you try anyway, Batsh thinks you are referring to a new variable b
. New variables are empty by default, so println(b)
would just output a blank line.
Listing 3
Variable Scope
The greeting()
function in Listing 4 tries to change variable a
inside a function. If you run the finished script, you see the output Hello. If you try to change a
, as the greeting()
function does, Batsh creates a second variable a
that is only visible within the function. To prevent this, you first need to make the variable inside the function global
. Listing 5, correspondingly changed, then outputs the value World.
Listing 4
Local Variable
Listing 5
Global Variable
All-Around
Consider the situation in which you need to run an image editing program 10 times to convert 10 TIFF images into the JPEG format. The Batsh while
loop can help with this task.
As shown in Figure 4, this process can be achieved more quickly, crisply, and therefore more clearly in Batsh than in Bash scripts. The script performs all the instructions in curly brackets as long as the condition in the parentheses is met.In Figure 4, the numeral in number
consequently increases by 1 until number
contains the value 11, and you fall out of the loop. The photos can now be converted using this loop.
Listing 6 shows the matching programming code. First, the while
loop puts together the file names of the original and converted photos using the current number
.
Listing 6
Looping
The Batsh function call
starts an arbitrary program – here, the ImageMagick convert
command – that expects the file name of the program to be executed as the first parameter. All remaining parameters are forwarded by call
directly to convert
.
Depending on which program is executed and how many parameters that program requires, you can pass any number of arguments in the call
statement. In this example, convert
must be installed on both Linux and Windows so that the resulting scripts work on both systems. Batsh is not yet familiar with the for
loop frequently used in Bash scripts.
Reading Circle
Listing 6 assembles the file names, but it would be more elegant to let the script deliver the file names in a directory and then feed them one after another to convert
. Batsh provides the built-in function readdir
for this:
allfiles = readdir();
In the background, readdir()
calls ls
in Linux or dir/w
in Windows and simply returns the results. However, this leads to two problems. First, you have to be careful with file names provided by ls
, because they could contain special characters that Bash later interprets incorrectly. Second, readdir()
simply outputs a long character string with the file names, and Batsh does not currently offer a way to split strings into individual parts.
« 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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.