You are on page 1of 34

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH

Name : A V N L Sarojini
Designation : Lecturer
Branch : Computer Engineering
Institute : A.A.N.M. & V.V.R.S.R. Poly.,
Gudlavalleru.
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM-302
Topic : Shell Programming &
Filtering Techniques
Duration : 50 Min
Sub Topic : basics types of shell variables

CM304.12 1
Recap

 Shell programming is a programming that contains


shell commands.

 There are 2 methods to execute a shell script


1) Using Sh command
2) Using chmod command

CM304.12 2
Objective
On completion of this period, you would able to
know
 Shell Variables
 Type of Shell variables
 Working with user defined variables
– Rules for framing variable name
– Shell keywords
– Reading values into shell variables.
– Displaying contents of shell variables.
– Arithmetic's in shell script
– Example Programs
CM304.12 3
What is “Shell variable”?

 Shell variables are part of shell programming.

 They provide the ability to store and manipulate


information with in shell program.

 You can create and destroy any number of variables as


needed to solve a problem in hand.

CM304.12 4
Type of shell Variables

 Variables in UNIX are of 2 types

(c) UNIX-defined Variables (or) System Variables.

(e) User-defined Variables.

CM304.12 5
Unix-defined Variables (or) System Variables

 These are standard variables which are always


accessible.

 The shell provides the values for these variables.

 These variables are usually used by the system


and govern the environment we work under.

CM304.12 6
UNIX-defined Variables (or) System Variables
Contd..

 If we desire we can change the values of these


variables as per our preferences and customize the
system environment.

 PATH, HOME, IFS, MAIL, TERM are some system


variables.

CM304.12 7
User-defined Variables

 These are defined by user and are used most


extensively in shell programming.

CM304.12 8
User-defined Variables

 Note that for all shells, variables need not be declared


explicitly, but simply used for Bourne shell, the use is
as follows

 <variable_name>=<value>

CM304.12 9
Rules for framing variable name

A) A Variable name is any combination of alphabets,


digits and an underscore( _ ).

B) No commas or blanks are allowed within a variable


name.

CM304.12 10
Rules for framing variable name Contd..

C) The first character of a variable name must either be


an alphabet or an underscore.

D) Variable names are case-sensitive.

E)Variable names should be of any reasonable length.

CM304.12 11
Shell Keywords

 Keywords are the words whose meaning has already


been explained to the shell.

 Keywords cannot be used as variable names.

CM304.12 12
Following are the list of keywords

echo if until trap read

else case wait set fi

esac eval unset while break

exec readonly do continue ulimit

shift done exit unmask export

for return

CM304.12 13
Assigning Values to Variables

 We can assign values to shell variables using a simple


assignment operator”=“

 Example

– alpha=“hello world”
– beta=45
– dirname=“/usr/aa5”

CM304.12 14
Reading values into shell variables
 The read statement is used to
– read a line of standard input
– split the line into fields of one or more strings
– assign those strings to shell variables.

 Any strings not assigned are assigned to the last


variable.

Syntax:
read <var 1> <var 2> … <var n>

CM304.12 15
Reading values into shell variables
Contd..

Example:

– read num

– read field1 field2 rest

CM304.12 16
All shell variables are string variables.

 All shell variables are string variables.

Example a=20

– In the statement a=20, the ‘20’ is stored in ‘a’


– ‘a’ is treated not as number, but as a string.

CM304.12 17
Using shell variables

 Once a variable has been defined, it’s value can be


used by “dereferencing” it with $.

Example:
mypath=/home/special/public_html
ls –al $mypath

CM304.12 18
Displaying contents of a variable

 echo command is used to display message as well as


contents of variables.

CM304.12 19
Displaying contents of a variable
Contd..

 Example
$c=“Two Words”
$echo $c

$name=Johny age=10
$echo $name $age
$echo Name of the boy is $name age is $age

CM304.12 20
Example Program

$cat telno.sh
echo “Enter the Name”
read name
echo “Enter the Telephone Number”
read number
echo $name $number

CM304.12 21
Arithmetic's in Shell Script

 You may recall that all shell variables are string


variables.

 If we have to carry out arithmetic operations on them


we have to use the command expr which is capable
of evaluating an arithmetic expression.

CM304.12 22
Example program

$cat arith.c
a=20
b=10
echo `expr $a + $b`
echo `expr $a - $b`
echo `expr $a \* $b`
echo `expr $a / $b`
Note: The character used before expr is called grave
accent i.e., the character below the tilde sign in the
keyboard.

CM304.12 23
Summary

 Shell variable provides ability to store and manipulate


information in a shell program.
 Shell variables can be
- system defined
- user defined
 To displays the value of a shell variable “echo”
command is used.

CM304.12 24
Quiz

1.In a=1235, ‘a’ is a numeric shell variable.


[true/false]

CM304.12 25
Quiz

1.In a=1235, ‘a’ is a numeric shell variable.


[true/false]

CM304.12 26
Quiz

2.Shell variables are case sensitive.


[true/false]

CM304.12 27
Quiz

2.Shell variables are case sensitive.


[true/false]

CM304.12 28
Quiz

3. Variables declared in a shell script can be displayed


at the dollar prompt using the set command.
[true/false]

CM304.12 29
Quiz

3. Variables declared in a shell script can be displayed


at the dollar prompt using the set command.
[true/false]

CM304.12 30
Pick up the correct option

4. Which of the following is NOT a shell Keyword.


A) shift
B) readlonly
C) unset
D) ls

CM304.12 31
Pick up the correct option

4.Which of the following is NOT a shell Keyword.


A) shift
B) readlonly
C) unset
D) ls

CM304.12 32
FAQ’s

1.Write shell scripts for the following

 Ramesh’s basic salary is input through the keyboard.


His dearness allowance is 40% of basic salary, and
house rent allowance is 20% of basic salary. Write a
program to calculate his gross salary.

CM304.12 33
FAQ’s

2.Write shell scripts for the following

 Write a shell script which will receive either the


filename or the filename with its full path during
execution. This script should obtain information
about this file as given by ls -l and display it in proper
format.

CM304.12 34

You might also like