4 Important Things You Need to Know About Shell Scripting
By Nazifa Alam
Shell scripting is typically used to perform various system administration tasks such as disk backups, system log evaluation and installation of complex programs. It is executed by a UNIX shell command line interpreter for file manipulation, program administration, and text printing.
Shell scripts are composed of a set of commands, which are performed by the Shell as instructed depending on its set parameters. In other words, the Shell is instructed to perform certain procedures where instructed. Individual commands are performed in a sequence to perform the operation and for this reason, it is known as shell scripting. Shell scripting is useful to have in place for repetitive tasks regarding system administration.
The basic layout of the shell architecture of the operating system consists of:
- Shell (one of the two main components)
- Operating System
- Kernel (one of the two main components)
Types of Shell Scripting
There are several types of shells that can be used for Shell Scripting and listed below, are four available shells that remain available to date:
- Bash – Also known as Bourne Again Shell, it is the highest-level script language available across all Linux and Debian based operating systems.
- CSH or C Shell – Which uses the C programming’s shell syntax and is more identical to C.
- KSH or Korn Shell – This shell is a UNIX based scripting program similar to Bash. It is a high-level programming language.
- TCSH – Which is a more developed version of Berkley Unix C shell. There is no exact full form of TCSH and the shell supports C syntax.
As Bash is the most commonly available shell that can be applied across all Linux based platforms (in addition to Mac OS), the following examples that will be covered further down are applicable to Bash. Due to the universality of the shell, scripting should not be an issue regardless of the platform.
As scripting uses ‘administrator’ or ‘superuser’ privileges, it is important to put the intended scripts to test prior to entering them. It is important to use the right option for certain commands for example, i for rm commands as your interaction is needed.
Creating a Shell Script
Creating a shell script includes the following steps:
- Using a vi editor or other editors to create a script and naming the file with extension .sh.
- Beginning the script using #! /bin/sh.
- Writing the script and saving the file.
- Running the script using bash file[.]sh.
Below is an outline of a script example involving the use of the is command to obtain the list of files and directory from the current directory:
The script needs to begin with:
And then followed up with,
echo “what is your name?”
echo “Hello, $name”
The purpose of shell variables is to store data in the form of characters or number data. Specifically for this procured example, the name variable to store” John” (or other names) as a name can be indicated using $ as the prefix. The echo commands serve to show a text string on a file or a standard output.
When the file runs as .sh, regarding this example, the following should appear:
What is your name?
The following commands to be noted of are the following:
- gzip – For creating, extracting or viewing [.]gz files.
- zip – For creating or extracting zip files
- find – For searching certain files.
- echo – For showing a line of text.
Discussed below are some relevant programming components such as variables, control structures and other important aspects to be aware of:
- Every script should begin with ‘#! /bin/bash’ so the script gets directed to the Bourne shell. The function ‘#!’ directs the script to the interpreter called shebang.
- Every new line serves as a new command.
- Comment lines should begin with a #.
- Commands should be surrounded by ().
The Hash-Bang Hack
The most direct way to single out the file as a script is to ensure that the first line is:
Beginning the script with # in addition to following it up with ! forces the script to be performed with the shell the line points to.
Each New Line is a New Command
Every new line is a new command or a constituent of something larger. For example, if/then/else statements will need multiple lines to complete however, each of these new lines are components of the entire statement. It is important to make sure that new commands interrupt these lines to prevent errors from occurring. Text editor should be turned off using ALT+L.
Adding in Comments
If you begin a line using #, the line is ignored and turned into a comment line. Using this newly created comment line, you can create reminders to yourself concerning the output of the previous command or what the next command will do. Applying this practice will help you and others edit your scripts more easily where needed. It is also important not to follow #s with !s due to the previously mentioned Hack-Bang function being an exception of this.
As the basics of shell scripting are being touched down upon, it is important to mention that parentheses should be used to create command substitutions. This is because it works better to place commands within other commands using this method.