Unix provides a powerful programming environment in which to create customised routines, consisting of strings of commands running from a single file. In DOS these were called batch files, in Unix they are known as shell scripts. This article introduces the basic concept of scripting and demonstrates a few of my own custom scripts. For more detailed background see Creating and Running Shell Scripts.
Take the ls command as an example. In its basic form it lists the files and directories in the current folder, in wide fashion..
.cshrc bin dev lib rescue sys
.profile boot dist libexec root tmp
.snap cdrom etc mnt sbin usr
COPYRIGHT compat home proc src var
Whereas the pwd command is used to print working directory or show the curently logged directory..
A simple script might combine these two commands together in order to create a more powerful amalgam. Using the vi editor, the scripts are created in clear text..
Save and exit the editor. Note that the script is simply called 'l' which is not already a standard Unix command.
Also note the script has been created in the /bin directory. This allows us to run the script from anywhere in the system as bin is in the system path.
All that remains is to make the file executable. In DOS and Windows, the three traditional executable file types end in .com, .bat and .exe. Under Unix literally any file type can be made executable, no matter whether it has an extension or not. Use of the chmod command usually requires root privileges so I will include the sudo command..
sudo chmod 755 /bin/l
Now the file is executable, I can get both a directory listing and a curent directory readout, simply by pressing 'l'
Map Drive Script
I regularly map a network drive from my Ubuntu/Mint laptop to a NAS drive running a Linux kernel. The NAS box is always running and contains files and music which are accessible from anywhere on the LAN. I can play music from any device in the house that is able to connect..
The following script maps a drive from the Linux laptop:
smbmount //nas1/sh_andym /home/andym/Music -o user=andym
The script is called /bin/mapnas1, can be run as a normal system user from anywhere in the directory tree, and simply prompts me for the share password upon connection.
A great feature of many of the graphical Linuxes such as Linux Mint, is that after successful connection an icon appears on the desktop representing the mapped share (screenshot)..
System Administration Menu Script
I sometimes perform routine tasks on my Unix systems. When I was a newb, I forced myself to type commands manually until I learned them. These days typing long strings (or even short ones) can be arduous, so I have developed a menu system (/bin/m) with a readout of basic system information such as system time, free disk space, etc. Each menu item is again, simply an executable letter in the /bin directory..
Here's the code in a text file..
Email and SMS Alert Script
While lecturing in Information Technology at TAFE, I built a teaching LAN comprising a fine mixture of Windows and Unix systems. As well as simplifying administrative tasks with the administration menu, I needed an alerter to let me know if a user attempted to plug an unauthorised system into the LAN. I devised a script for the Unix-based DHCP Server which sent both an email and an SMS together with the MAC Address, when a rogue system was detected.
Virus Update Server Script
The workstation antivirus profiles needed to be kept up to date and with nearly 100 systems on the network, I didn't want individual boxes grabbing the latest virus signatures. Instead I configured one of the unix servers to download the McAfee virus updates every morning where they would be available for the workstations to use.
Ping Tracker Script
This is a nifty way of tracking the presence of live systems on a network. The Ping Tracker routinely tests for the presence of a known IP Address and a successful ping is noted in a log file.
Online Monitor Script
The Online Monitor is similar to the Ping Tracker script, but sends an administrative alert if a system goes down (becomes unavailable) on the network.
Logfile Monitor Script
The script monitors an increase in a file's size over a specified period of time, as defined by the cron interval. This system is very handy for logfiles which grow as more data is added. In the example an Apache access log is tested for an increase in size, indicating someone's been accessing the web server..
Backup Configs Script
Backups are always necessary and this script copies all specified configuration files to a mount point on a remote server. Nifty.
For further reading, see Custom Alert Scripts which includes further development of these scripts and new ways of creating and monitoring system alerts.