Shell Script Writing
(Page 1 of 4 )
Shell scripts are designed to automate tasks that you would otherwise need to do manually, one step after another. This allows you to build up a sequence and a structure of commands that will take these steps instead of you. For this, you can use the tools present in the repertoire of the UNIX shell and its control mechanism. Exactly how this will come together to make a complete system is the subject of today's article.
Now in the articles up to this point related to UNIX shell program/script writing, I have covered different aspects of how to perceive and use variables, commands and more. Moreover, I also managed to illustrate two of the most basic necessary control mechanisms in a programming language: the "if-then-else" and the loop structure.
If you missed this, I strongly recommend searching for the articles here on the site or under my name. Please skim through them. This article will be by no means enough for you to write a script on your own. Learning a script language is a little more time-consuming. You will need to comprehend more concepts and see more examples before you can write your own scripts.
The core of this article will introduce a couple of new syntaxes that will help you to further simplify the writing of scripts, and present a couple of examples of scripts using all of the data I have presented so far. In particular, I will present the (()) structure, work with the strings, and explain how to use the getopts and select command and a couple of other UNIX tools.
I will start with the operations with strings. Sometimes it is crucial to perform a couple of simple operations in a variable -- for example, to replace a portion from a string or to delete the start/end, fast and simple. In this case, you can use the ${} syntax coupled with the operators presented in the table below:
How to use it | What it does |
${variable#pattern} | If the pattern matches to the start of the value of the variable, then it will delete the shortest matching part and return that. |
${variable##pattern} | Like the syntax above, with the difference being that now the longest matching part will be deleted. |
${variable%pattern} | With the % operator, the pattern will have to occur at the start of the value of the variable. This will delete the shortest match |
${variable%%pattern} | This will delete the longest match and the end, therefore return this. |
${variable/pattern/replace_with} | This will replace a pattern matching the value of the variable with the replace_with string. If you start the pattern with the #, it will refer to the start of the variable. The % is equivalent to the end. |
For example:
alfa=Avril_Avril
echo ${alfa}
Avril_Avril
echo ${alfa#Avril}
_Avril
echo ${alfa%Avril}
Avril_
echo ${alfa/_/=}
Avril=Avril
Using the double operators ( ##, **) has its role only when you take advantage of the special characters * (any characters - none, one or more) and ? (any character - one). In these cases, there may be shorter or longer matching patterns, as the example below will show it (we refer to the same alfa variable as before):
echo ${alfa##A*r}
il
echo ${alfa%%r*l}
Av
Next: The (()) structure >>
More Miscellaneous Articles
More By Gabor Bernat