BASH/C3/Arrays-and-functions/English
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. |