NASutils Knowledge BaseUnofficialPolski

Bash in 30 lessons

Bash is the shell you type into. A script is a file of the same commands, with variables, conditions and loops.

Start: Run a script

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. 1Run a scriptThe first line names the shell.
  2. 2Keep a value in a variableNo spaces around =.
  3. 3Read the command line$1 is the first argument, $# is the number of arguments and "$@" is all of them, each whole.
  4. 4Ask the userread -rp asks and keeps the answer.
  5. 5Use exit codesEvery command ends with a code.
  6. 6Decide with ifif, elif, else and fi.

Chapter 2

Decisions and loops

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

  1. 7Test files and text-f, -d, -e and -s ask about files, == compares text with a pattern.
  2. 8Choose with casecase compares a word with patterns; *) takes the rest.
  3. 9Repeat with loopsfor goes over words or counts; while and until test a condition.
  4. 10Read a file line by linewhile read takes one line per turn and IFS cuts it into fields.
  5. 11Count with numbers$(( )) counts whole numbers.
  6. 12Write functionsA function reads $1, keeps its names local and gives text back through its output.

Chapter 3

Data, functions and the command line

Keep many values together, write functions and read the command line.

  1. 13Return a codereturn sets the code that if and $? read.
  2. 14Keep many valuesAn array keeps its values apart even with spaces; mapfile reads a file into one.
  3. 15Count with namesdeclare -A makes an array with names for its values.
  4. 16Cut text without other commands# cuts from the front, % from the end, / replaces and ,, makes small letters.
  5. 17Join commandsThe sign | passes the output of one command to the next; $( ) puts an output into a text.
  6. 18Steer the output> writes a new file or replaces the old one, >> adds, 2> takes the error output and << gives a command several lines.

Chapter 4

Files and errors

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

  1. 19Write and read a fileWrite with printf and >, add with >> and read with while.
  2. 20Stop at the first error-e ends the script at a failed command, -u at a variable without a value, and pipefail counts a failure anywhere in a pipe.
  3. 21Clean up at the endmktemp -d makes a work folder and trap EXIT removes it, also after an error.
  4. 22Read optionsgetopts reads -l 95 and -q; shift leaves the rest.
  5. 23Build a calculatorA pattern with =~ checks the line and BASH_REMATCH keeps its parts.
  6. 24Read dfRead the second line of df -P -k and compare the percentage with a limit.

Chapter 5

Tools for the NAS

Programs that read the state of the NAS and report it.

  1. 25Find the largest filesfind prints the size and path of every file in the folder and its subfolders, sort puts the largest first.
  2. 26Summarise a logawk counts by day and level in one pass.
  3. 27Back up a folder with a datetar packs the folder, a test reads the archive, and only the newest three stay.
  4. 28Read smartctlThe script reads the output of smartctl -A, marks four error counters above zero and shows the temperature and the days of work.
  5. 29Build a notepadmapfile loads the notes into an array, case chooses add, list or delete.
  6. 30Finish a toolFunctions read the limit, the volumes, the disks and the log; the report goes to the screen and to report.txt.

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

Run it

chmod +x makes the file a program. The words after its name go to the script.

chmod +x 29-notepad.sh
./29-notepad.sh add "Buy two disks"
./29-notepad.sh list

One more lesson

The programs to download