In many of the other sections of the shell and utilities, we talked about a few programming constructs that you could use to create a quick script to perform some complex task. What if you wanted to repeat that task with different parameters each time? One simple solution is to is to re-type everything each time. Obviously not a happy thing.
The solution is to create a shell script we can use over and over again, which is the topic of this section. There are entire books on shell programming and to understand everything you probably need more material that any single book can provide. Rather than going through every possible shell constructs, we are going to cover more or less the basics. One of the things we will be using as a guide is how often a given construct appears in shell scripts that a standard Linux distribution provides by default and how important these constructs are to understanding the given script.
To create a shell script, we could use vi or some other text editor to create the file. However, we could take advantage of a characteristic of the cat command, which is normally used to output the contents of a file to the screen. You can also redirect the cat to another file.
If we wanted to combine the contents of a file, we could do something like this:
This would combine file1, file2, and file3 into newfile.
What happens if we leave the names of the source files out? In this instance, our command would look like this:
Now, cat will take its input from the default input file, stdin. We can now type in lines, one at a time. When we are done, we tell cat to close the file by sending it an end-of-file character, . So, to create the new command, we would issue the cat command as above and type in our command as the following:
The chmod command is used to not only change access to a file, but also to tell the system that it should try to execute the command. I said “try” because the system would read that file, line-by-line, and would try to execute each line. If we typed in some garbage in a shell script, the system would try to execute each line and would probably report “not found” for every line.
To make a file execute properly, we need to give it execute permissions. To give everyone execution permissions, you use the chmod command like this:
Now the file newfile has execute permissions, so, in a sense, it is executable. However, remember that I said that with a shell scripts, the system would read each line and try to parse it. In order for a shell script to function correctly, it also needs to be readable by the person executing it. In order to read a file, you need to have read permission on that file. More than likely, you already have read permissions on the file since you created it. However, since we gave everyone execution permissions, let’s give them all read permissions as well, like this:
You now have a new command called newfile. This can be executed just like any the system provides for you. If that file resides in a directory somewhere in your path, all you need to do is type it in. Otherwise, (as we talked about before) you need to enter in the path as well. Keep in mind that the system does not need to be able to read binary programs. All it needs to be able to do is execute them. Now you have your first shell script and your first self-written Linux command.