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 for practice. |
Display Slide 5
Source Command |
|
Display Slide 6
Source Command |
|
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”
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.
|
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
And the result is stored in myplaylist.txt . |
bg_function & | Now type:
bg underscore function & This is the function call.
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
|
"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]
[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. Come back to our slides. |
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. |