Lesson14.Shell Scripting Fundamentals


Lesson 14. Shell Scripting Fundamentals

In this lesson, you will learn about the shell and the process of shell scripting to help you automate tasks.

In this chapter, we will cover shell scripting, and really expose some of Unix's true power. We have mentioned shell scripting often in this book, touching upon the subject in almost every lesson and now we finally discuss it. As you may expect, the first lesson in the book could not be on scripting, as you wouldn't know how to log in, what commands you could script with, and all the little odds and ends that are required to build a script. You now have a better understanding, however, as we just had three lessons covering processes, input and output (I/O) control, and regular expressions (REs). Now we tie everything together, and present you with a lesson on the fundamentals of shell scripting.

If you have never worked with a computer before, and this is your first official exposure to scripting, don't panic. It's easily broken down by answering the most common questions.

What is a shell? A shell is nothing more than a command interpreter. In Unix, the shell is designed to be the interface to the user, to receive the user's input (interpret commands), and act on them. The shell then sends the output to the default, or a specified location, usually the user's terminal. There are some common Unix shells: the C shell, the Bourne shell, and the Korn shell, all of which were covered earlier in the book.

What is a script? A script is another name for a macro or batch file, which does nothing other than execute what you add to it. If a script is a file, then the contents of the script are essentially what you want to build with Unix commands. That being said, it should make sense that a script is basically a file with a list of commands that can be executed without user interaction. A script language is a simple programming language with which you can write scripts. We covered Perl in the last lesson; this is a commonly used scripting language in Unix environments, and Python is another. You can also develop complex shell scripts right from the shell.

What is a shell script? Well, if you now know what a shell is, and what a script is, then it should make sense to you that a shell script is nothing more than a script that you create using commands from your current shell (bash, csh, and so on) that the shell in use can interpret. For instance, a shell script written in one shell may not operate properly in a different shell as a different syntax may be used. In any case, you can check your local system's man pages for help if needed.

What is a shell script primarily used for? Quite simply, (most) scripts are used for automation of tasks. Who wants to type hundreds of lines of text into the shell prompt? No, didn't think you wanted to do that, but what if you put all that into a script (one file) and had that run? Quite possibly with at or cron? The whole picture should be clearing up now.

What is an example of a script in action? A simple shell script in action is nothing more than picking the shell you are currently working with (I am going to use bash for this example), and knowing what commands you can enter into it. To create a script, you need to know what commands you will enter into it, and what it is going to do beforehand. This way, you don't waste your time writing in your editor or at the shell prompt. If you have a clear picture defined beforehand, then this will save you time and frustration.

Again, a shell script is little more than a list of commands that are run in sequence. Study the man pages to those commands and get all the syntax laid out ahead of time before doing it on the Unix system, especially if it is not a test system and one you use for work or at work.

Conventionally, a shell script should start with a line such as the following:

 #!/bin/bash 

This commonly used line will indicate that the script should run in the bash shell regardless of which interactive shell the user has selected. It's important that your commands match the shell or you may see a great many error messages, or quite simply, your script will not work properly. This is very important since the syntax of different shells will almost always vary.

Now, after you set up the script with the correct indicator of what shell to use, enter some commands.

 #!/bin/bash echo "Hi this is Unix speaking, I know who you are, you are $USER" echo "Your current directory is also known, it happens to be, $PWD" ls  # note script used to check user, directory and list files 

Whoa, what do we have here? Well, without panicking (this is common to do when you see something like this) pick it apart, piece by piece.

The first line sets the shell. The second line uses the $USER variable to echo back your current username (in this case I happen to be logged in as root). The third line is identical to the second except you see the $PWD variable in use; this tells you your current directory location. The fourth line tells Unix to list the contents of the current directory. Doing each of these commands one at a time will simulate the script executing one line at a time. This can also all be placed in one file (master emacs or vi) and executed as a script file by a user or another program like at or cron.

Remember, this script should be populated with commands that bash can handle and process; therefore, this is why you may not have noticed some of these commands or they may not be available on your version of Unix. This is why it is important to master the basics so you can do scripting in the first place. As you can see from our example, some comfort with your shell is needed.

Keep going over this example until you understand it completely. It is important to learn how to do scripts if you are going to be a Unix administrator. For end users, you may never need to create a script, but you may consider that your current employer expects you to come in every day and run a certain amount of jobs. Remember the jobs command? That could be added to a script and automated with at or cron. This is your call, but scripting is something I would learn in more depth. Scripting is a powerful aspect of Unix, and is one of the things that separates the men and women from the mice.

What Is All This Gibberish? No, don't bash your head into a wall to try to understand this; it happens to be specific to bash and not Unix in general. That's why we talked about shells in the beginning of the book; it is important that you know that they are different. When a shell is chosen, or given to you by default, using scripts from one shell to another can be problematic. In the bash shell script example just covered, the last line in the script had a number sign (#) after the ls command. If you ran the script (and the syntax was entered correctly), the # reflects what is known as a comment. If you are an experienced Microsoft Windows user, you may have had to edit a file and you saw the # used to make a comment.

Comments are common when programming or scripting because you sometimes need to leave yourself a note as to what a line of the script of the code does. In this case, I made it very simple. The ls command is used to list files, and the comment specified that. The comment will be seen by Unix, but not repeated on the screen like the echo command performed.

Other items that may be foreign to you are the $USER and $PWD elements. These are environment variables, which respectively represent what user is currently logged in and executing the script and tell you what directory you may be in, such as /etc or /, for example. These variables produce output similar to the whoami and pwd commands.


If you have absorbed everything up to now, you will be writing scripts in no time at all. There is just not enough space here to cover all the little nuances between shells, and to cover everything you can do with scripting would take a lifetime. In any case, you should now feel comfortable with what a shell script is, understand all the terminology revolving around it, and how to define one in your shell of choice.

Have You Seen This Before? You may be wondering where you have seen this before. Scripting is common in MS-DOS.

If you are familiar with MS-DOS, then you will notice that shell scripts are similar to batch files. If you are familiar with *.bat files, then you know what I mean.




    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net