Lesson 14. Shell Scripting FundamentalsIn 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.
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.
|