Shell scripting for system administrators: beyond the basics

In an earlier article, we studied the fundamental aspects of shell scripting and covered topics such as redirection, variables, arithmetic expansions and control constructs. In this part, we will focus on more advanced concepts that are useful for system administrators and software developers...

f. ${string#substring} – This deletes the substring from the front of the original string $string.

For example, assume that we have a string 123abcdefgh.
Then ${string#1*a} deletes 123a from the original string.
g. ${string%substring} – This deletes the substring from the back of the original string.
h. ${string/substring/replace} – This will replace the first match of $substring with the $replace. A slight variation to this is ${string//substring/replace}, which will replace all matches of $substring.

We can have nested loops in a shell script. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process. In addition to break, we also have the continue command that affects loop behaviour.

The break and continue loop control commands correspond exactly to their counterparts in other programming languages. The break command terminates the loop (breaks out of it), while continue causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle. We also have case and select constructs that are technically not loops, since they do not iterate the execution of a code block. Like loops, however, they direct program flow according to conditions at the top or bottom of the block.

In some cases we may need commands that execute faster than external commands. For this purpose we have something called built‑in commands. The basic purpose of a built‑in command is for performance reasons. Such commands usually require forking off a separate process or need direct access to the shell internals. For example, echo is a shell built‑in, but not the same as /bin/echo.
[sourcecode language=”bash”][root@localhost ~]# type -a echo
echo is a shell builtin
echo is /bin/echo
[root@localhost ~]#
There are several job control commands and some of these commands take a job identifier as an argument. Some of these commands are given below.

a. jobs – This command lists the jobs running in the background. Have a look at the following example.
[sourcecode language=”bash”][root@localhost ~]# sleep 100 &
[1] 27447
[root@localhost ~]# jobs
[1]+  Running sleep 100 &
[root@localhost ~]#
b. disown – This removes one or more jobs from the shell’s table of active jobs.
c. fg, bg – The fg command is useful if we need to switch a job running in the background into the foreground. The bg command restarts a suspended job and runs it in the background.
d. wait – This suspends the execution of shell script until all jobs running in the background are terminated.
e. times – This is useful if we are interested in getting the statistics of the system time elapsed when executing commands.

We can note that standard UNIX commands make shell scripts more versatile. Commands such as ‘ls’, ‘cat’, ‘cp’, ‘chmod’ etc are useful while writing shell scripts. There are several advanced commands that we normally come across, so let us take a brief look at some of them.

find – Basically, this command is used to search for files. A typical example and its output is shown below.
[sourcecode language=”bash”]cvs@cn-setta-2:~> pwd
cvs@cn-setta-2:~> find . -name *.desktop
In the above example, we used the find command to search for all files ending with ‘.desktop’.

grep – This is a popular multipurpose search tool. Typically used in the following format…

grep pattern [file] – This searches the target file for occurrences of pattern. This command comes with many useful options. The following screenshot (Fig 7) displays more information on this command.

We have two more variations of grep – egrep and fgrep – and readers are advised to refer to their man page to get more details.
look – This command works like grep, but searches for a match in /usr/dict/words by default.

There are various text-processing commands that affect text and text files. Popular ones are:
sort – This is a file-sorting utility and normally used as a filter in pipes.
uniq – This also acts as a filter and removes duplicate lines from a sorted file.
expand – This command converts tabs into spaces.
cut – This is a very useful utility that is used for extracting fields from a file.
head – This lists the beginning of a file to stdout. The default value is first ten lines, but we can specify a different number.
tail – Displays the end of the file. This command is very helpful while monitoring the log files. The -f option outputs lines appended to the file.

Let us look at a couple of utilities that can be used with a file.
basename – This strips the path information from a filename, printing only the filename.
[sourcecode language=”bash”]cvs@cn-setta-2:~> basename –help
Usage: basename NAME [SUFFIX]
or:  basename OPTION
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.
–help     display this help and exit
–version  output version information and exit
basename /usr/bin/sort       Output “sort”.
basename include/stdio.h .h  Output “stdio”.
Report bugs to <>.
split, csplit – These utilities help us in splitting a file into smaller chunks. The csplit command splits a file according to the context.

So, in this part on shell scripting, we had a look at various operations that can be used to manipulate strings. We also looked at some of the popular job control commands and two of the most popular commands – find and grep. The file and text-processing commands are also widely used within shell script. Now it is time for you to go and use these concepts in your shell scripts so that you can start to understand and harness their power…

You can find the first part of our Shell Scripting for System Administrators article here.

Click here to return to the Linux User & Developer homepage

See what else featured in issue 92 of the magazine