BASH/C2/Basics-of-Shell-Scripting/English
Title of script: Basics of Shell Scripting
Author: FOSSEE and Spoken-tutorial Team
Keywords: Video tutorial, Bash, Shell, System variables, User defined variables, getting user input via keyboard.
|
|
---|---|
Display Slide | Welcome to the spoken tutorial on Basics of Shell Scripting. |
Display Slide | In this tutorial, we will learn about
|
Display Slide
Prerequisites
|
To follow this tutorial you should be familiar with Linux Operating System
|
Display Slide
System Requirements |
For this tutorial I am using
GNU Bash version 4 or above is recommended to practise this tutorial. |
Display slide
Variables
|
* Bash variables provide temporary storage for information.
|
Display Slide
System Variables
|
System variables are created and maintained by Linux Bash Shell itself.
|
Press Ctrl+Alt+T | Let us open the terminal by pressing Ctrl+Alt+T keys. |
Terminal:
set>>Press Enter |
Now type set and press Enter.
This will display all the system variables. |
Alternately, you can type env or printenv, to view all the system variables. | |
On terminal>> type
echo "$HOSTNAME">> Press Enter
|
Next, type
echo space opening double quote dollar sign HOSTNAME closing double quote and press Enter. The hostname of the system will be displayed. |
Now let's find out the full path of home directory. | |
On terminal type>>echo "$HOME">> press Enter | So, type
echo space open double quote dollar sign HOME close double quote and press Enter. The full path of user's home directory will be displayed. |
On terminal type>>echo "HOME">> press Enter
HOME |
Now, type
echo space open double quote HOME close double quote and press Enter. This will display only the HOME not the value of HOME variable. |
So it is neccessary to use dollar sign '$' at the beginning of every variable, to display its value.
<<PAUSE>> | |
Display slide
User Defined Variables
|
User defined variables:
|
Switch to Terminal>> type
username=sunita>> Press Enter |
On the terminal type:
username equal to sunita Please note that there should not be any blank space between username, equal to sign and sunita. Now press Enter. |
Type >>echo "$username">> Press Enter
sunita |
Now, to display the value of variable username
type: echo space open double quote dollar sign username close double quote and press Enter
<<PAUSE>> |
Display Slide
Unset syntax : unset variablename |
The value of a variable can be unset by using the unset command.
|
Let's use the previous example where username is our variable. | |
Switch to Terminal>> type unset username>> press Enter
|
Switch to the terminal and type
unset space username and press Enter. |
Type echo $username>> press Enter | Now type
echo space dollar sign username and press Enter.
<<PAUSE>> |
Display Slide
Global and Local Variables |
* In Shell script, user defined variables can be declared globally and locally.
|
Let us learn how to declare variables globally and locally. | |
Switch to terminal>>gedit g_variable.sh>> press Enter | Switch to the terminal and type
gedit space g_variable.sh and press Enter. |
Type the code as shown, in your g_variable.sh file. | |
Let me explain the code now. | |
Highlight #!/bin/bash | The first line, with the hash and exclamation symbols, is a shebang or a bang line. |
Highlight username=sunita | username=sunita is the userdefined variable and it is declared globally. |
Highlight echo "outside function: $username" | echo will display the string outside function:
dollar username will print the value of the variable username. |
Highlight func() | This is how a function is defined in BASH script.
We will discuss about functions in detail, in later tutorials. |
Highlight
{ echo "inside function: $username" } |
This is the body of the function.
|
Highlight func | Here, we call the function func.
|
Switch to terminal>>
chmod +x g_variable.sh>> Press Enter
|
Switch to the Terminal.
chmod space plus x space g_variable.sh and press Enter |
Type ./g_variable.sh>>Press Enter | Now let us execute this Shell script by typing
dot slash g_variable.sh and press Enter |
Output:
Highlight: outside function: sunita inside function: sunita |
Observe the output displayed.
Outside the function, username takes the value sunita. Inside the function also, username takes the same value i.e. sunita. |
Switch to the program code in gedit. | This is because username was declared globally outside the function.
<<PAUSE>> |
Next, let us learn how to declare a variable locally. | |
Switch to terminal>>gedit g_variable.sh>> press Enter | Switch to the terminal and type
gedit space l_variable.sh and press Enter. |
Type the code as shown, in your l_variable.sh file. | |
Let me explain the code now. | |
The code is the same as before, except for an extra line of code inside the function. | |
func() {
local username=jack echo "inside function: $username" } func |
Inside the function block, we have a line,
local space username equal to jack |
This assigns a new value for the variable username locally.
<<PAUSE>> | |
Switch to terminal>>
chmod +x l_variable.sh>> Press Enter |
Switch to the Terminal.
chmod space plus x space l_variable.sh and press Enter. |
Type ./l_variable.sh>>Press Enter | Now let us execute our Shell script by typing
dot slash l_variable.sh and press Enter. |
Output:
highlight: outside function: sunita inside function: jack |
Outside the function, username takes the value sunita.
This is because username is assigned this value locally, within the function. <<PAUSE>> |
Display Slide
Getting user input via keyboard
|
Now let us quickly see how to get user input via keyboard.
|
Display Slide
Syntax read -p "PROMPT " man read |
The syntax of read command is
read space hyphen p space open double quote PROMPT close double quote Press Enter Now type, man read
|
Switch to terminal>>gedit read.sh>> press Enter | Switch to the terminal and type
gedit space read.sh and press Enter. |
Type the code as shown, in your read.sh file. | |
Let me explain the code now. | |
In this example, input is given from the keyboard by the user. | |
#!/bin/bash | This is the shebang or a bang line. |
read -p "Enter username: " username | Here -p displays the prompt, without a newline and takes input from the keyboard.
|
echo "Hello $username"
|
echo command displays the message Hello and the name entered by the user, via the keyboard. |
<<PAUSE>>
So, let's switch to the Terminal and execute this file. | |
Switch to terminal>>
chmod +x read.sh>> Press Enter |
First, let's make our file executable by typing
chmod space plus x space read.sh and press Enter. |
Type ./read.sh>>Press Enter | Now let us execute our Shell script by typing
dot slash read.sh and press Enter. |
Output:
Enter username: |
Enter username:
|
Hello ashwini | The message Hello ashwini is displayed.
<<PAUSE>> |
Display slide
Summary |
In this tutorial we learnt,
|
Display slide
Assignment |
# Write a simple Bash program to get the following system variables
<<PAUSE>> |
Display Slide | 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
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
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 |
The script for this tutorial was written by the FOSSEE Team, IIT Bombay.
Thank you for joining.
|