BASH/C2/Basics-of-Shell-Scripting/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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.


Visual Cue
Narration
Display Slide Welcome to the spoken tutorial on Basics of Shell Scripting.
Display Slide In this tutorial, we will learn about
  • System variables
  • User defined variables and
  • Accepting user input via keyboard


Display Slide

Prerequisites


spoken-tutorial.org

To follow this tutorial you should be familiar with Linux Operating System


If not, for relevant tutorials please visit spoken hyphen tutorial dot org.

Display Slide

System Requirements

For this tutorial I am using
  • Ubuntu Linux 12.04 OS and
  • GNU Bash version 4.1.10

GNU Bash version 4 or above is recommended to practise this tutorial.

Display slide

Variables


* Bash variables provide temporary storage for information.
  • These variables can be used within the lifespan of the program.
  • There are two types of variables
  1. System variables
  2. User defined variables


Display Slide

System Variables


System variables are created and maintained by Linux Bash Shell itself.


They are defined by Capital letters.


Commonly used system variables are

  • BASH_VERSION,
  • HOSTNAME,
  • HOME etc


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


Output:

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:
  • These variables are created and maintained by users.
  • It is always a good idea to avoid uppercase for the names of user defined variables.
  • This makes it easy to differentiate between user defined and system 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


Highlight output:

sunita

Now, to display the value of variable username

type:

echo space open double quote dollar sign username close double quote and press Enter


This will display sunita on your terminal.

<<PAUSE>>

Display Slide

Unset

syntax :

unset variablename

The value of a variable can be unset by using the unset command.


The syntax for this is unset space variablename

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.


Nothing will be displayed on the terminal.


This means that the value of variable username has been removed.

<<PAUSE>>

Display Slide

Global and Local Variables

* In Shell script, user defined variables can be declared globally and locally.
  • By default, all variables are global.
  • This means, their values remain the same inside and outside the function.


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:


And

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.


Here another message inside function will be displayed, along with the value of username.

Highlight func Here, we call the function func.


This is our code. Now let's execute it.

Switch to terminal>>


Type

chmod +x g_variable.sh>> Press Enter


Switch to the Terminal.


First we need to make our file executable.


So type

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


Type

chmod +x l_variable.sh>> Press Enter

Switch to the Terminal.


Let's make our file executable by typing

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.


Whereas inside the function, username takes the value jack.

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.


The read command is used to accept input from the keyboard.


It can also be used to assign an input value to a user defined variable.

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


Please note that PROMPT is just a string, that waits for user input.


You may replace it with your own string.

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.


The user input will be stored in the variable username.

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


Type

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:


I will type ashwini and press Enter.

Hello ashwini The message Hello ashwini is displayed.


ashwini was assigned as an input value to the user defined variable username.

<<PAUSE>>

Display slide

Summary

In this tutorial we learnt,
  • System variables
  • User defined variables
  • Accepting user input via keyboard


Display slide

Assignment

# Write a simple Bash program to get the following system variables
  • pwd
  • logname
  1. Write a simple Bash program
  • To ask username from user
  • To exit the program, if user does not enter anything, within 10 seconds
  • {Hint: read -t 10 -p}

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


And this is Ashwini from IIT Bombay.

Thank you for joining.



Contributors and Content Editors

Ashwini