Example of recursion in a shell script
For those who are new to shell scripting and are looking for a quick example of recursion in shell scripting, here you go.
Problem: Count the number of directories in a given dir/file.
Algorithm:
1. Check if the current input argument is a dir. If not exit.
2. Write a recursive fn to which the current dir is passed as an argument.
3. List the contents of the current dir.
4. In a loop check if the content of the dir is a dir in itself or a file.
5. If it is a file, skip and return to the next listing.
6. If not, initialize an array to store the list of dirs at that leve and pass that sub-dir as current dir and keep going till you reach a dir which has no sub-directories. This procedure gets repeated till all sub-dirs within the current dir are parsed.
7. When there is no more sub-dir, increment global counter by 1 and return the counter value.
Here it is.
#!/bin/ksh
if [ ! -d $1 ]
then
echo “# of dir=0″
exit
fi
function recurse
{
list=`ls $1`
for x in $list
do
cur=$1
if [ -d $cur\/$x ]
then
i=`expr $i + 1`
a[$i]=$cur\/$x
recurse ${a[$i]}
fi
done
k=`expr $k + 1`
return $k
}
recurse $1
echo “# of dirs=$k”
Scientific study of Luck
Anything humans can think of is ultimately based on something that has a physical existence or can be sensed. It includes imaginary subjects or emotions or concepts. Consider something imaginary say Unicorn. The horse exists physically and so does a single horn but not a horse with a horn. The same applies to things that don’t exist physically also. Let us take the concept of God — though not known to exist physically, we feel it in the natural course like death, joy, pain etc. Think hard enough & you will realise that all our thoughts are extensions of physical objects or specific senses. Attempts have been made to explain more abstract concepts like psychology or love using science. Even things that are very complex like planetary motions are being predicted precisely using science. Even emotional/sensory pleasures like music (progressions) and beauty (symmetry and proportions) have been explained using mathematics. A very common sense/concept which is wildly popular in day-day life but not been studied systematically in science is Luck. Yes, there is the occassional mathematician/psychologist who studies it but they are far and few. I’d define luck mathematically as “A condition in which a least probable event (probability ~0) has a higher chance of occuring (probability ~1)” . Of course, it is assumed that lame duck excuses will not be considered as most of the time people willingly cover up their mistakes as luck. Typically things good or bad (depending on how it starts) happen continuously. Take any such case and work out the math and you can understand why I have defined it in this way. Luck in linguistics is associated with fortune, superstition and destiny. Why would the study of luck be beneficial? It has implications in increasing productivity as well as emtional well being. Who wouldn’t want to start a venture on a lucky day or propose marriage?
P.S. If I am “lucky”, some math genius will get inspired by this blog and crack the mystery
!!
Universal Truths
Universal Truths are statements which are true in all cases. For example, a birth ensures that there is a death. i.e any person who is born has to die. This is true for people of all religions, race, colour, caste, creed and nationality. So is the fact that fire produces heat and ice is cold. These facts are obvious and no one disputes their nature though their usefulness differs.
We are able to recognize truths intuitively if they are true under all circumstances. However, if there is something that is not true for all cases, disputes on its nature as truth arises. Is tax good for everyone? The opinion differs. Because it depends on the person.
So is religion/customs/culture etc. Every religion/sect claims that it is superior to others and those who don’t agree are sinners. However, despite such stern warnings, people disagree! If any particular discriminatory classification was true under all circumstances, we’d be able to realize it intuitively! By virtue of the fact that not everyone (for the sake of this argument, we’ll exclude folks considered insane) agrees to a statement, we can deduce that it is not true.
Religion masks this very cleverly by suggesting that truth is not obvious and is hidden behind a veil of falsities. In my opinion Universal truths would be obvious and since there is always disagreement regarding the superiority of a particular religion/culture/nation/sect over the rest there is no such thing as a superior religion/culture/nation/sect in every possible way!