-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Gambas Shell GSH edited this page Dec 23, 2023
·
24 revisions
Shell variables can be called as if they were functions with the use of the @ operator
This allows any variable to be executed, any output from this call is returned in a string[] array.
Parameters may be passed into the variable call by adding a (var1,.....).
Parameters inside the variable function are referenced as $0...$n
Defined as:
$a = "For i as integer = 0 to 2\nprint i, $0, $1\nnext"
?? @a("This message",34) ' note the use of ?? which is able to print the content of most data types
output:
0 This Message 34
1 This Message 34
2 This Message 34
For i as integer = 0 to 10
echo This is a gambas variable i and the the value = {i}
next
Outputs:
This is a gambas variable i and the the value = 0
This is a gambas variable i and the the value = 1
This is a gambas variable i and the the value = 2
This is a gambas variable i and the the value = 3
This is a gambas variable i and the the value = 4
This is a gambas variable i and the the value = 5
This is a gambas variable i and the the value = 6
This is a gambas variable i and the the value = 7
This is a gambas variable i and the the value = 8
This is a gambas variable i and the the value = 9
This is a gambas variable i and the the value = 10
$a = "This Message"
echo this is the message : $a
Outputs:
this is the message : This Message
' Method for inputing data into a cli command
' gsh shell variable, note the new <<< operator is required
' < is now only for filename, or variable with a file name
$a = "this is an input string"
cat <<< $a ' print the content of $a
$a = ".profile"
cat < $a ' print the .profile
' Gambas Variable
do
for i as integer = 0 to 2
cat <<< {i} ' inputs the value of the variable
next
dim a as string = ".profile"
cat < {a} ' inputs the file name to redirect to cli
done
' a file as input, it must begin with a ./, ~/ or / or be enclosed in quotes or it is assumed to be a Gambas variable
cat < ./thisfile
cat < ~/thisfile
cat < ../ThisFile
cat < "thisfile"
' From a gsh function
Sub thisfunc(toprint As String) as string
return toprint
end
cat < ThisFunc ".profile" ' outputs the content of the .profile
cat < { ThisFunc("this message") } ' output the actual text
' Redirected output and error output in gsh
' Shell variable
ls -l > $a
print $a
'Gambas variable
do
Public a as string ' must be public as the write happens outside of the script
ls -l > a
print "a=";quote(a)
done
' To a file , the name must start with a ./ ~/ or / or be enclosed in ".."
ls -l > ./thisfile
cat ./thisfile
ls -l > ~/thisfile
cat ~/thisfile
ls -l > "bin/thisfile"
cat "bin/thisfile"
' Output to a file, notice we need to use a quotes string as the file name
ls -l > "myfile1"
cat myfile1
' to a gsh function
Sub myfunc(indata As Variant) As String
print "indata : +++++++++++++++++++++++++++++++"
print upper(indata)
print "end of data: +++++++++++++++++++++++++++"
end
ls -l > myfunc ' this function will be called repeatedly
' for each write from the cli. Mostly will store data
' In all cases it is possible to use {(expression)} on the command line to cause the
' runtime evaluation of some value
$a = "saved"
ls -l > {($a&"stuff")} ' dynamic file name
cat < {($a&"stuff")} ' dynamic file name
Here are some examples of using the full features of the gsh cli interface
' Listing with all names uppercase and sent to less
ls -l | tr [a-z] [A-Z] | less
' Listing with all name uppercase and sent to a variable
ls -l | tr [a-z] [A-Z] > $a
? $a
' Listing using our own filter and output to less
' there is a test plugin called filter we will use here
' feel free to 'edit filter' to make it do what ever you want
' or copy it as a template. It is just a gsh script.
ls -l | filter | less
' now how to capture the result code of a cli command
' It is not possible to put a cli inline with gambas code yet so
' capturing and testing will have to do for now
begin
ls -l > "myfile1" !> $a
if $a = 0 then print "ok"
end
' of course we could write for ok
ls -l && echo ok || echo failed
' and failed
ls -l /wweww && echo ok || echo failed
' so the && and || operators are supported for cli interface statements
' it would be perfectly valid to put a gsh function into this line
' lets define some simple functions
sub mygood() :; print "good" :; end
sub mybad() :; print "no good" :; end
' lets run an example both good and bad
ls -l && mygood || mybad
ls -l /dfssdfhg && mygood || mybad
' those are some basics of the cli but we can do much more
' pipefitting is built into the cli interface
' lets create two pipeline for processing the data
' One result is uppercase and one is all lower case
ls -l |> tr [a-z] [A-Z] > $b |> tr [A-Z] [a-z] > $c
? $b
? $c
' the |> and |< operators allow multiple sources and sinks for each pipeline
' this line will input both ls and ls / into cat which is piped to less
cat |< ls -l |< ls -l / | less
' of course we can use gsh subroutines in these pipelines as well
' lets define a simple example
sub numgen() :; for i as integer = 0 to 10 :; print i :; next :; end
' lets test it
numgen
' It printed 1 to 10 so far so good
' so lets use it as a source in the pipe stream
cat |< numgen |< ls / | less
' you can mix and match all of the above
It is possible to use both expansion and enumeration in a gambas program line in script. The following section detail the syntax of a enumeration or expansion. Here we outline how to use it in gambas script.
for each i as integer in $"0..10":;? i:;next ' this is an enumeration
for each s as string in $"Dname{1..3}ext{a..c}":;? s:;next ' this is a textual enumeration
For each s as string in $"*.*":;? s:;next ' this is a file name expansion
For each s as string in $"**/*.*":;? s:;next ' This is a recursive file expansion if globstar is true
for each s as string in $"dir{a..c}/*.*":;? s:;next ' this is an enumerated file expansion
' Must use ! with mkdir as conflict with gambas mkdir which does not respect current directory
'so force to use cli version with !
!mkdir examples
cd examples
!mkdir $"m{0..5}testdir"
ls
outputs:
m0testdir m1testdir m2testdir m3testdir m4testdir m5testdir
touch $"m{0..5}testdir/s{0..5}testfile"
ls m*
Outputs:
m0testdir/:
s0testfile s1testfile s2testfile s3testfile s4testfile s5testfile
m1testdir/:
s0testfile s1testfile s2testfile s3testfile s4testfile s5testfile
m2testdir/:
s0testfile s1testfile s2testfile s3testfile s4testfile s5testfile
m3testdir/:
s0testfile s1testfile s2testfile s3testfile s4testfile s5testfile
m4testdir/:
s0testfile s1testfile s2testfile s3testfile s4testfile s5testfile
m5testdir/:
s0testfile s1testfile s2testfile s3testfile s4testfile s5testfile
ls $"m*/*[2]*"
outputs:
m0testdir/s2testfile m1testdir/s2testfile m2testdir/s2testfile m3testdir/s2testfile m4testdir/s2testfile m5testdir/s2testfile
.globstar can be set to allow recursive search for matches
.globstar = true
The following are valid enumeration types
echo $"0..100" $"a..z" $"z..a" $"100..0"
outputs:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 a b c d e f g h i j k l m n o p q r s t u v w x y z z y x w v u t s r q p o n m l k j i h g f e d c b a 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
echo $"{a..c}{3..0}{z..x}"
outputs:
a3z a3y a3x a2z a2y a2x a1z a1y a1x a0z a0y a0x b3z b3y b3x b2z b2y b2x b1z b1y b1x b0z b0y b0x c3z c3y c3x c2z c2y c2x c1z c1y c1x c0z c0y c0x
the format $"x..x" always returns a string[] and can be used anywhere
for each i as integer in $"0..10"
print i;;
next
outputs:
0 1 2 3 4 5 6 7 8 9 10
expansion sets may be separated by commas
echo $"{0..5,ghk,fgh} {a..e}"
outputs:
0 1 2 3 4 5 ghk fgh a b c d e
simple declaration:
alias cls='clear()'
alias ls='ls --color=auto'
enter alias by itself to get a list of aliases
alias
output:
?? = 'lprint'
cls = 'clear'
exit = 'Quit 0'
fgrep = 'fgrep --color=auto'
foreach() = 'begin:for each value as variant in &1:&2:next:end'
grep = 'grep --color=auto'
hh = 'hist -10'
home = 'cd ~'
ifor() = 'for i as integer = &1 to &2:&3:next'
l = 'ls -CF'
la = 'ls -A'
less = 'less -c -N -R'
ll = 'ls -alF'
ls = 'ls --color=auto'
path = '? env["PATH"]'
pkill = '!kill'
ps = 'ps -l'
enter
alias cls=
this will delete your alias
if you want to keep an alias it should be added to your
~/.gshrc file
aliases may have any number of parameters, they work just like a gambas subst() function
alias jfor()='for j as integer = &1 to &2 :; &3 :; next'
notice that you can use :; combination to separate statements.
you would use this alias like
jfor(0,20,print j;;)
output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Gambas3 Linux Shell a Replacement for bash that uses Gambas syntax with extensions