doc • scripting

Gambas Scripting

Introduction

The Gambas interpreter can be used to execute script files. The scripter gbs3 works by creating a temporary project from your script, by compiling it, and by running it. The compiled project is cached so that it is used immediately if you run the script again.

To create a Gambas script file, make it executable and write this preamble at the beginning:

#!/usr/bin/env gbs3

The Gambas script interpreter gbs3 can execute internal commands directly:

#!/usr/bin/env gbs3

Print "Hello"

The Gambas script can be used like a module in a common Gambas project, the interpreter automatically executes a Main() method:

#!/usr/bin/env gbs3

Public Sub Main()
    DoPrintHello()
End

Private Sub DoPrintHello()
    Print "Hello"
End

So the code can be organized with private and public subs and functions, constants and variables just like in Gambas modules inside Gambas projects.

Passing Arguments To The Script

There is an ARGS[] array which contains arguments passed by command line.

#! /usr/bin/gbs3

Dim arg as String
Dim i as Integer

for each arg in ARGS
    Print "Argument Nr. " & i & " = " & arg
    Inc i
next

See that Position 0 of ARGS is used by Gambas itself, it contains the path to the cached compiled project:

$ ./test.gbs ding dong
Argument Nr. 0 = /tmp/gambas.1000/script-cache/b1826db433d3855de7e021ca9ad34b87/test.gbs
Argument Nr. 1 = ding
Argument Nr. 2 = dong

Components

Gambas components can be used with the command USE

Including Other Gambas Script Files

Other Gambas script files can be included with the command INCLUDE