NASutils Knowledge BaseUnofficialPolski

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.

Start: Print a line

Programming on the NAS

The 30 lessons

Chapter 1

First steps

Print text, keep values in variables, calculate and read what the user types.

  1. 1Print a lineprint writes a text on the screen.
  2. 2Several parts on a lineA comma puts parts side by side and a semicolon at the end keeps the line open.
  3. 3Keep a value in a variableA name that ends in $ keeps text; every other name keeps a number.
  4. 4Count with numbersThe four operations, int for the whole part, mod for the remainder and str$ for a number with the decimals you choose.
  5. 5Cut and join textleft$, right$ and mid$ cut a text, len measures it, instr finds a part and + joins two texts.
  6. 6Ask the userinput reads a number, line input reads a whole line with its spaces.

Chapter 2

Decisions and loops

Use conditions to choose what to do and loops to repeat it.

  1. 7Decide with ifif, elsif, else and endif choose one of several blocks.
  2. 8Join conditionsand, or and not join conditions.
  3. 9Choose from a menuswitch compares one value with several cases.
  4. 10Repeat with loopsfor counts, while tests first, repeat tests last.
  5. 11Leave or skip a turnbreak leaves a loop and continue starts its next turn.
  6. 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.

  1. 13Many values, one namedim makes an array, arraysize gives its length.
  2. 14Cut a line into fieldssplit cuts at every separator, token skips empty parts.
  3. 15Name a block of linessub gives a block a name and values to work with.
  4. 16Give a value backreturn hands a value to the caller; local keeps a variable inside the sub.
  5. 17Read the command linepeek("argument") counts the words after the program's name and peek$("argument") takes the next one.
  6. 18Report success or failureexit N ends the program with a code.

Chapter 4

Files and errors

Read and write files and handle errors such as a missing file.

  1. 19Write and read a fileopen takes "w" to write, "a" to add to the end and "r" to read.
  2. 20Handle a missing fileif open(...) answers 1 or 0 instead of stopping the program.
  3. 21Read a tableRead the header, cut every row with split and turn its fields into numbers with val.
  4. 22Build a calculatorPut input, token, switch and a check for zero into one program.
  5. 23Sort valuesdata and read fill an array.
  6. 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.

  1. 25Run a commandsystem$ gives back what a command printed, system gives back its exit code.
  2. 26Read dfRun df -P -k for a folder, cut its second line and turn blocks into GiB.
  3. 27Find the largest filesLet wc and sort do the work and print the first three rows.
  4. 28Summarise a logCount the levels with switch and show the text of every error.
  5. 29Build a notepadadd, list and delete work on an array that load fills and save writes to notes.txt.
  6. 30Finish a toolOne program reads volumes, disks and a log, writes report.txt and 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.

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.yab

Run 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

The programs to download