Script Basics

 < Day Day Up > 



As we have already mentioned, if you have a series of commands you want to automate, the most efficient way to do that is via a script. Scripts can be written in any basic text editor. You can use Kate, KWrite, or any other text editor that you want. The only problem to keep in mind when writing a script is that scripts are generally written for a particular shell (bash, C shell, Korn, etc.). For this reason, all the shells have an agreed upon standard that the first line of a script declares what type of shell it runs in. Then that shell will be used to run the script. For example, if you want a script run by the C shell, your first line would be this:

  #!/bin/chsh 

If you want your script to run in bash:

  #! /bin/sh
Note 

Keep in mind that any line, other than the first line of the script, that has a # is considered a comment and is ignored. In case you are unfamiliar with this use of the word comments, they are used by programmers to leave notes for any other programmer who might look at their code. A comment is not executed and has no effect on the workings of the program or script. It is just an explanatory note for other programmers.

What this first line of the script is saying is that the subsequent lines should be executed with the designated shell. The ! symbol, in this case, means to execute, so this line literally says execute the following lines in the designated shell.

It might be helpful to see a few scripts in action. Let’s take a look at a couple of simple scripts. While these scripts are quite simple, they illustrate the basics of script writing and should help you begin to get comfortable with the concept of scripts. You can use any text editor you like. When you are done typing your script, read through it carefully for spelling errors or typos. Beginners often miss basic errors that will cause a script to behave in unpredicted ways or not run at all. Here is a sample script, seen in Figure 20.3:

click to expand
Figure 20.3: Saving your script file.

  #!/bin/sh   hostname   who    exit 0

If you save this with the name scriptone.sh, you can then go to the shell, change to whatever directory you saved your script to, and run the ls command. You should see scriptone.sh in green print among the other contents of that directory, as you see in Figure 20.4. You can now enter bash scriptone.sh and see output similar to what is shown in Figure 20.5.

click to expand
Figure 20.4: Script files in green print.

click to expand
Figure 20.5: Running script one.

Notice the last line of the script. It is not one of the shell commands we have previously discussed. This command tells a script that it is done and to exit. If you omit this line, your script may not run at all; if it does run, it will have unpredictable results.

You might also be wondering why the name of your script was in green. This indicates an executable script. All such scripts appear in green, just as all directories appear in blue, and tar files in red. In older versions of Linux a script file was not immediately executable. You had to first use the chmod command (an abbreviation for change mode) to change the file’s mode to an executable one. For example, after saving your script you would have had to enter the following at the shell:

  chmod 777 scriptone.sh

If you are using an older version of Linux you may still have to do this. If you try typing in the name of your script and an error occurs, try using chmod, as shown above, to change the mode to an executable script. That was not too difficult, was it?

Let’s try one more script. In this script we will introduce some interesting new additions to your repertoire of shell commands. As you know, you can run an ls command and list everything in a directory. However, if you use the command prompt in Windows very much, you might be wondering if you can filter the results. In Windows, for example, you could enter dir *.exe and get only files ending with an .exe extension. Can you do the same thing in Linux? Yes, you certainly can. In fact, the asterisk wildcard works the same way. There is also another wildcard character you can use, the question mark. A question mark indicates a single character. So for example if you type ls *.txt at the shell, you will see all text files. If you enter ?.txt, you will see only text files with a one-character name. There are several wildcards, and we will use them in our next script. Several of the commonly used wildcards are summarized in Table 20.1.

Table 20.1: Wildcards

Wildcard

Purpose

*

This means any character and any number of characters. For example, *.sh means any shell, any file that ends in the .sh extension, regardless of how many characters are in the name.

?

This denotes one character. For example, ????.txt means all text files that have a four-character name.

!

This means not, or negation.

[abc]

This means containing any of the characters listed. [abc]???.txt means all text files that start with an a, b, or c followed by three characters.

[a-m]

This means any character in the range shown. [a-l]??.txt means any file starting with some letter from a to e, followed by two characters.

Let’s put some of these wildcards you just learned to good use and put them in a script. Open your favorite text editor and enter the following text:

  #!/bin/sh   ls *.txt   ls [a-m]???.txt   ls ??.txt   exit 0

Now save this script in the same manner as you did the previous script. Make sure the file type is set to All Files and save it with the name scripttwo.sh. Then from the shell enter bash scripttwo.sh. You should see a display similar to the one shown in Figure 20.6. Obviously, your display will depend entirely on what text files are in your directory. If you like, you can save blank documents with various names in this directory, just so you can test the script.

click to expand
Figure 20.6: Running script two.

Let’s examine this script in some detail to make certain you understand it completely. The first line of the script is the same as in the first script we examined. In fact, all of the scripts we work with in this book will begin in this manner. What we see next are three separate calls to the ls command. The first uses the asterisk wildcard to ask for all files, regardless of their name, that end in .txt. This will display all text files in that directory. The second line says to display all files that begin with some letter from a to m, followed by three more letters and ending in a .txt extension. The third line asks for all text files whose names have only two letters. Finally, we come to the exit 0 line, which instructs the script to exit.

This second example shows two things. First, it should demonstrate that writing scripts is not particularly hard. Make certain that you always check your spelling and end your script with the exit 0, and everything should be fine. The second thing this script illustrates is the enormous flexibility you have with wildcards.



 < 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

Similar book on Amazon

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