BASIC in 30 lessons
The lessons use yabasic, a BASIC without line numbers. The interpreter reads your file and runs it at once, so there is nothing to compile.
The 30 lessons
Chapter 1
First steps
Print text, keep values in variables, calculate and read what the user types.
- 1Print a line
printwrites a text on the screen. - 2Several parts on a lineA comma puts parts side by side and a semicolon at the end keeps the line open.
- 3Keep a value in a variableA name that ends in
$keeps text; every other name keeps a number. - 4Count with numbersThe four operations,
intfor the whole part,modfor the remainder andstr$for a number with the decimals you choose. - 5Cut and join text
left$,right$andmid$cut a text,lenmeasures it,instrfinds a part and+joins two texts. - 6Ask the user
inputreads a number,line inputreads a whole line with its spaces.
Chapter 2
Decisions and loops
Use conditions to choose what to do and loops to repeat it.
- 7Decide with if
if,elsif,elseandendifchoose one of several blocks. - 8Join conditions
and,orandnotjoin conditions. - 9Choose from a menu
switchcompares one value with several cases. - 10Repeat with loops
forcounts,whiletests first,repeattests last. - 11Leave or skip a turn
breakleaves a loop andcontinuestarts its next turn. - 12A loop in a loopThe inner loop runs from start to end in each turn of the outer one.
Chapter 3
Data, functions and the command line
Keep many values together, write functions and read the command line.
- 13Many values, one name
dimmakes an array,arraysizegives its length. - 14Cut a line into fields
splitcuts at every separator,tokenskips empty parts. - 15Name a block of lines
subgives a block a name and values to work with. - 16Give a value back
returnhands a value to the caller;localkeeps a variable inside the sub. - 17Read the command line
peek("argument")counts the words after the program's name andpeek$("argument")takes the next one. - 18Report success or failure
exit Nends the program with a code.
Chapter 4
Files and errors
Read and write files and handle errors such as a missing file.
- 19Write and read a file
opentakes"w"to write,"a"to add to the end and"r"to read. - 20Handle a missing file
if open(...)answers 1 or 0 instead of stopping the program. - 21Read a tableRead the header, cut every row with
splitand turn its fields into numbers withval. - 22Build a calculatorPut
input,token,switchand a check for zero into one program. - 23Sort values
dataandreadfill an array. - 24Count words in a fileRead a file line by line and keep three sums.
Chapter 5
Tools for the NAS
Programs that read the state of the NAS and report it.
- 25Run a command
system$gives back what a command printed,systemgives back its exit code. - 26Read dfRun
df -P -kfor a folder, cut its second line and turn blocks into GiB. - 27Find the largest filesLet
wcandsortdo the work and print the first three rows. - 28Summarise a logCount the levels with
switchand show the text of every error. - 29Build a notepad
add,listanddeletework on an array thatloadfills andsavewrites tonotes.txt. - 30Finish a toolOne program reads volumes, disks and a log, writes
report.txtand ends with a code.
Projects
Longer programs that draw a menu, play a game, greet you at sign-in or show the state of the NAS. Each page shows the screen as the program drew it on a test NAS.
- A menu with boxes in yabasicA yabasic program draws a framed menu with a highlighted bar and opens boxes with the NAS's memory, volumes, uptime and load. You learn to read keys with inkey$ and to draw frames and colours with escape codes.
- A greeting screen in yabasicA yabasic program greets you by the hour and shows the NAS model, uptime, load, the free space of each data volume and a tip of the day in a framed banner. You learn to print frames and colours with plain print, to read the date and to pick a tip with your own random numbers.
- A NAS summary on one screen in yabasicA yabasic program shows the CPU with its cores and logical processors, the temperatures, load, memory, every data volume and the traffic of each network port on one framed screen with bars. You learn to read /proc files whole, to count distinct pairs and to pad coloured text.
- Sector hunt: a puzzle game in yabasicA yabasic game hides 14 bad sectors on a disk of 96 and lets you read the good ones by their numbers, with arrows, space and f. You learn two-dimensional arrays, a fill with a stack instead of recursion and random numbers that repeat for the same seed.
- A big clock in yabasicA yabasic program shows the time in large block digits and the date in a frame, new every second until you press q. You learn a bitmap font kept in five strings, inkey$ with a timeout as the tick of a loop and the day of the week from a date.
- Where did the space go? A bar chart in yabasicA yabasic program measures the folders of one level with du, sorts them from the largest and draws a bar chart with their sizes and their share of the total. You learn to read the output of a command, to sort two arrays together and to draw bars in half steps.
Write it
Sign in to the NAS over SSH and type these commands. micro opens an empty file; Ctrl+S saves it and Ctrl+Q closes it.
mkdir -p /share/Public/lessons
cd /share/Public/lessons
micro 29-notepad.yabRun it
yabasic runs the file. The words after the file name go to the program.
yabasic 29-notepad.yab add "Buy two disks"
yabasic 29-notepad.yab list