Tuesday, April 2, 2013

Shell Scripting 03: Shell script to find the factorial of a number

With what we have learned in the last two posts, lets have a look at a script to find the factorial of number.

The following script is written with a view to introduce shell as a programming language.



The above script accepts an integer parameter. It then calculates its factorial and print it.

In the very first line you could find declare command. The declare command permit restricting the properties of variables. -i option will treat the variables as integers.

In line 10, you could find a test command apart from [ ]. what ever follows after test is the condition. In short test and [ ] have the same purpose.

In the else part you could find expr command. expr command evaluates its arguments as expressions. Here we use expr command to check whether the the parameter passed to the script is an integer or not. You can learn more on expr command here.

I hope you enjoyed this one.
Feel free to ask doubts. 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes