17BHM304 Unix Unit V
17BHM304 Unix Unit V
17BHM304 Unix Unit V
The shell provides an interface to the UNIX system. It gathers input from the user
and executes programs based on that input. When a program finishes executing,
it displays that program's output.
A shell is an environment in which the user can run commands, programs, and
shell scripts.
Shell Prompt:
The prompt, $, which is called command prompt, is issued by the shell. While the
prompt is displayed, the user can type a command. The shell reads the input after
pressing Enter key.
Shell Types:
In UNIX there are two major types of shells:
1. The Bourne shell. In Bourne-type shell, the default prompt is the $ character.
2. The C shell. In C-type shell, the default prompt is the % character.
Variable:
A variable is a character string to assign a value. The value assigned could be a
number, text, filename, device, or any other type of data.
Naming variables:
The name of a variable can contain only letters ( a to z or A to Z), numbers ( 0 to 9)
or the underscore character ( _). By convention, Unix Shell variables would have
their names in UPPERCASE.
The variables cannot use other characters such as !,*, or - is that these characters
have a special meaning for the shell.
The following examples are valid variable names:
_ALI ,TOKEN_A, VAR_1 ,VAR_2
Following are the examples of invalid variable names:
2_VAR, -VARIABLE ,VAR1-VAR2, VAR_A!
The syntax of defining a variable is of the form:
variable_name=variable_value
For example:
TEST="Unix Programming"
Functions: Shell functions are used to specify the blocks of commands that may
be repeatedly invoked of different stages of execution to perform a certain task.
They are similar to subroutines, procedures, and functions in other programming
languages.
The formal definition of a shell function is as follows:
function_name ()
{
statements ;
}
A function binds a name to the block of statements that composes the body of
the function. The ( and ) characters are required at the function definition.
Ex:
$ function greetings()
{
echo “Hello World”
}
Output:
$ greetings
Hello World
To invoke a function, only its name is required, thus typing
$ greetings on the command line executes the greetings() function
Parameterized Function: A function with parameters is said to be a
parameterized function.
Ex:$ function add()
{
a=$1
b=$2
add=$((a+b))
echo add
}
Output:$ add 3 4
7
Test command: The shell provides the "test" command to check a condition. It
returns zero if the condition is true and non-zero if the condition is false. The test
command provides a mechanism for checking file attributes and performing string
and numeric comparisons.
It has the general syntax
test condition
For example the test condition to check if a file exists is
test -f file
Test is most often used with if statements and loops.
Decision Making
Unix Shell supports conditional statements which are used to perform different
actions based on different conditions. The following two decision making
statements are:
The if...else statements
The case...esac statement
The if...else statements:
If else statements are useful decision making statements which can be used to
select an option from a given set of options.
Unix Shell supports following forms of if..else statement:
if...fi statement: The if...fi statement is the fundamental control statement that
allows the shell to make decisions and execute statements conditionally.
Syntax:
if [ expression ] then
Statement(s) to be executed if expression is true
fi
Example:
a=10 b=20
if [ $a == $b ] then
echo "a is equal to b" fi
if [ $a != $b ] then
echo "a is not equal to b" fi
This will produce following result:
a is not equal to b
if...else...fi statement :The if...else...fi statement is the next form of control
statement that allows Shell to execute statements in more controlled way and
making decision between two choices.
Syntax:
if [ expression ] then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
Example:
a=10 b=20
if [ $a == $b ] then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
This will produce following result:
a is not equal to b
if...elif...else...fi statement:
The if...elif...fi statement is the one level advance form of control statement that
allows Shell to make correct decision out of several conditions.
Syntax:
if [ expression 1 ] then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ] then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ] then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi
Example:
a=10 b=20
if [ $a == $b ] then
echo "a is equal to b"
elif [ $a -gt $b ] then
echo "a is greater than b"
elif [ $a -lt $b ] then
echo "a is less than b"
else
echo "None of the condition met" fi