Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Small scripts and software packages

After this lesson: I can write and run a small shell script, and say which package a program came from.

55 min lesson13 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

The report you typed every Monday

A shell script is a file of commands you write once and run whenever you need them. A package is how a program, such as one your script uses, gets onto a Linux machine.

A shop sends a file of visitor numbers every week. Each Monday you type the same three commands to find the busiest day. One Monday you type them wrong and send the wrong day to your manager. A script would not have slipped.

By the end of this lesson

Note

I can write and run a small shell script, and say which package a program came from.

Key words

WordWhat it meansExample
Scriptcommands saved in a file, so you can run them again
Shebangthe first line of a script, which names the program that runs it#!/bin/bash
Argumenta word you type after a command, which the command can use$1
Exit statusa number a command leaves behind: 0 means it worked$?
Packagea program and its files, bundled so the system can install it
Package managerthe tool that installs, updates and removes packagesapt

The cells students fill in are left blank in this preview.

One pipeline answers one question

The lab keeps a small file of visitor numbers at /srv/lab-web/report.csv. Each line is a day, a comma, and a number.

cut -d, -f1 keeps the first column. -d, says the columns are split at commas. tail -n +2 skips the heading line. sort -t, -k2 -n -r sorts by the second column, as numbers, biggest first. head -n 1 keeps the top line.

Cut out the first column

Command
cut -d, -f1 /srv/lab-web/report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Find the busiest day

Command
tail -n +2 /srv/lab-web/report.csv | sort -t, -k2 -n -r | head -n 1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think sort sorts numbers by size. By default it sorts as text, so 98 comes after 175 because 9 comes after 1. Leave out -n and the pipeline above prints Tuesday,98. Add -n whenever the column is numbers.

A script is commands saved in a file

A script is a text file of commands. The shell runs them top to bottom, as if you had typed them.

The first line is the shebang. It names the program that runs the file. A line starting with # is a comment, and the shell skips it.

Inside a script, $1 stands for the first word typed after its name. So the script works on any file you name.

Write the script into a file

Command
printf '#!/bin/bash\n# Prints the busiest day in a CSV file.\ntail -n +2 "$1" | sort -t, -k2 -n -r | head -n 1\n' > busiest.sh

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Try to run it

Command
./busiest.sh /srv/lab-web/report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask bash to read it instead

Command
bash busiest.sh /srv/lab-web/report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Let the file be run

Command
chmod +x busiest.sh

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run it

Command
./busiest.sh /srv/lab-web/report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Every command leaves an exit status

When a command ends it leaves a number. 0 means it worked. Any other number means something went wrong.

$? holds the number the last command left. Scripts and cron use it to know whether a job worked.

A command that fails

Command
ls /nothere ; echo "status $?"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The script that works

Command
./busiest.sh /srv/lab-web/report.csv ; echo "status $?"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a file that cat can show will also run. Reading and running are different rights. A new file has no x for anybody. Run chmod +x first, or ask bash to read it.

Software comes in packages

On Ubuntu a program arrives as a package. The package manager keeps a list of what is installed and which package owns each file.

dpkg reads that list. apt installs from the internet and needs sudo. Add -s to apt-get install and it only pretends, so it is safe to try.

Where is the program?

Command
command -v curl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Which package owns that file?

Command
dpkg -S /usr/bin/curl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Is the package installed?

Command
dpkg -s curl | grep -E "^(Package|Status)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Pretend to install it again

Command
apt-get install -s curl 2>&1 | tail -n 3

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: the apt verbs

You typeWhat it doesNeeds sudo?
sudo apt updateRefreshes the list of what can be installedYes
sudo apt install htopInstalls a package and what it needsYes
sudo apt remove htopRemoves the packageYes
apt-get install -s htopShows what an install would do, changing nothingNo
dpkg -S /usr/bin/curlNames the package that owns a fileNo

The cells students fill in are left blank in this preview.

Going further

Type these yourself. Change the script so it prints the quietest day. Which one flag do you add or remove?

Then work out the cron time that would run busiest.sh at 07:30 every Monday. On a server you would put that line in /etc/cron.d, with the user before the command.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.