Browse Resources

Placement Partners

  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
  • JoomlaWorks Simple Image Rotator
Variables & Constructs | Print |  Tell-a-friend

Till now in our scripting series, we have talked about the regular expressions, commands, and file indirection. Let us now take an analogy. If a script can be compared to a human body and scripting to life, what we have talked, till now, are sense organs (making life more meaningful and powerful), heart (driving the life), and hands (giving the movement), respectively. So, then what is the brain of scripting - the logic part? Yes, it is none other than its constructs. Commands are the heart but we need the constructs to control them. And to assist them, we have the memory components called variables to store various information . Unlike the normal programming world, in the scripting world, we do not have a concept of type to variables. They are just free flowing. You may assign anything to them and interpret them the way you want. This promotes ease of expression. For example, XYZ=5324, XYZ="5234", XYZ='5234', all mean the same thing. To access the value in the variable, we write ${}. So, to print the value in the variable XYZ, we type: echo ${XYZ}. echo is an in-built shell command, which means there is no external executable for echo, but is part of the shell executable. Contrast it with say 'ls' or 'cat'. We will be discussing more advanced topics like built-in commands, in our forthcoming articles.

Though, I said that all the 3 versions above are same, they do differ in some scenarios. The "" and '' versions are generally used when we have white spaces in the value. Moreover, '' is used when we want the value as is, and "" is used when we want the value to be evaluated before assigning. Examples: ABC='${XYZ}' vs ABC="${XYZ}", and then echo ${ABC} will print ${XYZ} and 5234, respectively. Please note that there is no space before and after =, and that is mandatory .Now you might be wondering after all what are these constructs is all about. To put in simple words these are just another jargon for the keywords and their semantics in a language, namely for loop, if condition, case statement, and various expressions.

Let us take a day-to-day example we face, to make all this comprehensible. I doubt if anyone has not faced a scenario where you want to rename a set of files with some pattern, into another set of files. To be more specific, say you downloaded lot of images from your camera after a visit to Germany, and they all are named as dsc001.jpg, dsc002.jpg, and so on. Now, it would have been so nice if they were rather named as germany001.jpg, germany002.jpg, and so on. But the innocent camera does not really know that you were in fact in Germany. And you might not think of wasting your time renaming each one of your hundreds of images from Germany. That is where scripting plays its role. The following for loop does the desired:

$ for old_file in dsc*.jpg > do > new_file=`echo ${old_file} | sed -e 's/^dsc/germany/'` > mv ${old_file} ${new_file} > done

Note that the '>' above is not something you type, it comes as a prompt if something is not complete while you are typing on shell. In the above example, for loop is not complete till we type done.

Let us analyze the above 'for' loop. In the above mentioned example the words 'for', 'in', 'do', 'done' are just the keywords, which needs to be placed as is, to form the for construct. Then, we pass the list of all files matching the regular expression "dsc*.jpg" to the for loop, as its last item. old_file is the variable, which would take one item at a time, from this list, in each of its iterations. So, in the first iteration, old_file will contain dsc001.jpg, in second dsc002.jpg, and so on. And every iteration, we compute the new name for the old file in the variable new_file, and then do a file rename from ${old_file} to ${new_file}, using the mv (move) command. Now, the core - how did we get new_file. We executed a sequence of shell commands inside ``, whose output is the value, we want in new_file, and then `` gets the output and = assigns it. About the command sequence, we gave the old file name as input by printing and piping it into the sed command, which replaces "dsc" (^ - from start of line) in the input by "germany" and puts back the transformed text on the output. sed is a powerful stream editor, which can do various text filtering and transformation operations. For the curious, refer the man pages (man sed). For the uninitiated, wait for the future articles on the advanced topics.

The for construct, we discussed above might look quite different from a general programming language, say C. In fact, it is, and that makes it more expressive. To make it work like "for (i = 0; i < 10; i += 1) { ... }", you may do either of the following:

$ for i in `seq 0 1 10` $ for ((i = 0; i < 10; i++)) > do > do > ... > ... > done > done

The 'seq' is a shell command, which generates a sequence / list of numbers in from the first to the last with increments of the second parameter. For decrement, put a negative number in the second parameters. And then a similar use of ``, as in the above case. The other 2 loop constructs are: 1) while ; do ; done 2) until ; do ; done. Refer to the man pages of bash (the shell), and search for "while", for details on how to use these.

Similarly, there the 2 main conditional constructs: 1) if ; then ; elif ; else ; fi 2) case in ) ;; ) ;; esac Use * as pattern for default

For more theoretical details, refer to "man bash". For practice and examples, look forward for our ongoing articles, where we would be using all these constructs. With this, we have covered the basics of scripting. Now, before we get into the advanced scripting topics, we need to take a glance at 'make' files, and how relevant is it with scripting. We will start our next article with that.