Scripting for the Shell
- 0
- Add a Comment
Scripting for the Shell
We’ll close up the shell scripting series today with a look at the previous script, a summary of what we’ve seen, and some links to push you a bit further along the path to understanding the value of scripting for your favorite shell.
#!/usr/bin/perl
perl -pi.bak -e ’s/original-text/replacement-text/’ infile
As I’ve said, this is a pretty simple and straightforward script, intended to accomplish a single task. The heart of this script lies in the regular expression powers of Perl. This script looks for a string in a text file [original text] and replaces that string with new text [replacement-text]. But that’s not all. It creates a backup of the original file [-pi.bak] using the .bak extension. It’s a handy way to preserve different versions of changed documents.
So, let’s sum up a bit, here. Shell scripting can be as complex as programming or as simple as a single command. Generally, these scripts are written to a files, granted executable permissions and run when necessary. They’re at once elegant and powerful They allow a system administrator to execute complex command sets with a few keystrokes, to run maintenance tasks automatically with cron, or to eliminate the repetitive nature of many administrative tasks. Aside from all that, they’re quite easy to create. In the end, they’re fun and useful bits of code nearly unique to Linux.
This series really only showed some interesting and useful ways to script for the shell. Hopefully, it whetted your appetite to create your own and brought to your attention some situations on your own machine where a shell script would be useful. It’s nearly impossible to give them an in-depth examination within the constraints of Penguin Shell. However, that won’t stop me from providing some useful links for further exploration. Learning these concepts and putting them to good use in your system is Linux “sweat equity,” learning from the sweat of your own toils.
Some very useful shell scripting links, in no particular order:
