BASH/C3/Advance-topics-in-a-function/English

From Script | Spoken-Tutorial
Revision as of 05:42, 8 May 2014 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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