Amongst other things, FreeBSD is a fully fledged programming environment in its own right, 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.
I have created a number of custom shell scripts to assist me with system administration, including a script which automatically pages my mobile phone, if a user attempts to join a system to my network which has an unknown MAC Address.
This is one of the many truly coolerino things about working with Unix.
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. Yes folks, once you know a bit about how a Unix system runs, then it's actually often easier than MS Windows to achieve the same result. No knowledge of Visual Basic or in-depth programming necessary. I have long believed that running Unix is where Networking truly meets Programming. Fantastic!
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 weirdass-but-wonderful things about Unix is that it treats everything like a file. Hardware devices? All just files in /dev. Keyboard and screen? Yep, just files.. I know, it's weird, but it also makes Unix extremely straightforward to manipulate and work with. That's one reason why so many people love it.
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's a diagram for those who prefer a diagram..
STANDARD INPUT (keyboard) --> System --> STANDARD OUTPUT (screen)
This concept of standard input and standard output are important basic concepts and essential if you want to cut the mustard with the Big Boys. 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
5
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
then
echo andym is logged in
else
echo andym not available
fi
The -s option causes grep to work silently and any error messages are directed to the file
/dev/null instead of the standard output.
- A.