17BHM304 Unix Unit V

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

FUNDAMENTALS OF UNIX SHELL PROGRAMMING

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

This will produce following result:


a is less than b
The case...esac Statement: Unix Shell supports case...esac statement to perform
a multiway branch. However, this is not always the best solution, especially when
all of the branches depend on the value of a single variable. It handles exactly the
situation, and it does so more efficiently than repeated if...elif statements.
Syntax:
case word in
pattern1) Statement(s) to be executed if pattern1 matches ;;
pattern2) Statement(s) to be executed if pattern2 matches ;;
pattern3) Statement(s) to be executed if pattern3 matches ;;
esac
Example:
FRUIT="kiwi"
case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
;;
"banana") echo "I like banana nut bread."
;;
"kiwi") echo "New Zealand is famous for kiwi."
;;
esac
This will produce following result:
New Zealand is famous for kiwi.
Looping Control statements
In this all statements are executed repeatedly again and again as long as condition
is true. This is also known as repetition or iteration.
Shell allows various types of looping.
They are
• While loop
• Until loop
• For loop
While loop:
This is the pretested loop or entry controlled loop. In this first check the
condition, if that condition was true then control enter inside the loop otherwise
control transferred outside the loop.
Syntax: Ex:
while [ condition ] while [ i –le 10 ]
do do
Statements echo “$i” done
done i =` expr $i + 1 `
done
until loop:
This is also pretested loop or entry controlled loop. In this first check the
condition, if that condition was false then control enter inside the loop otherwise
control transferred outside the loop.
Syntax: Ex:
until [ condition ] until [ i -ge 10 ]
do do
Statements echo “$i” done
done i =` expr $i + 1 `
done
for loop:
This is a fixed execution loop. This loop is allow to execute list of statements
certain period of time.
Syntax:
for variable in value1 value2 value3 ….. value n
do
statements
done
Ex: for i in 1 2 3 4 5
do
echo $i
i=` expr $i + 1 `
done

TEXT PROCESSING USING VI EDITOR:


Vi stands for visual editor. Vi editor is a full screen text editor that comes
with nearly every unix system. It allows the user to have a look at one screen full
of information at a time and edit on the entire screen full of information full of
information. This facility provides a better picture and feeling of the file of the
user and such vi editor is referred to as visual editor. Three modes of the vi
editor . the vi editor works in three modes they are
1. The command mode
2. Input mode or insert mode
3. Ex mode
1. THE COMMAND MODE:
This is a mode into which the user gets into as soon as he enters into the vi
environment from the shell using the vi command.
SYNTAX: $vi
In this command all the keys pressed by the user or interpreted to be the editor
command. The keys that are pressed in the mode or not displayed on the screen .
the user can get into the input mode by giving any one of the command.
O,O,r,R,i,I,s,S,a,A
where o,O - stands for opening a line
r,R – stands for replacement
i,I – stands for insertion
s,S - stands for substation
a,A – stands for appending
The control can be brought back to the command mode by using the escape key.

2. INPUT MODE OR INSERT MODE:


The insert mode is used for entering text while in insert mode every key
pressed is displayed on the screen and entered into your screen. This mode
permits a insertion of new text, appending to the existing new text and
replacement of text.
3. THE EX MODE:
The third mode called ex mode is used to execute additional functions such
as searching, global replacement, manipulation of multiple files and many more.
This mode is also known as last line mode that allows the user to use the
commands in the bottom line of the vi screen from the command mode. The user
can get into the ex mode by typing the : (colon).

Unix File Permission: File ownership is an important component of UNIX that


provides a secure method for storing files. Every file in UNIX has the following
attributes:
 Owner permissions: The owner's permissions determine what actions the
owner of the file can perform on the file.
 Group permissions: The group's permissions determine what actions a user,
who is a member of the group that a file belongs to, can perform on the file.
 Other (world) permissions: The permissions for others indicate what action all
other users can perform on the file.
The Permission Indicators: While using ls -l command it displays various
information related to file permission as follows:
$ls -l /home/amrood
-rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile
drwxr-xr--- 1 amrood users 1024 Nov 2 00:10 mydir
Here first column represents different access mode ie. permission associated with
a file or directory. The permissions are broken into groups of threes, and each
position in the group denotes a specific permission, in this order: read (r), write
(w), execute (x):
 The first three characters (2-4) represent the permissions for the file's owner.
For example rwxr-xr-- represents that onwer has read (r), write (w) and execute
(x) permission.
 The second group of three characters (5-7) consists of the permissions for the
group to which the file belongs. For example -rwxr-xr-- represents that group has
read (r) and execute (x) permission but no write permission.
 The last group of three characters (8-10) represents the permissions for
everyone else. For example -rwxr-xr-- represents that other world has read (r)
only permission.
File Access Modes: The permissions of a file are the first line of defense in the
security of a Unix system. The basic building blocks of Unix permissions are the
read, write, and execute permissions, which are described below:
1. Read: Grants the capability to read ie. view the contents of the file.
2. Write: Grants the capability to modify, or remove the content of the file.
3. Execute: User with execute permissions can run a file as a program.
Directory Access Modes: Directory access modes are listed and organized in the
same manner as any other file. There are a few differences that need to be
mentioned:
1. Read: Access to a directory means that the user can read the contents. The
user can look at the filenames inside the directory.
2. Write: Access means that the user can add or delete files to the contents of the
directory.
3. Execute: Executing a directory doesn't really make a lot of sense so think of
this as a traverse permission. A user must have execute access to the bin
directory in order to execute ls or cd command.
Changing Permissions: To change file or directory permissions, you use the
chmod (change mode) command. There are two ways to use chmod: symbolic
mode and absolute mode.
Using chmod in Symbolic Mode: The easiest way for a beginner to modify file or
directory permissions is to use the symbolic mode. With symbolic permissions you
can add, delete, or specify the permission set you want by using the operators in
the following table.
Here's an example using testfile. Running ls -1 on testfile shows that the file's
permissions are as follows:
$ls -l testfile -rwxrwxr-- 1
amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on testfile,
followed by ls -l so you can see the permission changes:
$chmod o+wxtestfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod g=r-x testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Here's how you could combine these commands on a single line:
$chmod o+wx,u-x,g=r-x testfile $ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Using chmod with Absolute Permissions: The second way to modify permissions
with the chmod command is to use a number to specify each set of permissions
for the file. Each permission is assigned a value, as the following table shows, and
the total of each set of permissions provides a number for that set.
Here's an example using testfile. Running ls -1 on testfile shows that the file's
permissions are as follows:

Changing Owners and Groups: While creating an account on Unix, it assigns a


owner ID and a group ID to each user. All the permissions mentioned above are
also assigned based on Owner and Groups. Two commands are available to
change the owner and the group of files:
1. chown: The chown command stands for "change owner" and is used to change
the owner of a file.
2. chgrp: The chgrp command stands for "change group" and is used to change
the group of a file.
Changing Ownership: The chown command changes the ownership of a file. The
basic syntax is as follows:
$ chown user filelist
The value of user can be either the name of a user on the system or the user id
(uid) of a user on the system.
Following example: $ chown amrood testfile
Changing Group Ownership: The chrgp command changes the group ownership
of a file. The basic syntax is as follows: $ chgrp group filelist The value of group
can be the name of a group on the system or the group ID (GID) of a group on the
system. Following example: $ chgrp special testfile

You might also like