include("site.inc"); $template = new Page; $template->initCommon(); $template->displayHeader(); ?>
For newcomers to scripting, don’t worry. A script, in this case a shell script, is merely a text file with commands mostly the same as the commands you can type at the keyboard. I’ll point out the differences.
The following sections quickly introduce scripting for those new to this venture.
For your first venture, enter the following script into a text file:
rpm -qa | grep rpm
This script has a two-part command. The rpm –qa part queries all RPM packages, as covered in Chapter 4. The grep rpm part finds only packages with rpm in their names. This is a very simple script, but it can serve to show how to work with scripts.
Save this file under the name listrpmpkgs, since this script lists RPM packages.
Note
If you’re new to Linux, you’ll notice there’s no program named Notepad.exe. There are, though, a plethora of Linux text editors to choose from. See Appendix F for a listing of Linux text-editing tools.
Once you’ve entered a script, you can run it with the sh command, as shown following, passing the name of your script to the sh command:
$ sh listrpmpkgs
librpm404-devel-4.0.4-8x.27
librpm404-4.0.4-8x.27
rpm404-python-4.0.4-8x.27
rpm-4.1-1.06
rpm-devel-4.1-1.06
gnorpm-0.9-1
rpm-python-4.1-1.06
redhat-rpm-config-8.0-1
rpm-build-4.1-1.06
rpmrebuild-1.0-0
Type the command you have placed in your script at the command line. There should be no difference in the output. For example:
$ rpm -qa | grep rpm
librpm404-devel-4.0.4-8x.27
librpm404-4.0.4-8x.27
rpm404-python-4.0.4-8x.27
rpm-4.1-1.06
rpm-devel-4.1-1.06
gnorpm-0.9-1
rpm-python-4.1-1.06
redhat-rpm-config-8.0-1
rpm-build-4.1-1.06
rpmrebuild-1.0-0
The previous script example required the sh program, a Linux shell, to run the script. You also had to have the script file, such as listrpmpkgs, available. So, if you have stored the file in /home2/bin, to run the script, use the following command:
$ sh /home2/bin/listrpmpkgs
That’s not very convenient. Furthermore, you always have to remember where you stored the script file listrpmpkgs. To make this command work better, you can turn your script into a command.
To turn a script into a command, do three simple things:
1.Add a special magic comment to the start of the file so Linux recognizes your text file as a command script.
2.Change the permissions on the file so that it is marked as executable.
3.Copy the file to a directory located in your command path.
Shell scripts use a # to indicate a comment, text intended for human readers that can help explain the purpose of the script. By convention, Linux shells use a #! comment in the first line of a script file as a special marker that indicates the file is a shell script. The text that comes after the #! holds the name of the command that should be used to run the script. In almost all cases, that command should be /bin/sh for a shell script.
So edit the listrpmpkgs script again, and add the magic comment so that the file reads as follows:
#!/bin/sh
rpm -qa | grep rpm
Make sure the #! comment starts at the beginning of the first line.
Next, change the permissions on the script to mark it as an executable program. Use the chmod command to do this. The chmod command changes the file permissions. To see the permissions, run the ls –l command before changing the permissions:
$ ls -l listrpmpkgs
-rw-rw-r-- 1 ericfj ericfj 31 Nov 7 20:02 listrpmpkgs
The first set of characters, the -rw-rw-r--, indicate the permissions in three batches: permissions for the file owner, the owner’s group of users, and world (everyone else). The rw means read and write, and the r alone means read only for everyone not the owner and not in the owner’s group.
To add the permission to execute the file for the file owner only, use the following command:
$ chmod u+x listrpmpkgs
In this command, the u stands for the user who owns the file (for historical reasons, an o stands for others, not owner). The +x means add the x permission, short for execute permission.
After running this command, you can see the revised permissions.
$ ls -l listrpmpkgs
-rwxrw-r-- 1 ericfj ericfj 31 Nov 7 20:02 listrpmpkgs
Cross Reference
Use the man chmod command to see more information on this command.
You now have a command you can run locally. For example:
$ ./listrpmpkgs
librpm404-devel-4.0.4-8x.27
librpm404-4.0.4-8x.27
rpm404-python-4.0.4-8x.27
rpm-4.1-1.06
rpm-devel-4.1-1.06
gnorpm-0.9-1
rpm-python-4.1-1.06
redhat-rpm-config-8.0-1
rpm-build-4.1-1.06
rpmrebuild-1.0-0
The next step is to copy the file to a directory in your system command path. To see which directories are in your path, run the following command:
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/ericfj/bin:/usr/java/j2sdk1.4.0_01/bin
Pick one of these directories. The /usr/local/bin directory is a common place to share locally created commands. If this is a personal command for your own use only, a directory under your home directory will be better. In this example, the /home/ericfj/bin is one such directory.
Copy the script file to a directory in your command path, and you are ready to go.
Note
If you use the C shell, csh, or the T C shell, tcsh, you need to run the rehash command to tell the shell to look again at the set of commands available in your command path.
Enter the following command:
$ listrpmpkgs
librpm404-devel-4.0.4-8x.27
librpm404-4.0.4-8x.27
rpm404-python-4.0.4-8x.27
rpm-4.1-1.06
rpm-devel-4.1-1.06
gnorpm-0.9-1
rpm-python-4.1-1.06
redhat-rpm-config-8.0-1
rpm-build-4.1-1.06
rpmrebuild-1.0-0
You have now extended the Linux command set with your own command.
Note
Windows users may be used to the convention that program file names end in .exe and scripts end in .bat or .cmd. When you run these programs or scripts, you don’t include the extension, exe, .bat, or .cmd. With Linux and UNIX, though, the full file name is important, so if you name your script rpminfo.bat, you must type rpminfo.bat each time you run the script. That’s why most Linux programs and scripts have no filename extension.
If you want to share your script with others, you should give them the right to execute it as well. You can do that with the following command:
$ chmod a+x listrpmpkgs
In this case, the a stands for all users.
The listrpmpkgs script used so far isn’t very useful. It performs one command and that’s it. We cannot customize it without writing a new script.
One way to make a script more flexible is to allow it to use command-line options. Just like the rpm command accepts a zillion options, you can make your scripts accept options.
Shells define special variables for the command-line options passed to the shell. Table 15-2 lists these options.
Table 15-2: Shell variables for command-line options
Variable | Holds |
$0 | The name of the script itself, from the command line |
$1 | The first option |
$2 | The second option |
$3 | The third option |
$4 | The fourth option |
$5 | The fifth option |
$6 | The sixth option |
$7 | The seventh option |
$8 | The eighth option |
$9 | The ninth option |
$* | All command-line options |
$# | Holds the number of command-line options |
Note
Use $#argv in place of $# if you use the C shell to run your scripts.
You can use these variables to allow the user to pass the text to search for, instead of always searching for rpm. With this addition, your new script, renamed rpmgrep, follows in Listing 15-1:
Listing 15-1: rpmgrep
#!/bin/sh
rpm -qa | grep $*
This script now expects a command-line option that holds the text to search for. Mark this script as an executable; then you can run it as follows:
$ ./rpmgrep python
python-devel-2.2.1-17
gnome-python2-gtkhtml2-1.99.11-8
gnome-python2-canvas-1.99.11-8
gnome-python2-1.99.11-8
rpm404-python-4.0.4-8x.27
orbit-python-1.99.0-4
gnome-python2-bonobo-1.99.11-8
gnome-python2-gconf-1.99.11-8
libxslt-python-1.0.19-1
libxml2-python-2.4.23-1
python-optik-1.3-2
python-2.2.1-17
rpm-python-4.1-1.06
mod_python-3.0.0-10
python-tools-2.2.1-17
If you want to make this command available, copy it to a directory in your command path as described in the preceding section.