Basic Script Programming

Post

Posted
Rating:
#1 (In Topic #1948)
Avatar
Enthusiast
ercoupeflyer is in the usergroup ‘Enthusiast’
ercoupeflyer is in the usergroup ‘Blogger’

Creating and executing a simple gambas script

It is pretty simple and expedient to write quick script for simple tasks. It is easier than having to learn the ins and outs of csh or bash scripting.
Here is a quick example to get you started. The following program contains a main procedure but they are not required.
From the verbose output it can be seen scripts are not magic, the scripter actually creates a project on the fly and then compiles the project and executes it in place.
It can be seen from the execution time that all this takes about 0.004051073992741 seconds, Gambas is sure fast!!!

The #! explained, we use the env program to pass the environment
env - run a program in a modified environment
-S  means pass all following - parameters to the gbs3 program
-v on the gbs3 program give a little more details regarding the program to be run
You can cut and paste the following into a file called helloworld, chmod it to 777
execute it with ./helloworld
Scripts have Special keywords all start with #Script these are used to define application specific information.
#!/usr/bin/env -S gbs3 -v
'' print all the parameters passed on the command line or just print hello world
#Script Title="Hello World"
#Script Startup="main"
#Script Version="0.0.1"
' Gambas module file
Public Sub Main()
  if args.count < 2 then
    Print "Hello World"
  else
    Dim PrtMessage as string[] = args.all
    PrtMessage.delete(0)
    Print PrtMessage.join()
  endif

  quit 0

Catch
  Print Error.text;;error.where
End

Output without parameters should look like this:

gbs3 : Create project: /tmp/gambas.1000/638100/project.tmp
gbs3 : Begin Script Process: HelloWorld.gbs
gbs3 : Processing Script File : HelloWorld.gbs
gbs3 : MAIN Defined  : /home/brian/bin/HelloWorld.gbs.7
gbs3 : Before Compile————————————————–
gbs3 : 
    1 :' #!/usr/bin/env -S gbs3 -v 
    2 :'Auto Generated Script 11/04/2022 17:52:44.802 Version (3.17.90)
    3 :'#Script Title="Hello World"
    4 :'#Script Startup="main"
    5 :'#Script Version="0.0.1"
    6 :' Gambas module file
    7 :Public Sub Main()
    8 :if args.count < 2 then
    9 :  Print "Hello World"
   10 :else
   11 :  Dim PrtMessage as string[] = args.all
   12 :  PrtMessage.delete(0)
   13 :  Print PrtMessage.join()
   14 :endif
   15 :quit 0
   16 :Catch
   17 :  Print Error.text;;error.where
   18 :End

gbs3 : _____________________________________________
gbs3 : Completed Generation of files 
gbs3 : .project File
# Gambas Project File 3.0
Title=Hello World
Startup=main
Vendor=Ordinary
Version=0.0.1
License=General Public License 2.0

gbs3 : /usr/bin/gbc3 -a /tmp/gambas.1000/638100/project.tmp 2>&1
gbs3 : Compile exit code=0
gbs3 : Cache="/tmp/gambas.1000/script-cache", md5 ="aa10f262a1c431f1b0e7582e73ab4190", FileName="HelloWorld.gbs",sPath="/home/brian/bin/HelloWorld.gbs"
gbs3 : /usr/bin/gba3 -v -o /tmp/gambas.1000/script-cache/aa10f262a1c431f1b0e7582e73ab4190/HelloWorld.gbs /tmp/gambas.1000/638100/project.tmp
gbs3 : —————————————- —————————— ——– —–
Path                                     Abbrev. path                       Size Index
—————————————- —————————— ——– —–
.startup                                 .startup                             40     0
.project                                 .project                            122     1
.gambas                                  .gambas                               -     2
.lang                                    .lang                                 -     3
.gambas/MAIN                             /2:MAIN                             456     4
—————————————- —————————— ——– —–
Writing archive format version 2
/tmp/gambas.1000/script-cache/aa10f262a1c431f1b0e7582e73ab4190/HelloWorld.gbs
Total size: 804 bytes

gbs3 : /usr/bin/gbr3 -a /home/brian/bin/HelloWorld.gbs /tmp/gambas.1000/script-cache/aa10f262a1c431f1b0e7582e73ab4190/HelloWorld.gbs
gbs3 : ***************** Script Output Begins Bellow This Line (12/07/2025 00:27:12.139)**************************
Hello World
gbs3 : ***************** Script Output Ends  ****(12/07/2025 00:27:12.143)**********************
gbs3 : Execution Time = 0.004051073992741 

Output after removing the -v option should look like this:

Hello World
An even simpler helloworld script, cut and paste this into a file and execute it. The output is just hello world!
#!/usr/bin/env gbs3
print "Hello World"

Last edit: by ercoupeflyer

Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Quincunxian is in the usergroup ‘Blogger’
Thanks for posting this.
I must admit to knowing that scripting was available but never had the need ( as in an application need) to learn how to do it.
The only thing I can think of is to create an interface where a user/player could create a script from within a Gambas application to modify that application - essentially a mod I guess.
Yeah, that would take a lot of work but it's something I can put my hands on as a 'real use case'.

Going to think about that for a while.



Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#3
Avatar
Enthusiast
ercoupeflyer is in the usergroup ‘Enthusiast’
ercoupeflyer is in the usergroup ‘Blogger’
Scripter already has the ability to be integrated as a component in your application. It is able to on the fly compile and add classes to your running application.
have a look at this article I wrote a while ago.

Gambas Scripting — Gambas Documentation
Online now: No Back to the top
1 guest and 0 members have just viewed this.