Shell script basics Andrew Mallett | Updated July 2017

FreeBSD is a fully fledged programming environment in its own right and comes with its own compiler (gcc). Shell scripts are executable files containing a number of commands to perform various functions, very similar to DOS batch files but much more powerful.

With a bit of knowledge about scripting you can create tremendously powerful scripts, which would be much harder to achieve in a MS Windows environment. Indeed, once you know a bit about how a Unix system runs, then it's actually often easier than Microsoft Windows to achieve the same result. No knowledge of Visual Basic or in-depth programming necessary.

A little knowledge goes a long way

To get into shell scripting some basic knowledge of how Unix works is required. So, back to basics like input/output. One of the many weird-but-wonderful things about Unix is that it treats everything like a file. For instance all hardware devices are treated just like any other files and live in /dev. Keyboard and screen? Yep, just files.. This also makes Unix extremely straightforward to manipulate and work with.

By default, a command treats your terminal (read:keyboard) as the standard input file from which to read in information. Your terminal (read:screen) is also treated as the standard output file to which information is sent from the command.

Here is a diagram for those who prefer diagrams..

STANDARD INPUT (keyboard) --> System --> STANDARD OUTPUT (screen)

This action can be changed by redirecting standard input and standard output from and to any other file.

Redirecting input and output

While standard input is usually from the keyboard, the output of an operation can be redirected to an alternative destination. For instance, instead of (or as well as) to the screen, the output of a calculation or search could be sent to a file or a printer. Outputting to file in particular, is not an uncommon operation.

Example: The who command displays who is currently logged on and outputs the result to standard output (screen)..

root             ttyv0    May 14 23:04
andym            ttyv1    May 14 23:05
andym            ttyp0    May 14 23:00 (andyxp)
bugger           ttyp1    May 14 23:03 (andyxp)
twit             ttyp2    May 14 23:04 (andyxp)

Instead getting sent to screen, this output can be redirected to a file using the greater than symbol, >..

who > whoisthere.txt

The above command puts the output of the who command into a file called whoisthere.txt, instead of to screen. Don't forget to run a pwd first, so you know which directory the file will be outputted to.

Note that if the file whoisthere.txt already exists, then the contents of this file will be overwritten with the new command. To append to the contents of the file without overwriting the contents, use two greater than symbols..

who > whoisthere.txt
ls -la >> whoisthere.txt

Connecting commands together

UNIX allows you to link commands together using a pipe. The pipe acts as a temporary file which only exists to hold data from one command until it is read by another. The pipe takes the standard output from one command and uses it as the standard input to another command.

command1 | command2 | command3

The | (vertical bar) character is used to represent the pipeline connecting the commands. To pipe the output from one command into another command:

who | wc -l

This command tells you how many users are currently logged in on the system by using the wc command to count the number of lines (1 line = 1 user).

The standard output from the who command - a list of all the users currently logged in on the system - is piped into the wc command as its standard input. Used with the -l option this command counts the numbers of lines in the standard input and displays the result on the standard output.

Finally..a first script

There is a special device know as /dev/null to which you can redirect unwanted output. This is a null (non-existent) device represented by the file null in the directory /dev.

Here's a little script which puts all this together. It takes the output from the who command and uses this as the input for the grep command, to search fro teh string "andym". An if statement then echoes the result to screen..

if who | grep -s andym > /dev/null
echo andym is logged in
echo andym not available

The -s option causes grep to work silently and any error messages are directed to the file /dev/null instead of the standard output.