patpitchaya - Fotolia
Learn shell scripts: A sys admin's guide to automation
Shell scripts and sys admins should be best friends. A day rarely goes by without using some form of shell script or another. Learn how those scripts work and start writing them.
Shell scripts enable every level of system administration and user interaction on Unix and Linux systems. Several system administration tasks can be automated with shell scripts. Disaster recovery procedures are mostly shell scripts. And end users even learn shell scripts to perform repetitive jobs. To use them in daily operations, learn what makes up a shell script and how to write one.
A shell script is a series of Unix, Linux or Shell commands stored in a file. Instead of executing each command individually on the command line, the file runs as a command. This file can have any unique name; sys admins can use any text editor to create it. While it is not required, shell scripts are usually named with a file extension that reflects the shell script interpreter that runs it.
Shell scripts reside at every level and in any directory. To determine if a file is a shell script, use the file command. The $ file somefilename script details what the file contains before you open it using a text editor.
The lowdown on shell interpreters
There are hundreds of shell interpreters available. The most common are Bourne, Bash, Korn and C.
The Bourne shell script was the first shell. All Unix and Linux systems can execute Bourne shell scripts. If you work in a multi-OS environment, learn shell scripts with the Bourne shell. Users often name Bourne shell scripts with the .sh file extension.
Bourne again shell (Bash) scripts are a superset of Bourne shell built-in commands. It is the typical default shell on a Linux system, but usually must be added to commercial Unix systems. Bash shell scripts also use the .sh file extension.
Korn shell scripts are also based on the Bourne shell and act as a different superset of Bourne shell built-in commands. Korn shell scripts are used across all Unix systems -- where they work the best -- but administrators may add Korn to Linux systems. This shell's script has features and functions built in that aren't found in other shells, dealing with function processing, auditing and security. The Korn shell's scripts are typically named with the .ksh file extension.
C shell scripts are products of the C language syntax that most Linux and Unix systems can run, though differences between C shell interpreters can affect the success of script execution. The typical file extension for C shell scripts is .csh.
The syntax and built-in commands between various shells is different; scripts will not necessarily run on different shell script interpreters. For example, a C shell script probably cannot execute in Bourne shell, and a bash script may not execute in Korn shell -- but there are techniques and methods to writing shell scripts that run in multiple interpreters.
Worlds collide: Writing shell scripts that work in Windows, Linux and Unix
Many sys admins ask if Windows PowerShell can run the same shell scripts as Unix and Linux systems. While Windows PowerShell can run some very simple Bourne shell commands, the Windows environment remains mostly separate from Linux or Unix. The next version of Windows, however, will feature embedded Ubuntu Linux, which will enable the user to run Linux scripts.
Seven simple steps to write shell scripts
Shell scripts can be large and complex, as with any programming language. In addition to executing Unix and Linux commands, a shell script can contain built-in shell commands that the shell interprets; these commands do not exist as a separate file on a Unix or Linux system. To learn shell scripts, sys admins should follow a few steps:
1. Determine a series of commands that will accomplish a specific task. To start, create a "Hello World!" script.
2. Determine which shell interpreter will execute the shell script. This example uses a Bourne shell script.
3. Determine a file name for the shell script, such as "helloworld.sh".
4. Using the text editor you choose, open a file and enter the series of commands:
$ vi helloworld.sh
echo "Hello World!"
5. Save the contents of the file and close the text editor with the command:
6. Add an "execute" permission to the shell script file:
$ chmod +x helloworld.sh
7. Execute the file from the command line:
Include the dot-slash "./" at the beginning of the script file -- Unix and Linux systems don't typically search the current directory for commands.
The exit 0 command at the end of the script isn't required. However, when it doesn't exist, the exit code of the shell script is the exit code of the last executed command. In this example, the exit code is the echo command. I recommend adding an exit (or return) command at the end of every shell script.
To enable others to read and execute your shell scripts, follow these basic rules:
- Embed documentation into your shell script with comments, denoted by an opening hash character, that contain the following information:
# Configured Usage:
- Use a template to achieve a consistent look and feel. Numerous shell script templates are available for download. You can also create your own template or work from one recommended by your organization's systems administrators.
- Consider attending a training to learn shell scripting techniques; many companies offer online Bourne shell classes. There are also several instructor-led trainings available.
About the author
Dana French has worked in the IT industry in a variety of capacities for over 30 years, including programming in several languages, project management, systems administration, data center management and business management. With extensive experience in both technical and business management, he possesses a unique perspective of business continuity.
Going for advanced shell scripts? This book on shell scripting is for pros
Script your way to automation in your ops department
IT ops careers depend on scripting prowess