Difference between revisions of "BASH/C3/Advance-topics-in-a-function/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script: Advance topics in function ''' '''Author:''' Lavitha Pereira '''Keywords: Video tutorial, source command, background function''' {| style="border-spacing:…')
 
 
Line 41: Line 41:
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For this tutorial I am using''' '''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For this tutorial I am using''' '''
  
* '''Ubuntu Linux''' 12.04''' '''Operating System and
+
* '''Ubuntu Linux''' 12.04 Operating System and
 
* '''GNU BASH''' version 4.2
 
* '''GNU BASH''' version 4.2
  
Please note,''' GNU Bash''' '''version 4''' or above is recommended to practice this tutorial.
+
Please note,''' GNU Bash''' version 4 or above is recommended for practice.
  
 
|-
 
|-
Line 50: Line 50:
  
 
'''Source Command'''
 
'''Source Command'''
| 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;"| * '''Source '''command is used to load a file into '''Shell '''script.
+
| 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;"|  
 +
* '''Source '''command is used to load a file into '''Shell '''script.
 
* It reads and execute commands from that file.
 
* It reads and execute commands from that file.
 
+
* It also imports code into the script.
 
+
  
 
|-
 
|-
Line 59: Line 59:
  
 
'''Source Command'''
 
'''Source Command'''
| 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;"| * It also imports code into the script.
+
| 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 useful when multiple scripts use a common data or a function library.
 
* This is useful when multiple scripts use a common data or a function library.
 
 
  
 
|-
 
|-
Line 78: Line 76:
  
 
'''<nowiki>souce <filename> <arguments></nowiki>'''
 
'''<nowiki>souce <filename> <arguments></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;"| '''source space filename'''
+
| 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;"| '''source filename'''
  
'''source space path_to_file'''
+
'''source path_to_file'''
  
 
'''source filename arguments'''
 
'''source filename arguments'''
 
 
 
  
 
|-
 
|-
 
| 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 '''function.sh''' file
 
| 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 '''function.sh''' file
 
| 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;"| Let me open a file ''''function.sh''''.  
 
| 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;"| Let me open a file ''''function.sh''''.  
 
 
 
  
 
|-
 
|-
Line 104: Line 96:
  
 
'''source detail.sh '''
 
'''source detail.sh '''
| 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;"| Source''' detail.sh '''will''' '''load the file '''detail.sh '''into''' function.sh '''
+
| 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;"| Source''' detail.sh '''will load the file '''detail.sh '''into''' function.sh '''
  
 
|-
 
|-
Line 114: 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;"| Let me open''' detail.sh file. '''
+
| 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;"| Let me open''' detail.sh''' file.
  
I have a function''' “machine”'''
+
I have a function''' “machine”'''.
  
Now type here,
+
Now type inside the function,
  
 
'''echo "function machine is called in function.sh file"'''
 
'''echo "function machine is called in function.sh file"'''
  
 
+
Click on '''Save'''.
 
+
 
+
 
|-
 
|-
 
| 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;"| Type:
 
| 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;"| Type:
Line 135: Line 125:
 
| 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;"| Let us go back to '''function.sh '''file.
 
| 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;"| Let us go back to '''function.sh '''file.
  
Type:
+
Here type:
  
'''echo''' '''“Beginning of program”'''
+
'''echo “Beginning of program”'''
  
click on''' Save'''
+
Click on''' Save'''
  
 
Then type: '''machine'''
 
Then type: '''machine'''
  
 
'''echo “End of program”'''
 
'''echo “End of program”'''
 
 
 
  
 
|-
 
|-
Line 157: 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;"| This will print the message “Beginning 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;"| This will print the message “Beginning of program”
  
machine is the function call.
+
'''machine''' is the '''function call.'''
  
 
This will print the message''' End of program'''
 
This will print the message''' End of program'''
  
Note that '''machine''' is the function which we created in the '''detail.sh''' file.
+
|-
 +
| 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 as per narration
 +
| 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 '''machine''' is the '''function''' which we created in the '''detail.sh''' file.
  
And we are calling the function here in '''function.sh''' file.
+
And we are calling the '''function''' here in '''function.sh''' file.
  
Now click on '''Save'''
+
Now click on '''Save'''.
  
 
|-
 
|-
 
| 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;"| Let us execute''' '''the 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;"| Let us execute the program.
 
+
So type on the terminal
+
  
 
|-
 
|-
| 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;"| Type''' '''
+
| 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;"| So type on the '''terminal'''
 
+
'''chmod +x function.sh>>'''Press '''Enter '''
+
  
 +
'''chmod +x function.sh>>'''
  
 +
Press '''Enter '''
  
 
| 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;"| Type:'''chmod space plus x space function dot sh'''
 
| 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;"| Type:'''chmod space plus x space function dot sh'''
Line 201: Line 188:
 
'''End of program'''
 
'''End 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;"| The output is 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;"| The output is displayed
 
 
  
  
Line 213: Line 198:
  
 
'''Background function'''
 
'''Background 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;"| * To run a process in the background, we use <tt>&</tt>''' (ampersand)''' at the end of a '''function call.'''
+
| 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 slides.
 +
 
 +
* To run a process in the background, we use <tt>&</tt>''' (ampersand)''' at the end of a '''function call.'''
 
* The '''shell''' forks a '''child process''' to run the command.  
 
* The '''shell''' forks a '''child process''' to run the command.  
 
* The forked process will have a '''job number (<tt><nowiki>[</nowiki></tt>''n''<tt>]</tt>)''' and a '''PID (Process IDentifier)'''
 
* The forked process will have a '''job number (<tt><nowiki>[</nowiki></tt>''n''<tt>]</tt>)''' and a '''PID (Process IDentifier)'''
 
 
  
 
|-
 
|-
Line 236: Line 221:
 
| 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 -e "Inside bg_function\n" '''
 
| 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 -e "Inside bg_function\n" '''
  
 
+
| 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 '''echo statement''' here displays the message '''"Inside bg_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;"| The echo statement here displays the message '''"Inside bg_function”'''
+
  
 
|-
 
|-
Line 244: Line 227:
  
 
'''} '''
 
'''} '''
| 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;"| Next, we will use the '''find command '''to find all the .'''mp3 '''files.This statement will find all the files with the extension '''.mp3'''
+
| 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;"| Next, we will use the '''find command '''to find all the .'''mp3 '''files.
  
 +
This statement will find all the files with the extension '''.mp3'''
  
It will do so in the current working directory.
 
  
 +
It will do so in the '''current working directory.'''
  
'''Hyphen iname '''is used to ignore the case.And the result is stored in''' myplaylist.txt '''file.
+
 
 +
'''Hyphen iname '''is used to ignore the case.
 +
 
 +
And the result is stored in''' myplaylist.txt '''.
  
 
|-
 
|-
Line 258: Line 245:
 
'''bg underscore function &'''
 
'''bg underscore function &'''
  
This is '''function call.'''
+
This is the '''function call.'''
  
  
Ampersand puts 'bg_function' in the background
+
'''Ampersand''' puts ''''bg_function'''' in the background.
  
 +
 +
Now click on '''Save'''.
  
 
Let us execute the program.
 
Let us execute the program.
 
 
 
  
 
|-
 
|-
Line 273: Line 259:
  
 
'''chmod +x background.sh>>'''Press '''Enter '''
 
'''chmod +x background.sh>>'''Press '''Enter '''
 +
| 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 the '''terminal.'''
  
 
+
Type:'''chmod space plus x space background dot sh'''
 
+
| 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;"| Type:'''chmod space plus x space background dot sh'''
+
  
 
Press '''Enter.'''
 
Press '''Enter.'''
  
 
|-
 
|-
| 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;"| Type''', '''
+
| 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;"| Type  
  
 
'''./background.sh>>'''Press '''Enter'''
 
'''./background.sh>>'''Press '''Enter'''
Line 288: Line 273:
 
Press''' Enter.'''
 
Press''' Enter.'''
  
The output is displayed.
 
  
Blank output indicates '''.mp3''' file is not present in current directory.
+
|-
 +
| 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;"|Blank output indicates '''.mp3''' file is not present in current directory.
  
 
|-
 
|-
Line 300: Line 286:
  
 
'''jobs -l'''
 
'''jobs -l'''
| 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.
+
| 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;"| Now, come back to our program.
  
Now type:  
+
Type:  
  
'''echo -e "Process runing in background are: \n" '''
+
'''echo -e "Process runing in background are: slash n" '''
  
 
and
 
and
 +
'''jobs space hyphen l'''
  
'''jobs -l'''
 
  
 
Click on''' Save'''
 
Click on''' Save'''
 
 
 
  
 
|-
 
|-
Line 327: Line 310:
  
  
| 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;"| '''Jobs '''space''' hyphen l '''lists the status of all '''background jobs'''.
+
| 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;"| '''Jobs space hyphen l '''lists the status of all '''background jobs'''.
  
 
|-
 
|-
 
| 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;"| Switch to '''terminal'''
 
| 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;"| Switch to '''terminal'''
| 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;"| Now come back to our terminal.
+
| 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;"| Now come back to our '''terminal'''.
  
Let me clear the prompt.
+
Let me clear the '''prompt'''.
  
 
|-
 
|-
| 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;"| Type''' '''
+
| 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;"|Press the uparrow key.
 
+
'''Press the uparrow key.'''
+
  
  
  
| 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;"| Press the''' uparrow '''key''' '''twice'''.'''
+
| 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;"| Press the''' uparrow '''key twice.
  
 
|-
 
|-
Line 367: Line 348:
  
  
Here, one within opening and closing square bracket is the '''job number.'''
+
Here, '''one''' within opening and closing square bracket is the '''job number.'''
  
  
'''5787 is the PID.'''
+
3962 is the PID.
  
 
'''PID '''will vary accordingly.
 
'''PID '''will vary accordingly.
  
  
'''If the function takes time to execute, it will run in the background.'''
+
If the function takes time to execute, it will run in the background.
  
'''And we will see the status as 'Running' '''
+
And we will see the status as ''''Running' '''.
  
  
'''If the function gets executed before the script, we will see the status as 'Done'.''''
+
If the function gets executed before the script, we will see the status as ''''Done'.''''
  
  
Line 386: Line 367:
  
 
This brings us to the end of this tutorial.
 
This brings us to the end of this tutorial.
 +
 +
Come back to our slides.
  
 
|-
 
|-
Line 398: Line 381:
  
 
* '''Source '''command
 
* '''Source '''command
* To put a function in '''background'''
+
* To put a function in the '''background'''
 
* With the help of some examples  
 
* With the help of some examples  
 
 
  
 
|-
 
|-

Latest revision as of 05:42, 8 May 2014

Title of script: Advance topics in function

Author: Lavitha Pereira

Keywords: Video tutorial, source command, background function


Visual Cue
Narration
Display Slide 1 Dear friends,

Welcome to the spoken tutorial on Advance topics in function

Display Slide 2 In this tutorial, we will learn
  • Source command
  • how to put a function in the background
  • with the help of some examples.


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.

Display Slide 4

System requirements

For this tutorial I am using
  • Ubuntu Linux 12.04 Operating System and
  • GNU BASH version 4.2

Please note, GNU Bash version 4 or above is recommended for practice.

Display Slide 5

Source Command

  • Source command is used to load a file into Shell script.
  • It reads and execute commands from that file.
  • It also imports code into the script.
Display Slide 6

Source Command

  • This is useful when multiple scripts use a common data or a function library.
The syntax for Source command is as follows.
Display Slide 7

Syntax:

source <filename>

source <Path_to_file>

souce <filename> <arguments>

source filename

source path_to_file

source filename arguments

Open function.sh file Let me open a file 'function.sh'.
#!/usr/bin/env bash This is a shebang line.
#function.sh

[Highlight]

source detail.sh

Source detail.sh will load the file detail.sh into function.sh
function machine{

type,

echo "function machine is called in function.sh file"

}

Let me open detail.sh file.

I have a function “machine”.

Now type inside the function,

echo "function machine is called in function.sh file"

Click on Save.

Type:

echo “Beginning of program”

machine

echo “End of program”

Let us go back to function.sh file.

Here type:

echo “Beginning of program”

Click on Save

Then type: machine

echo “End of program”

echo “Beginning of program”


machine

echo “End of program”

This will print the message “Beginning of program”

machine is the function call.

This will print the message End of program

Highlight as per narration Note that machine is the function which we created in the detail.sh file.

And we are calling the function here in function.sh file.

Now click on Save.

Let us execute the program.
So type on the terminal

chmod +x function.sh>>

Press Enter

Type:chmod space plus x space function dot sh

Press Enter.

Type,

./function.sh>>Press Enter

Type dot slash function dot sh

Press Enter.

Highlight

Beginning of program

function machine is called in function.sh file

End of program

The output is displayed


Let's move on to background function.
Display slide 9

Background function

Come back to our slides.
  • To run a process in the background, we use & (ampersand) at the end of a function call.
  • The shell forks a child process to run the command.
  • The forked process will have a job number ([n]) and a PID (Process IDentifier)
Let us understand this with the help of an example. I will open the file background.sh
#!/usr/bin/env bash This is the shebang line.
bg_function()

{

bg underscore function marks the beginning of the function.
echo -e "Inside bg_function\n" The echo statement here displays the message "Inside bg_function”
find . -iname "*.mp3" > myplaylist.txt

}

Next, we will use the find command to find all the .mp3 files.

This statement will find all the files with the extension .mp3


It will do so in the current working directory.


Hyphen iname is used to ignore the case.

And the result is stored in myplaylist.txt .

bg_function & Now type:

bg underscore function &

This is the function call.


Ampersand puts 'bg_function' in the background.


Now click on Save.

Let us execute the program.

Type

chmod +x background.sh>>Press Enter

Come back to the terminal.

Type:chmod space plus x space background dot sh

Press Enter.

Type

./background.sh>>Press Enter

Type dot slash background dot sh

Press Enter.


Blank output indicates .mp3 file is not present in current directory.
type:

echo -e "Process runing in background are: \n"

type:

jobs -l

Now, come back to our program.

Type:

echo -e "Process runing in background are: slash n"

and jobs space hyphen l


Click on Save

"Process runing in background are: \n"


This echo statement displays the message “Process runing in background are:”
jobs -l


Jobs space hyphen l lists the status of all background jobs.
Switch to terminal Now come back to our terminal.

Let me clear the prompt.

Press the uparrow key.


Press the uparrow key twice.
Type,

./background.sh>>Press Enter

Press Enter.
Terminal:

[Output]


Inside bg_function


Process runing in background are:

[1]+ 5787 Running bg_function &

Last line of the code

The output is shown.


Here, one within opening and closing square bracket is the job number.


3962 is the PID.

PID will vary accordingly.


If the function takes time to execute, it will run in the background.

And we will see the status as 'Running' .


If the function gets executed before the script, we will see the status as 'Done'.'


The output will vary from machine to machine.

This brings us to the end of this tutorial.

Come back to our slides.

Display Slide 10

Summary

Summary

Let us summarise.

In this tutorial we learnt

  • Source command
  • To put a function in the background
  • With the help of some examples
Display Slide 10

Assignment

As an assignment:


Write a function addnumbers to add two numbers and call the function in another file.

Display Slide 11

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

About the Spoken Tutorial Project

Watch the video available at the link shown below.


It summarises the Spoken Tutorial project.


If you do not have good bandwidth, you can download and watch it.

Display Slide 12

Spoken Tutorial Workshops

The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide 13

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project.


It is supported by the National Mission on Education through ICT, MHRD, Government of India.


More information on this Mission is available at:

http://spoken-tutorial.org\NMEICT-Intro

Display Slide 14 The script has been contributed by FOSSEE and Spoken-Tutorial teams.


This is Ashwini from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey