Skip to content

Sample Programs

Brandon Ingli edited this page Jan 19, 2022 · 7 revisions

These are some sample programs for BADI. They'll eventually be moved into automated testing, proper documentation, and what not.

Hello, World

echo is used to send output.

echo "Hello, World!"

Output:

Hello, World!

Basic Arithmetic

expr is used to indicate a standalone arithmetic expression. The result is sent to output.

let is used to assign the value of an arithmetic expression to a variable. Nothing is sent to output without being echoed out.

BADI differs from bash here in that floating point numbers will be supported and the default type. We also introduce an integer division operator.

expr 5 + 2
let a = 5 + 2
echo $a

expr 5 - 2
let b = 5 - 2
echo $b

expr 5 * 2
let c = 5 * 2
echo $c

expr 5 / 2
let d = 5 / 2
echo $d

expr 5 // 2
let e = 5 // 2
echo $e

expr 5 % 2
let f = 5 % 2
echo $f

expr 5 ** 2
let g = 5 ** 2
echo $g

Output:

7
7
5
5
10
10
2.5
2.5
2
2
1
1
25
25

For Loops

Like bash, BADI supports a few basic types of for loops. Indentation does not matter for the interpreter, and is only for ease of reading.

for i in 1 2 3
do
  echo $i
done

for j in {4..6}
do
  echo $j
done

for k in {7..10..2}
do
  echo $k
done

Output:

1
2
3
4
5
6
7
9

While Loop

BADI supports just one form of while loop. Modifications to the loop variable must be made in the loop body. Note that there are subtle syntactical differences between BADI and bash in the loop condition.

x=1
while $x <= 5
do
  echo $x
  let x = $x + 1
done
echo "X is $x"

Output:

1
2
3
4
5
X is 6

Functions

Unlike bash, BADI has only one function syntax, using the function reserved word. Parameters passed are accessible through special numbered variables, starting at $1. $0 is reserved for future use. Use $# to get the number of arguments passed.

function hello {
  echo "Hello $1"
  echo "Got $# args"
}
hello "World" "again"

Output:

Hello World
Got 2 args

Clone this wiki locally