diff options
author | Puneeth Chaganti | 2009-09-18 14:46:54 +0530 |
---|---|---|
committer | Puneeth Chaganti | 2009-09-18 14:46:54 +0530 |
commit | 2a5c97fae7c0685ded1d36c53f0c09fcf1dea94e (patch) | |
tree | 898b1a1f12ff59cb34ea76106d3ebe212f9a06d6 /ult | |
parent | ac0d1430f21384d1ccffa484d62a6e24478855c3 (diff) | |
download | sees-2a5c97fae7c0685ded1d36c53f0c09fcf1dea94e.tar.gz sees-2a5c97fae7c0685ded1d36c53f0c09fcf1dea94e.tar.bz2 sees-2a5c97fae7c0685ded1d36c53f0c09fcf1dea94e.zip |
Added a section on functions in ULT; minor edits to SciTE section.
Diffstat (limited to 'ult')
-rw-r--r-- | ult/Section_5.rst | 50 | ||||
-rw-r--r-- | ult/session4.rst | 2 |
2 files changed, 51 insertions, 1 deletions
diff --git a/ult/Section_5.rst b/ult/Section_5.rst index 81d7c68..df10ba8 100644 --- a/ult/Section_5.rst +++ b/ult/Section_5.rst @@ -607,6 +607,56 @@ The infinite loop changes to the following, when ``until`` is used. 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: ---------------- diff --git a/ult/session4.rst b/ult/session4.rst index f0b61cf..c7f8bcd 100644 --- a/ult/session4.rst +++ b/ult/session4.rst @@ -428,7 +428,7 @@ Searching and Replacing 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. |