14.4.4. Turning a 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.