Advanced Shell Scripting

 < Day Day Up > 

In the preceding chapter we looked at shell scripting. If you will recall, we examined the basics of scripting as well as how to write some basic programming techniques in a script. After studying that chapter, you should be basically competent at writing simple shell scripts in Linux. If you will recall, we mentioned that entire books have been written on scripting. That means that a single chapter touched the basics. This chapter certainly won’t make you an expert at writing shell scripts. However, we should take a moment to examine a few items that you might find useful in script writing.

case Statements

We looked at if statements when we initially examined shell scripts. if statements work for many situations where you need to make some sort of decision in the execution of your script. However, what happens when you have a series of decisions to make? Do you simply write a series of five, six, or more if statements? Yes that works, but it is certainly not an elegant choice. This is where case statements come in. A case statement allows you take one of many choices.

The basic format of a case statement is that you put the word case, followed by the variable, then the word in. After that you list the choices. Each choice is followed by a parenthesis and two semicolons. The entire case statement ends with the keyword esac. Here is a generic example:

    case somevariable in       choice one);;       choice two);;       choice three);;     esac

Let’s look at an example script:

    #!/bin/bash     echo 1. Windows      echo 2. Macintosh     echo 3. Linux     echo 4. OS2     echo Please choose your operating system     read opsys     case $opsys in        1) echo What? Haven’t you been reading this book?;;        2) echo Good grief…;;;;        3) echo Alright! Now you got it!;;        4) echo What planet are you on?;;     esac     exit 0

If you type this into your favorite text editor, you can save it as, then run it from the shell. If you run it from the shell, you should see something like what is shown in Figure 21.9.

click to expand
Figure 21.9: Using case statements.

This may look a little complicated, and there are several rules to remember. Each choice must end with a double semicolon, the entire case statement must end with esac, and each choice is followed by a parenthesis. This may seem like a lot to remember, but with a little practice it will become very natural. This is also another technique that is common to most programming languages.

Multiple Statements on One Line

Sometimes in your script you might have several related statements to write. You might want to put them all on one line. You could do this either to save space or to group commands that belong together logically. For whatever reason, doing it is simple. You separate each command on the line with a single semicolon, as you see here:

   statement one; statement two; statement three

Open your favorite text editor and enter the following. Then save it as and run it.

   #!/bin/bash    ls; ps;who;logname;    exit 0

When you run this, it will work just as if you had placed each command on a separate line. You can see the results in Figure 21.10.

click to expand
Figure 21.10: Multiple commands.

Whether or not to place your commands on a single line or space them out is a matter of preference. Linux scripts allow you to do either. Let’s take another look at putting multiple commands on a single line. What we will do is to put echo statements in the previous script to explain to the user what we are doing. Your script should look something like this:

   #!/bin/bash    echo running ls; ls; echo running ps; ps; echo running who; who;           echo running logname; logname;    exit 0

If you run your script now, you will see something like what is shown in Figure 21.11.

click to expand
Figure 21.11: Echoing with multiple commands.

 < Day Day Up > 

Moving From Windows to Linux
Moving From Windows To Linux (Charles River Media Networking/Security)
ISBN: 1584502800
EAN: 2147483647
Year: 2004
Pages: 247
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: