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
| Word | What it means | Example |
|---|---|---|
| Script | commands saved in a file, so you can run them again | |
| Shebang | the first line of a script, which names the program that runs it | #!/bin/bash |
| Argument | a word you type after a command, which the command can use | $1 |
| Exit status | a number a command leaves behind: 0 means it worked | $? |
| Package | a program and its files, bundled so the system can install it | |
| Package manager | the tool that installs, updates and removes packages | apt |
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
cut -d, -f1 /srv/lab-web/report.csvShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Find the busiest day
tail -n +2 /srv/lab-web/report.csv | sort -t, -k2 -n -r | head -n 1Shown 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
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.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Try to run it
./busiest.sh /srv/lab-web/report.csvShown 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
bash busiest.sh /srv/lab-web/report.csvShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Let the file be run
chmod +x busiest.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Run it
./busiest.sh /srv/lab-web/report.csvShown 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
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
./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 -v curlShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Which package owns that file?
dpkg -S /usr/bin/curlShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Is the package installed?
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
apt-get install -s curl 2>&1 | tail -n 3Shown 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 type | What it does | Needs sudo? |
|---|---|---|
| sudo apt update | Refreshes the list of what can be installed | Yes |
| sudo apt install htop | Installs a package and what it needs | Yes |
| sudo apt remove htop | Removes the package | Yes |
| apt-get install -s htop | Shows what an install would do, changing nothing | No |
| dpkg -S /usr/bin/curl | Names the package that owns a file | No |
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.