BASH/C3/Advance-topics-in-a-function/English
Title of script: Advance topics in function
Author: Lavitha Pereira
Keywords: Video tutorial, source command, background function
|
|
Display Slide 1 | Dear friends,
Welcome to the spoken tutorial on Advance topics in function |
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. |
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. |
Display Slide 5
Source Command |
* Source command is used to load a file into Shell script.
|
Display Slide 6
Source Command |
* It also imports code into the script.
|
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”
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.
|
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
|
bg_function & | Now type:
bg underscore function & This is function call.
|
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]
[1]+ 5787 Running bg_function & Last line of the code |
The output is shown.
PID will vary accordingly.
And we will see the status as 'Running'
This brings us to the end of this tutorial. |
Display Slide 10
Summary |
Summary
Let us summarise. In this tutorial we learnt
|
Display Slide 10
Assignment |
As an assignment:
|
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.
|
Display Slide 12
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
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.
|
Display Slide 14 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |