If you are not a programmer, you may feel apprehensive about programming. But shell scripting (or programming) can be as simple as storing a few commands in a file. In fact, you can have a useful shell program that has a single command.
While writing this book, for example, I have captured screens from the X Window System and have used the screen shots in figures. I have used the X screen-capture program, xwd, to store the screen images in the X Window Dump (XWD) format. The book's production team, however, wanted the screen shots in TIFF format. Therefore, I used the Portable Bitmap (PBM) toolkit to convert the XWD images to TIFF format. To convert each file, I run two programs and delete a temporary file, by using the following commands:
xwdtopnm < file.xwd > file.pnm pnmtotiff < file.pnm > file.tif rm file.pnm
These commands assume that the xwdtopnm and pnmtotiff programs are in the /usr/bin directory-one of the directories listed in the PATH environment variable. By the way, xwdtopnm and pnmtotiff are two programs in the PBM toolkit.
After converting a few XWD files to TIFF format, I get tired of typing the same sequence of commands for each file, so I prepare a file named totif and saved the following lines in it:
#!/bin/sh xwdtopnm < $1.xwd > $1.pnm pnmtotiff < $1.pnm > $1.tif rm $1.pnm
Then, I make the file executable by using this command:
chmod +x totif
The chmod command enables you to change the permission settings of a file. One of those settings determines whether the file is executable or not. The +x option means that you want to mark the file as executable. You need to do this because Bash runs only executable files. (See the chmod command reference in Appendix A for more information on permission settings for files.)
Finally, I convert the file figure1.xwd to figure1.tif by using the following command:
The ./ prefix indicates that the totif file is in the current directory-you don't need the ./ prefix if the PATH environment variable includes the current directory. The totif file is called a shell script or shell program. When you run this shell program with the command totif figure1, the shell substitutes figure1 for each occurrence of $1.
That, in a nutshell, is why you might create shell programs-to have your Linux shell perform repetitive chores.
Here is another interesting example of a shell program. Suppose that you occasionally have to use MS-DOS text files on your Linux system. Although you might expect to use a text file on any system without any problems, there is one catch: DOS uses a carriage return followed by a line feed to mark the end of each line, whereas Linux (and other UNIX systems) use only a line feed. As a result, if you use the vi editor with the -b option to open a DOS text file (for example, type vi -b filename to open the file), you see ^M at the end of each line. That ^M stands for Ctrl-M, which is the carriage-return character.
On your Linux system, you can easily rid the DOS text file of the extra carriage returns by using the tr command with the -d option. Essentially, to convert the DOS text file filename.dos to a Linux text file named filename.linux, type the following:
tr -d '\015' < filename.dos > filename.linux
In this command, '\015' denotes the ASCII code for the carriage-return character in octal notation. In this command, the < symbol is used to read from a file and > is used to save output to a file.
You can use the tr command to translate or delete characters from the input. When you use tr with the -d option, it deletes all occurrences of a specific character from the input data. Following the -d option, you must specify the character to be deleted. Like many UNIX utilities, tr reads the standard input and writes its output to standard output. As the sample command shows, you must employ input and output redirection to use tr to delete all occurrences of a character in a file and save the output in another file.
If you don't want to remember all this information every time you convert a DOS file to UNIX, store the following in a file named dos2unix:
tr -d '\015' < $1 > $2
Then, make the file executable by using this command:
chmod +x dos2unix
That's it! Now you have a shell program named dos2unix that converts a DOS text file to a UNIX text file. If you have the MS-DOS partition mounted as /dosc, you can try the dos2unix shell program with the following command:
dos2unix /dosc/autoexec.bat aexec.bat
The preceding command creates a file named aexec.bat in the current directory. If you open this file with the vi -b aexec.bat command, you should not see any ^M characters at the ends of lines.
If you are familiar with MS-DOS, you may notice that shell scripts closely resemble MS-DOS batch files, except for some syntax differences. Shell scripts, however, are much more powerful.