Technical skill

Programming

Subject: .../Programming

Give the computer a repeatable set of steps

Programming means writing instructions that a computer can follow to handle a task. For example, someone may need to check a long log file for the marker ERROR. A small program can open the file, search for that marker, and report whether it found one. The program's inputs, expected result, and possible failures should be clear before it runs.

A shell script is a text file of commands run by a shell such as Bash. It is useful when a task mostly connects existing command-line tools. A general-purpose language such as Python may be easier when the task needs structured data, complex parsing, or a lot of application logic. In either case, learn what the commands report when they succeed or fail. Unix tools often use zero for success and nonzero for failure, though a command may use several numbers for different outcomes.

This Bash script searches the file path supplied as its first argument:

#!/usr/bin/env bash
file=${1:?Usage: scan-log FILE}
if grep -n -- 'ERROR' "$file"; then
  printf 'At least one error marker was found\n'
else
  status=$?
  if (( status == 1 )); then
    printf 'No error markers found\n'
  else
    printf 'Could not scan the file\n' >&2
    exit "$status"
  fi
fi

The assignment puts the first argument into file; the ${1:?...} form prints a usage message and stops if the argument is missing or empty. grep -n prints matching lines with their line numbers. The quotes around "$file" keep a path with spaces as one argument. -- tells GNU grep that options have ended, so a file name beginning with a dash is not treated as a flag.

Tell different results apart

GNU grep returns status 0 when it finds a match, 1 when it finds none, and 2 when it cannot perform the search. The script treats “no error marker” as a completed search, but reports an actual search problem on standard error, the separate stream used for error messages, and exits with the error status. Another program can use that exit status to decide what to do next.

Try a normal file, a missing file, and a path containing spaces. If a program changes files, permissions, services, or network settings, make the target clear and preview a risky action where possible. Keep credentials out of source files and logs. Prefer structured command output to parsing a display meant for people. For repeated runs, consider whether the operation is idempotent: running it again should not create unintended duplicate changes.

A syntax check only proves Bash can parse a file. The program's assumptions, permissions, error handling, and side effects determine what it does in practice. If a command restarts a service, check the service afterward instead of assuming the restart command's success means the service is available.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Junior C# Programmer preview
image descriptionJunior C# Programmer
by Infinite Education
2827 users
Skillset Senior C# developer preview
image descriptionSenior C# developer
by Ann Mary
2060 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Android Developer preview
image descriptionAndroid Developer
by Jack Wilson
1830 users
Show more
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful