Difference between revisions of "BASH/C3/More-on-functions/English"
(Created page with ''''Title of script: More on functions''' '''Author:''' Lavitha Pereira '''Keywords: Video tutorial, Bash shell, function, function call''' {| style="border-spacing:0;" | styl…') |
Nancyvarkey (Talk | contribs) |
||
Line 18: | Line 18: | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial, we will learn | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial, we will learn | ||
− | * To pass an argument to a '''function ''' | + | * To pass an '''argument''' to a '''function ''' |
* Define''' local variable''' within''' '''a '''function '''and | * Define''' local variable''' within''' '''a '''function '''and | ||
* Define '''global variable''' in a '''function''' | * Define '''global variable''' in a '''function''' | ||
* With the help of some examples | * With the help of some examples | ||
− | |||
− | |||
|- | |- | ||
Line 33: | Line 31: | ||
If not, for relevant tutorials please visit our website which is as shown,'''(http://www.spoken-tutorial.org)''' | If not, for relevant tutorials please visit our website which is as shown,'''(http://www.spoken-tutorial.org)''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 58: | Line 53: | ||
|- | |- | ||
| 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 '''<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;"| Highlight '''<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 86: | Line 81: | ||
− | The syntax is, '''function name''' i.e. '''say | + | The syntax is, '''function name''' i.e. '''say welcome ''' |
Line 103: | Line 98: | ||
So, I have | So, I have | ||
− | '''say_welcome''' | + | '''say_welcome space''' within double quote '''functions in space''' and within double quote '''Bash'''. |
|- | |- | ||
Line 140: | Line 135: | ||
− | | 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;"| We see that the positional parameters were substituted by the arguments passed to | + | | 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;"| We see that the positional '''parameters''' were substituted by the '''arguments''' passed to the '''function'''. |
− | '''Dollar 1($1) '''was substituted by the | + | '''Dollar 1($1) '''was substituted by the string''' Bash''' and '''Dollar 2($2) '''with''' learning.''' |
Line 156: | Line 151: | ||
* It's value will be valid within the '''function''' in which it is defined. | * It's value will be valid within the '''function''' in which it is defined. | ||
* '''Local variables '''are declared using''' '''keyword ''''local'.''' | * '''Local variables '''are declared using''' '''keyword ''''local'.''' | ||
− | |||
− | |||
|- | |- | ||
Line 165: | Line 158: | ||
| 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;"| '''Global variable:''' | | 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;"| '''Global variable:''' | ||
− | * | + | * The value of a''' global variable '''can be accessed throughout a '''Bash '''script. |
− | + | ||
− | + | ||
|- | |- | ||
Line 183: | Line 174: | ||
|- | |- | ||
| 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;"| '''say_hello(){ ''' | | 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;"| '''say_hello(){ ''' | ||
− | | 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;"| Function name 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;"| Function name is '''say underscore hello''' |
|- | |- | ||
Line 244: | Line 235: | ||
* '''$middle_name '''and | * '''$middle_name '''and | ||
* '''$last_name''' | * '''$last_name''' | ||
− | |||
− | |||
|- | |- | ||
Line 279: | Line 268: | ||
Hello Pratik K Patil | Hello Pratik K Patil | ||
− | |||
− | |||
| 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 first line of output displays the message '''Hello Pratik K Patil.''' | | 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 first line of output displays the message '''Hello Pratik K Patil.''' | ||
Line 288: | Line 275: | ||
Which means the value is limited to the '''function'''. | Which means the value is limited to the '''function'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 331: | Line 315: | ||
* To pass '''arguments''' to a''' function ''' | * To pass '''arguments''' to a''' function ''' | ||
− | * To declare '''Local variable '''in | + | * To declare '''Local variable '''in a '''function''' |
− | * To declare '''Global variable '''in | + | * To declare '''Global variable '''in a '''function''' |
* with the help of a few examples | * with the help of a few examples | ||
− | |||
− | |||
|- | |- | ||
Line 347: | Line 329: | ||
* Where the '''function''' accepts two '''arguments.''' | * Where the '''function''' accepts two '''arguments.''' | ||
* The '''function''' should multiply the two '''arguments'''. | * The '''function''' should multiply the two '''arguments'''. | ||
− | * Make 3 '''function | + | * Make 3 '''function calls''' with '''arguments''' (1, 2), (2, 3) and (3, 4) |
− | + | ||
Latest revision as of 22:28, 20 March 2014
Title of script: More on functions
Author: Lavitha Pereira
Keywords: Video tutorial, Bash shell, function, function call
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on More on functions |
Display Slide 2 | In this tutorial, we will learn
|
Display Slide 3Prerequisites
|
To follow this tutorial you should have knowledge of Shell Scripting in BASH.
If not, for relevant tutorials please visit our website which is as shown,(http://www.spoken-tutorial.org) |
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. |
Let us first learn how to pass an argument to a function, and its usage. | |
Let me open a file 'function_parameters.sh' | |
Highlight #!/usr/bin/env bash | This is the shebang line. |
Highlight say_welcome()
{ |
Our function name is say_welcome
|
Highlight echo "Welcome to $1 $2" | $1 is the first positional parameter
$2 is the second postional parameter |
Highlight } | Close curly bracket closes the function definition. |
Highlight
say_welcome "Bash" "learning" |
Here, the function 'say_welcome' is called with arguments.
|
Highlight
say_welcome "functions in" "Bash"
|
In a similar manner, I will call the same function with a different set of arguments.
say_welcome space within double quote functions in space and within double quote Bash. |
Save and Switch to terminal | Save the file and go to the terminal. |
Type
chmod +x function_parameters.sh>>Press Enter
|
Type:chmod space plus x space function underscore parameters dot sh
|
Type
./function_parameters.sh>>Press Enter |
Type dot slash function underscore parameters dot sh
Press Enter. |
Terminal
[Output] Welcome to Bash learning Welcome to functions in Bash
|
We see that the positional parameters were substituted by the arguments passed to the function.
|
Display slide 5
Local and Global variables in functions |
* In Bash, variables can be declared as local variables and global variables.
|
Display slide 6
Local and Global variables in functions |
Global variable:
|
Let us learn these 2 ways to declare a variable within a function. | |
Let me open a file 'function_local.sh' | |
Highlight #!/usr/bin/env bash | This is the shebang line. |
say_hello(){ | Function name is say underscore hello |
Highlight local first_name=$1
#Local variable |
Here, variable first_name is declared with keyword local.
|
Highlight last_name=$2
# Global variable |
A variable declared without any keyword, is treated as a global variable.
|
Highlight echo "Hello $first_name $middle_name $last_name"
Highlight } |
In this echo line, we will display the value of variables
After this, we close this function. |
Highlight middle_name="K"
#global variable |
Now, here the variable middle_name is declared without keyword.
|
Highlight say_hello "Pratik" "Patil" | Once again, we will call the function here.
|
Highlight
echo "My first name is: $first_name" echo "My middle name is: $middle_name" echo "My last name is: $last_name" |
These echo statements will display the value of variables
|
Please keep in mind that variable first_name is a local variable. | |
Save and Switch to terminal | Save the file and go to the terminal. |
Type
chmod +x function_local.sh>>Press Enter |
Type:chmod space plus x space function underscore local dot sh
|
Type ./function_local.sh>> Press Enter | Now type
dot slash function underscore local dot sh
|
Terminal
[Output] Hello Pratik K Patil |
The first line of output displays the message Hello Pratik K Patil.
Here, the variable first_name that contains value “Pratik” is local.
|
Now, let us see how the local variable behaves outside the function. | |
Terminal
[Output] My first name is: My middle name is: k My last name is: Patil |
Here, nothing is displayed in first_name.
|
Hope the difference is clear to you.
| |
Display Slide 7
Summary |
In this tutorial, we learnt
|
Display Slide 7
Assignment |
As an assignment.
Write a program,
|
Display Slide 8
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 9
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Display Slide 10
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
|
Display Slide 11 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |