Thursday 12 December 2019

Gambas Scripts

If you are familiar with Gambas (or Basic) you shouldn't have too much trouble writing Gambas scripts.

Although scripting is often used for small programming tasks, it can be used to create more ambitious projects.

It may also be useful on headless/GUI-less systems, like the Raspberry Pi.

As Gambas scripts can be written using a simple text editor, you don't need the full Gambas IDE graphical interface to write these scripts. A Gambas script file is basically just a text file with 'execute' permission.

Hello World

So let's start with a simple 'hello world' example. Type this code into a file:-

#!/usr/bin/env gbs3

Print "Hello World!"

Save the file with a suitable name. It doesn't need a special file extension, so let's call it: gscript_ex1

Use the file manager to set file Permissions (via file Properties) to allow execution as a program.

Navigate to the directory containing gscript_ex1 and open a terminal. Type:-


Terminal should now look similar to this:-

Gambas script scripter hello world

Like all scripts in Linux, the first line must be the "shebang" which points to the Gambas script interpreter.

This simple example could be expanded to use routines like this:-

#!/usr/bin/env gbs3

Public Sub Main()

Private Sub Hello_1()
    Print "Hello England"

Private Sub Hello_2()
    Print "Hello Scotland"

Private Sub Hello_3()
    Print "Hello Wales"

{remember: don't forget to set file "execute" permission for each script file}

using arguments

You can pass arguments to a script as illustrated in this next example (gscript_ex3):-

#! /usr/bin/gbs3

Dim iResult As integer

iResult = ARGS[1] * ARGS[2]

Print ARGS[1] & " times " & ARGS[2] & " = " & iResult

When you run this script, include two integers as arguments, like this:-

./gscript_ex3 7 3

The output from this script should be:-

7 times 3 = 21

ARG[0] is a special reserved argument. Try printing it, and see what you get!

calling methods in class files

We can also create a class and call its methods, by including the "INCLUDE"  directive.

Create a file called gMaths.class:-

#!/usr/bin/env gbs3


Static Public Function add(x As integer, y As integer)As integer
    Return x + y

Static Public Function multiply(x As integer, y As integer)As integer
    Return (x * y)


Now create a second file called gscript_ex4:-

#! /usr/bin/gbs3

INCLUDE "gMaths.class"

Dim iResult As integer

iResult = Maths.add(ARGS[1], ARGS[2])
Print ARGS[1] & " + " & ARGS[2] & " = " & iResult

iResult = Maths.multiply(ARGS[1], ARGS[2])
Print ARGS[1] & " x " & ARGS[2] & " = " & iResult

Now run gscript_ex4 and provide 2 integers, like this:-

./gscript_ex4 15 5

The main program should run and use the Maths.add and Maths.multiply methods from the gMaths.class.

using Gambas components

I believe that any Gambas components listed under Interpreter Enhancements are available in Gambas Script. But any that are not can be used in your scripts to extend functionality by including the USE directive.

In the next example I want to create 'sockets' so I use the gb.Net component:-

#! /usr/bin/gbs3

USE ""

Public mysock as new ServerSocket

Public Sub Main()


    mysock.Type = Net.Internet
    Print "Listening..."


If you run this, you can check that its running by typing in a second terminal:-

netstat -at

By using the techniques and options illustrated above, it should be possible to build quite powerful applications, especially for headless systems such as servers and devices.

No comments:

Post a Comment