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

From Script | Spoken-Tutorial
Revision as of 16:22, 7 May 2014 by Ashwini (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 to practice this tutorial.

Display Slide 5

Source Command

* Source command is used to load a file into Shell script.
  • It reads and execute commands from that file.


Display Slide 6

Source Command

* It also imports code into the script.
  • 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 space filename

source space 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 here,

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



Type:

echo “Beginning of program”

machine

echo “End of program”

Let us go back to function.sh file.

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

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

Type

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

* 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 file.

bg_function & Now type:

bg underscore function &

This is function call.


Ampersand puts 'bg_function' in the background


Let us execute the program.



Type

chmod +x background.sh>>Press Enter


Type:chmod space plus x space background dot sh

Press Enter.

Type,

./background.sh>>Press Enter

Type dot slash background dot sh

Press Enter.

The output is displayed.

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

type:

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

type:

jobs -l

Come back to our program.

Now type:

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

and

jobs -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.

Type

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.


5787 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.

Display Slide 10

Summary

Summary

Let us summarise.

In this tutorial we learnt

  • Source command
  • To put a function in 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