Difference between revisions of "BASH/C3/Arrays-and-functions/English"
(Created page with ''''Title of script: Arrays & functions''' '''Author:''' Lavitha Pereira '''Keywords: Video tutorial, Bash shell, function, function call''' {| style="border-spacing:0;" | sty…') |
Nancyvarkey (Talk | contribs) |
||
Line 61: | Line 61: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>#!/usr/bin/env bash </nowiki>''' | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>#!/usr/bin/env bash </nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is the '''shebang line.''' |
|- | |- | ||
Line 81: | Line 81: | ||
<nowiki>#Takes array name as variable </nowiki> | <nowiki>#Takes array name as variable </nowiki> | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The use of | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The use of '''''Dollar At-sign''''' was explained in previous tutorials in this series. |
Line 93: | Line 93: | ||
<nowiki>#displays all element of an array </nowiki> | <nowiki>#displays all element of an array </nowiki> | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Dollar opening curly | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Dollar opening curly brace array within square bracket At sign closing curly brace''' |
Line 106: | Line 106: | ||
'''}''' | '''}''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Dollar opening curly | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Dollar opening curly brace array within square bracket one closing curly brace''' |
Line 119: | Line 119: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>array_display ${operating_systems[@]}</nowiki> | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>array_display ${operating_systems[@]}</nowiki> | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here, | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here, '''array 'operating_systems'''' is passed to the '''function 'array_display'.''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 133: | Line 129: | ||
<nowiki>function_name ${array_name[@]} </nowiki> | <nowiki>function_name ${array_name[@]} </nowiki> | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The syntax is: | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The syntax to pass an '''array''' to a '''function''' is: |
− | ''' | + | '''function_name space dollar opening curly brace array_name within square bracket 'At sign' closing curly brace''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Come back to our program. |
|- | |- | ||
Line 148: | Line 144: | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| ''''colors' '''is declared with the elements | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Similarly, ''''colors' '''is declared with the '''elements''' |
'''White, green, red '''and''' blue''' | '''White, green, red '''and''' blue''' | ||
− | Here, | + | Here, '''array 'colors' '''is passed to the '''function 'array_display'.''' |
|- | |- | ||
Line 236: | Line 232: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight '''{''' | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight '''{''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Open curly | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Open curly brace opens the '''function definition.''' |
|- | |- | ||
Line 261: | Line 257: | ||
echo "This will not appear" | echo "This will not appear" | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that the statements after '''return''' will not be executed in a function. | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that the statements after '''return''' will not be executed in a '''function'''. |
|- | |- | ||
Line 267: | Line 263: | ||
} | } | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''fi''' indicates the end of '''if | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''fi''' indicates the end of '''if statement''' |
|- | |- | ||
Line 273: | Line 269: | ||
{ | { | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here the '''function '''name ''' | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here the '''function '''name is''' exit_function ''' |
|- | |- | ||
Line 286: | Line 282: | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''echo statement '''displays the message | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''echo statement '''displays the message '''"This is exit function"''' |
|- | |- | ||
Line 296: | Line 292: | ||
} | } | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''fi''' indicates the end of this '''if | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''fi''' indicates the end of this '''if statement.''' |
|- | |- | ||
Line 304: | Line 300: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''echo "We are in main program" ''' | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''echo "We are in main program" ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This displays the message '''"We are in main program"''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''exit_function 3 3 ''' | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''exit_function 3 3 ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This is another '''function call''' with '''arguments 3 '''and '''3.''' |
|- | |- | ||
Line 314: | Line 310: | ||
'''echo "This line is not displayed"''' | '''echo "This line is not displayed"''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''echo statement ''' | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''echo statement '''displays the message '''"This line is not displayed"''' |
|- | |- | ||
Line 361: | Line 357: | ||
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Workflow of Program:''' | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Workflow of Program:''' | ||
− | ''<nowiki>[ | + | ''<nowiki>[Highlight program and explain]</nowiki>'' |
− | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| # The control will be in main program, which is the script itself. | + | | style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| |
+ | # The control will be in main program, which is the script itself. | ||
# The control goes to ''''return_function'''' because of the '''function call.''' | # The control goes to ''''return_function'''' because of the '''function call.''' | ||
− | # As the two variables are equal, it displays the message | + | # As the two variables are equal, it displays the message '''"This is return function"''' |
− | # Then it encounters''' return 0. '''And | + | # Then it encounters''' return 0. '''And the control flows from '''function '''to the statement below the '''function call, '''in the main program. |
− | # Then it displays the message | + | # Then it displays the message '''"We are in main program"''' |
− | # After that, the control goes to ' '''exit_function'''' because of the function call. | + | # After that, the control goes to ''''exit_function'''' because of the '''function call'''. |
− | # As the two variables are equal, it displays the message | + | # As the two variables are equal, it displays the message '''"This is exit function"''' |
# Then it encounters''' exit 0'''. This will terminate the program. | # Then it encounters''' exit 0'''. This will terminate the program. | ||
# Any statement after '''exit '''will not be executed. | # Any statement after '''exit '''will not be executed. | ||
# Also, the statement: | # Also, the statement: | ||
− | + | '''"This line is not displayed"''' ` | |
will not be executed. | will not be executed. | ||
Line 391: | Line 388: | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial, we learnt | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial, we learnt | ||
− | * To pass an array to a '''function ''' | + | * To pass an '''array''' to a '''function ''' |
− | * Use of''' exit''' in a function | + | * Use of''' exit''' in a '''function''' |
− | * Use of '''return''' in a function | + | * Use of '''return''' in a '''function''' |
* With the help of some examples | * With the help of some examples | ||
− | |||
− | |||
|- | |- | ||
Line 406: | Line 401: | ||
Write a program, | Write a program, | ||
− | * Where a '''function '''adds all the elements in an array | + | * Where a '''function '''adds all the elements in an '''array''' |
* The '''function''' should display the sum of elements. | * The '''function''' should display the sum of elements. | ||
− | * Make 2 '''function''' '''calls''' with array elements (1, 2, 3) and (4, 5, 6) | + | * Make 2 '''function''' '''calls''' with '''array''' elements (1, 2, 3) and (4, 5, 6) |
Line 458: | Line 453: | ||
− | This is Ashwini Patil from IIT Bombay signing off | + | This is Ashwini Patil from IIT Bombay signing off. |
Thank you for joining. | Thank you for joining. | ||
|} | |} |
Latest revision as of 08:30, 26 March 2014
Title of script: Arrays & functions
Author: Lavitha Pereira
Keywords: Video tutorial, Bash shell, function, function call
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on Arrays & functions |
Display Slide 2 | In this tutorial, we will learn
|
Display Slide 3Prerequisites
|
To follow this tutorial you should have knowledge of Shell Scripting.
arrays and if statement in BASH.
|
Display Slide 4
System requirements |
For this tutorial I am using
Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
Pass an array to a function | Let us first learn how to pass an array to a function, and its usage. |
Let me open a file 'function_array.sh' | |
#!/usr/bin/env bash | This is the shebang line. |
array_display()
{
|
Our function name is array_display
|
[Highlight]
array=($@) #Takes array name as variable |
The use of Dollar At-sign was explained in previous tutorials in this series.
|
echo " Array elements are: ${array[@]}"
#displays all element of an array |
Dollar opening curly brace array within square bracket At sign closing curly brace
|
echo "Second element i: ${array[1]}"
#displays the 2nd element return 0 } |
Dollar opening curly brace array within square bracket one closing curly brace
|
[Highlight]
operating_systems=(Ubuntu Fedora Redhat Suse) |
operating_systems is declared with the elements Ubuntu, Fedora, Redhat and Suse |
array_display ${operating_systems[@]} | Here, array 'operating_systems' is passed to the function 'array_display'. |
Display slide 5
Pass an array to a function Syntax: function_name ${array_name[@]} |
The syntax to pass an array to a function is:
function_name space dollar opening curly brace array_name within square bracket 'At sign' closing curly brace |
Come back to our program. | |
colors=(White green red blue)
array_display ${colors[@]}
|
Similarly, 'colors' is declared with the elements
White, green, red and blue
|
Save and Switch to terminal | Now let's save the file and go to the terminal. |
Type
chmod +x function_array.sh>>Press Enter
|
Type:chmod space plus x space function underscore array dot sh
|
Type,
./function_array.sh>>Press Enter |
Type dot slash function underscore array dot sh
Press Enter. |
Terminal
[Output] Array elemnts are: Ubuntu Fedora Redhat Suse Second element is: Fedora Array elements are: White green red blue Second element is: green |
As we can see, array elements of 'operating_systems' and 'colors' are displayed.
|
Display slide 6
'return' and 'exit' statement in functions |
In Bash, 'exit' and 'return' statements gives status code of a function or a program. |
Display slide 7
Return and exit in functions |
return:
exit:
|
Let us learn these 2 ways to return within a function. | |
Open file return_exit.sh | Let me open a file 'return_exit.sh' |
Highlight
#!/usr/bin/env bash |
This is the shebang line. |
function return_function | Function name is return_function |
Highlight { | Open curly brace opens the function definition. |
if [ $1 == $2 ]; then | This if statement compares two variables.
If the two variables are equal, then commands in 'if' are executed. |
echo "This is return function"
#Returns to main program |
This echo statement displays the message
This is return function. return 0 moves the control from function to main program with status code 0(zero). |
[Highlight]
echo "This will not appear" |
Note that the statements after return will not be executed in a function. |
fi
} |
fi indicates the end of if statement |
function exit_function
{ |
Here the function name is exit_function |
if [ $1 == $2 ]; then | The if statement here compares two variables.
If the two variables are equal, then the commands in 'if' are executed. |
echo "This is exit function"
|
This echo statement displays the message "This is exit function" |
exit 0 | exit 0 will terminate the program. |
fi
} |
fi indicates the end of this if statement. |
return_function 3 3 | This is a function call with arguments 3 and 3. |
echo "We are in main program" | This displays the message "We are in main program" |
exit_function 3 3 | This is another function call with arguments 3 and 3. |
Highlight
echo "This line is not displayed" |
This echo statement displays the message "This line is not displayed" |
Note that exit will terminate the program.
Anything after 'exit' will not be executed. | |
Save and Switch to terminal | Save the file and go to the terminal. |
Type
chmod +x return_exit.sh>>Press Enter |
Type:chmod space plus x space return underscore exit dot sh
|
Type ./return_exit.sh>> Press Enter | Now type
dot slash return underscore exit dot sh
|
Terminal
[Output] This is return function We are in main program This is exit function |
The output displays the messages as shown.
Now, let us understand the flow of the program. |
Workflow of Program:
[Highlight program and explain]
|
"This line is not displayed" ` will not be executed. |
Hope the difference is clear to you.
Let us now summarise. | |
Display Slide 8
Summary |
In this tutorial, we learnt
|
Display Slide 9
Assignment |
As an assignment.
Write a program,
|
Display Slide 10
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial About the Spoken Tutorial Project |
Watch the video available at the link shown below.
|
Display Slide 11
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Display Slide 12
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
|
Display Slide 13 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |