Chapter 20: Basic Shell Scripting

 < Day Day Up > 



Introduction

The previous chapter discussed administering your computer via shell commands. You will find that frequently multiple commands are used together to accomplish some tasks. For example, if you really want to get a picture of your computer’s performance, you might use several different shell commands to get a complete view of your system. Normally, shells are interactive. This means that a shell will accept a command from you via your keyboard and execute that command. If you use certain commands on a routine basis, it can be tedious to retype the same series of commands each time you need them. You can store this sequence of commands to a text file and instruct the shell to execute this text file instead of entering the commands. This is what is known as a shell script.

Windows has something quite similar to this, although it is not used much anymore. In Windows you can use a standard text editor to create what is called a batch file and execute that file from the command prompt. Batch files were very popular when DOS and Windows 3.1 were the current operating systems, but they have fallen out of use in recent years. So that you understand the concept, let’s examine how to write a simple batch file in Windows.

The first step is to open Notepad and enter the following lines:

 cd\windows   dir *.log  cd\

These are the commands to change directories to the windows directory, list all files that end with .log, and then change back to the root directory (c:\). Then choose File and Save As, and save the file to the root directory with the name test.bat. Make certain that you change the file type to All Files, rather than Text, as you see in Figure 20.1.

click to expand
Figure 20.1: Saving your batch file.

Now open a command prompt and change to the c:\ directory (you can do this by typing in cd\ and pressing the Enter key). Then enter test.bat and then press the Enter key. You should see something very much like what is shown in Figure 20.2.

click to expand
Figure 20.2: Running the batch file.

You can see that in essence a batch file is simply a way to execute a number of commands by typing in only a single command at the prompt. Batch files are sometimes used if there is a series of commands that is routinely executed in sequence. The Linux script performs just about the same purpose as the batch file, except that Linux scripts have more versatility and can be used in much the same manner as many standard programming languages.

Shell scripts, in essence, are miniature programs. They can do many of the things that traditional programming languages such as C, C++, BASIC, and others do. This makes them very powerful and versatile. There are entire books written just on shell scripting, and that should give you some idea of how deep this topic can be. In this chapter we will be exploring only the basics of writing shell scripts. If you are planning to use your Linux machine for a server, perhaps for a small office, or if you want to delve deeper into the world of Linux, then you will want to study this chapter carefully. However, if you are a casual Linux user who just wants to surf the Internet and use the Office tools, you should feel free to skip this chapter.



 < 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

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