Added a section on functions in ULT; minor edits to SciTE section.
authorPuneeth Chaganti <puneeth@fossee.in>
Fri, 18 Sep 2009 14:46:54 +0530
changeset 59 8c15077f028d
parent 58 88ab79104a62
child 60 8a36825e21c5
Added a section on functions in ULT; minor edits to SciTE section.
ult/Section_5.rst
ult/session4.rst
--- a/ult/Section_5.rst	Thu Sep 17 23:58:10 2009 +0530
+++ b/ult/Section_5.rst	Fri Sep 18 14:46:54 2009 +0530
@@ -607,6 +607,56 @@
     echo "True"
   done
 
+Functions
+---------
+
+When a group of commands are repeatedly being used within a script, it is convenient to group them as a function. This saves a lot of time and you can avoid retyping the code again and again. Also, it will help you maintain your code easily. Let's see how we can define a simple function, ``hello-world``. Functions can be defined in bash, either using the ``function`` built-in followed by the function name or just the function name followed by a pair of parentheses. 
+::
+
+  function hello-world
+  { 
+  echo "Hello, World."; 
+  }
+
+  hello-world () {
+    echo "Hello, World.";
+  }
+
+  $ hello-world
+  Hello, World.
+
+Passing parameters to functions is similar to passing them to scripts. 
+::
+
+  function hello-name
+  { 
+  echo "Hello, $1."; 
+  }
+
+  $ hello-name 9
+  Hello, 9.
+
+Any variables that you define within a function, will be added to the global namespace. If you wish to define variables that are restricted to the scope of the function, define a variable using the ``local`` built-in command of bash.
+
+We shall now write a function for the word frequency generating script that we had looked at in the previous session. 
+
+::
+
+  function word_frequency {
+    if [ $# -ne 1 ]
+    then
+      echo "Usage: $0 file_name"
+      exit 1
+    else 
+      if [ -f "$1" ]
+      then
+        grep  "[A-Za-z]*" -o "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | less
+      fi
+    fi
+  }
+
+As an exercise, modify the function to accept the input for the number of top frequency words to be shown (if none is given, assume 10).
+
 
 Further Reading:
 ---------------- 
--- a/ult/session4.rst	Thu Sep 17 23:58:10 2009 +0530
+++ b/ult/session4.rst	Fri Sep 18 14:46:54 2009 +0530
@@ -428,7 +428,7 @@
 SciTE
 -----
 
-SciTE is a *source code* editor, that has a feel similar to the GUI text editors. It has a wide range of features that are extremely useful for a programmer, editing code. Also it aims to keep configuration simple, and the user needs to edit a text file to configure SciTE to his/her liking. 
+SciTE is a *source code* editor, that has a feel similar to the commonly used GUI text editors. It has a wide range of features that are extremely useful for a programmer, editing code. Also it aims to keep configuration simple, and the user needs to edit a text file to configure SciTE to his/her liking. 
 
 Opening, Saving, Editing files with SciTE is extremely simple and trivial. Knowledge of using a text editor will suffice.