Вы находитесь на странице: 1из 24

Program 1

Aim :- Introduction of UNIX.

The Unix operating system is a set of programs that act as a link between the computer and
the user.

The computer programs that allocate the system reSource Codes and coordinate all the
details of the computer's internals is called the operating system or the kernel.

Users communicate with the kernel through a program known as the shell. The shell is a
command line interpreter; it translates commands entered by the user and converts them into
a language that is understood by the kernel.

Unix was originally developed in 1969 by a group of AT&T employees Ken


Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna at Bell Labs.

There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix
and BSD are a few examples. Linux is also a flavour of UNIX which is freely
available.

Several people can use a UNIX computer at the same time; hence UNIX is called a
multiuser system.

A user can also run multiple programs at the same time; hence UNIX is a
multitasking environment.

CHARACTERISTICS OF UNIX:-
1. Portability.
2. Open system.
3. Rich and productive programming environment.
4. Communication.
5. Multi-user capability.
6. Multitasking.

1
Program 2
Aim :- Architecture of UNIX.

Fig. 1 ( Block Diagram of UNIX )

The main concept that unites all the versions of UNIX is the following four basics

Kernel The kernel is the heart of the operating system. It interacts with the
hardware and most of the tasks like memory management, task scheduling and file
management.

Shell The shell is the utility that processes your requests. When you type in a
command at your terminal, the shell interprets the command and calls the program
that you want. The shell uses standard syntax for all commands. C Shell, Bourne
Shell and Korn Shell are the most famous shells which are available with most of the
Unix variants.

Commands and Utilities There are various commands and utilities which you can
make use of in your day to day activities. cp, mv, cat and grep, etc. are few
examples of commands and utilities. There are over 250 standard commands plus
numerous others provided through 3rd party software. All the commands come along
with various options.

Files and Directories All the data of Unix is organized into files. All files are then
organized into directories. These directories are further organized into a tree-like
structure called the file system.

2
UNIX SYSTEM DIRECTORIES:-
The standard system directories are shown below. Each one contains specific types of files.
The details may vary between different UNIX systems, but these directories are command to
all.

/ROOT

/bin /dev /etc /home /lib /tmp /user, kernel file

UNIX GROUPS:-
A group is a collection of users who can share files and other system resources.
For example, users who working on same project could be formed into a group. A group is
traditionally known as UNIX group. Each group must have a name, a group identification
number (GID), a list of users name that belong to the group. A GID number identifies the
group internally to the system. When several people have access to the system, the
administrator must manage the users. To do so, he must know the common commands and
files to be configured. The important files are:

/etc/passwd file

/etc/group file

Types of group:-
There are two types of groups:

1. Primary Group: This group specifies a group that the operating system assigns to
files that are created by user. Each user must belong to primary group.

2. Secondary Group: This group specifies one or more groups to which a user also
belongs. Users can belong to up to 15 secondary groups.

The group command lists the groups that a user belongs to. A user can have only one
primary group at a time. However, a user can temporarily change the users primary group,
with the newgrp command.

3
Program 3
Aim :- UNIX Commands
1) echo :- To print something
a) echo Hello : prints given string
b) echo $var : prints the value of var variable
2) read :- Read data form keyboard
3) cal :- Calendar
a) cal : prints current month
b) cal 3 2017 : prints 3rd month of 2017
4) date :- current date & time
5) clear :- To clear terminal
6) mkdir :- make directory
a) mkdir file1 : creates a directory named file1
b) mkdir p file1 file2 file3 : creates three directories names file1, file2, file3
c) mkdir v file1 : creates a directory named file1 and tells whether the
directory is created or not
d) mkdir p file1/file2/file3 : creates three directories in a hierarchical structure.
e) mkdir pv : combination of mkdir p & mkdir v
7) rmdir :- remove directory
a) same as mkdir (all)
8) cd :- change directory
a) cd : change directory to root
b) cd.. : change directory to previous (parent directory)
c) cd../.. : change directory to previous to previous (grandparent)
d) cd file1 : change directory to file1
e) cd file1/file2 : change two directories simultaneously (to file2)
9) pwd :- present working directory ; tells the name of current directory

File Commands
10) touch :- create empty file
a) touch file1 : creates an empty file named file1

11) cat :- can perform multiple tasks on a file (creation, insertion of data, gets
file data etc.)
a) cat > file1 : creates an empty file or used to insert data in already existing
file
b) cat < file1 : displays the data of file1.
c) cat >> file1 : insert data into file1 without over-writing
d) cat file1 > file2 : move data of file2 in file1
e) cat file1 >> file2 : move data of file2 in file1 (without over-writing)

4
12) cp :- copy
a) cp file2 file1 : copy data of file2 in file1 (over-writes the data of file1)

13) mv :- move/cut
a) mv file2 file1 : moves/cut data of file2 and paste it in file1 (over-writes)

14) chmod :- change mode; used to change permissions of any file

u :- User r :- Read (4) + :- add permission


g :- Group w :- Write (2) - :- remove permission
o :- Other x :- Execution (1) = :- remove all current permissions
a :- All No permission (0) and adds new one

a) chmod u+r file1 : give read permission to user for file1


b) chmod g-w file1 : remove write permission of group for file1
c) chmod o= x file1 : remove all existing permissions of other and give executing
permission
d) chmod a+r file1 : give read permission to all for file1
e) chmod ug-r file1 : remove read permission of user & group for file1
f) chmod 000 file1 : remove all permission of user , group & other for file1
g) chmod 421 file1 : give only read to user, write to group and executing to other
for file1
h) chmod 777 file1 : give all permissions to all for file1.

Comparison commands

15) cmp :- used to compare files


a) cmp file[12] : compare file1 with file2

16) comm :- used to find common b/w two files


a) comm file[12] : tells the common part of file1 and file2
b) comm -1 file[12] : tells common removing first column
c) comm -13 file[12] : tells common b/w file1 and file2 removing 1st & 3rd
column

17) diff :- used to get difference b/w files


a) diff file[12] : tells difference b/w file1 and file2

18) -lt ( < ) :- less than

19) -gt ( > ) :- greater than

5
20) -le ( <= ) :- less than equal to

21) -gt ( >= ) :- greater than equal to

22) -eq ( = ) :- equal

23) wc :- word count


a) wc C : number of characters
b) wc c : number of bytes
c) wc w : number of words
d) wc l : number of lines

List Commands

24) ls :- list
a) ls : list of files with directories of current directory
b) ls l : list of attributes of files with their directories
c) ls R : list of all the directories and their sub-directories
d) ls hl : to get value in bytes/kilo-bytes instead of blocks
e) ls tl : sorting the list according to time
f) ls rtl : revers of time
g) ls sl : sorting the list according space
h) ls rsl : revers of space
i) ls chap* : all directories start with chap ( * = meta character )

Filters

25) grep :- checks a certain pattern in file/files


a) grep Mar : all files having Mar in their details
b) ls l | grep Mar : list of those files having Mar in their details
( | pipeline symbol )
c) grep v : prints those files which does not matches the pattern
d) grep n : prints matched lines and its line number
e) grep l : prints only name of files with matching lines
f) grep c : prints only count of matching lines
g) grep I : makes case insensitive

26) sort :- Arranges line of text alphabetically/numerically


a) sort file1 : sort text line of file1 in alphabetically order
b) sort n : sort numerically
c) sort r : sort in reverse order
d) sort f : sorts upper and lower case together
e) sort +x : Ignores first x fields ( x = any number )
f) sort +xn : sorts numerically ignoring first x lines

6
27) pr :- Paginating Files ; prepares a file for printing adding suitable headers,
footers
a) pr file1 : prepares file1 for printing
b) pr k file1 : prints file1 in k number of columns ( k = any number )
c) pr l file1 : shows number of lines
d) pr on file1 : offset lines by n spaces ; increases left margin space of page by n.

28) head :- to display first lines ( Default = First 10 lines )


a) head file1 : displays first 10 lines of file1
b) head n3 file1 : displays first 3 lines of file1
c) head -3 file1 : displays first 3 lines of file1

29) tail :- to display last lines ( Default = Last 10 lines )


a) tail file1 : displays last 10 lines of file1
b) tail -3 file1 : displays last 3 lines of file1

30) Uniq :- To locate repeated and non-repeated lines in a file


a) uniq file1 : no repetition of lines in file1
b) uniq u file1 : select non-repeated lines
c) uniq c file1 : count frequency of occurrence

31) tr :- Translating Characters


a) tr |/ ~- < file1 : translate | with ~ and / with - in file1
b) tr d | < file1 : deleting | from file1
c) tr s | < file1 : compressing | in file1

Terminal Commands

32) who :- name of all users, their terminals, their access time

33) whoamI :- Self-user name

34) ps :- process status


a) ps : shows all running processes
b) ps f : shows all attributes of processes
c) ps e/ ps A : System processes + User Processes
d) ps u user_name : Process status of particular user
e) ps t terminal_name : Process status of particular terminal

7
Program 4
Aim :- Program to use Shell Script.

Source Code :-
echo "Welcome user"
echo "Enter user-name to Login :"
read user
echo "$user Login successful"

Output :-

8
Program 5
Aim :- To check whether the number is between 10 and 20 or not.

Source Code :-
echo "Enter any number"
read num
if [ $num -lt 10 ]
then
echo "Number is less than 10"
elif [ $num -gt 20 ]
then
echo "Number is greater than 20"
else
echo "Number is between 10 and 20"
fi

Output:-

9
Program 6
Aim :- To find greatest among 3 numbers using if-else statements.

Source Code :-
echo "Enter 3 numbers"
read num1
read num2
read num3
if [ $num1 -gt $num2 ]
then
if [ $num1 -gt $num3 ]
then
echo "$num1 is greatest"
else
echo "$num3 is greatest"
fi
else
if [ $num2 -gt $num3 ]
then
echo "$num2 is greatest"
else
echo "$num3 is greatest"
fi
fi

Output :-

10
Program 7
Aim :- Use of word count (wc) statement and pipeline symbol.

Source Code :-
echo "Enter any character"
read char
if [ ` echo $char | wc -c ` -eq 2 ]
then
echo "Single character"
else
echo "Multiple Characters"
fi

Output :-

11
Program 8
Aim :- To check whether the word is in lower case or upper case.(Using Case Statement)

Source Code :-
echo "Enter any character"
read var
case $var in
[a-z])
echo "Letter is in lower case";;
[A-Z])
echo "Letter is in Upper case";;
[0-9])
echo "It is a digit";;
?)
echo "Special Symbol";;
*)
echo "Too many characters";;
esac

Output :-

12
Program 9
Aim :- To check whether the word is Vowel or not. (Using Case Statement)

Source Code :-
echo "Enter a word "
read word
case $word in
[aeiou]*|[AEIOU]*)
echo "Starts with a vowel";;
[0-9]*)
echo "Starts with a digit";;
???)
echo "3 letter word";;
*)
echo "Does not start with a vowel";;
esac

Output:-

13
Program 10
Aim :- Use of while loop.

Source Code :-
echo "Enter any number to get its Table"
count=1
read num
temp=` expr $num `
while [ $count -lt 10 ]
do
num=` expr $num + $temp `
echo "$num"
count=`expr $count + 1 `
done

Output :-

14
Program 11
Aim :- Use of Until loop.

Source Code :-
count=1
until [ $count -ge 10 ]
do
echo $count
count=` expr $count + 1 `
done

Output :-

15
Program 12
Aim :- Program to find item.

Source Code :-

for item in *
do
if [ -f $item ]
then
echo $item
fi
done

Output :-

16
Program 13
Aim :- Use of break statement.

Source Code :-
count=1
while [ $count -le 10 ]
do
if [ $count -eq 6 ]
then
break
fi
echo $count
count=` expr $count + 1 `
done
echo "We are out of loop"

Output :-

17
Program 14
Aim :- Use of continue statement.

Source Code :-
count=0
while [ $count -lt 10 ]
do
count=` expr $count + 1 `
if [ $count -eq 5 ]
then
continue
fi
echo $count
done
echo "We are out of loop"

Output :-

18
Program 15
Aim :- To find the sum of 10 natural numbers.

Source Code :-
count=1
sum=0
temp=0
while [ $count -le 10 ]
do
temp=` expr $sum `
sum=` expr $sum + $count `
echo "$temp + $count = $sum"
count=` expr $count + 1 `
done

Output :-

19
Program 16
Aim :- To find the factorial of any number.

Source Code :-
echo "Enter any number to get its factorial"
read num
i=1
fact=1
while [ $i -lt $num ]
do
i=` expr $i + 1 `
fact=` expr $fact \* $i `
done
echo $fact

Output :-

20
Program 17
Aim :- To find the reverse of any number.

Source Code :-
echo "Enter a number to get its revers"
read num
sum=0
rev=0
temp=` expr $num `
while [ $num -gt 0 ]
do
rem=` expr $num % 10 `
rev=` expr $rev \* 10 + $rem `
num=` expr $num / 10 `
sum=` expr $num + $rem `
done
echo "Reverse of $temp is = $rev"

Output :-

21
Program 18
Aim :- Program to check palindrome number.

Source Code :-
echo "Enter a number to ckeck palindrome"
read num
sum=0
rev=0
temp=` expr $num `
while [ $num -gt 0 ]
do
rem=` expr $num % 10 `
rev=` expr $rev \* 10 + $rem `
num=` expr $num / 10 `
sum=` expr $num + $rem `
done
echo "Reverse of $temp is = $rev"
if [ $temp -eq $rev ]
then
echo "Palindrome"
else
echo "Not Palindrome"
fi

Output :-

22
Program 19
Aim :- Program to use and (-a).

Source Code :-
echo "Enter three numbers"
read a
read b
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is greatest"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "$b is greatest"
else
echo "$c is greatest"
fi

Output :-

23
Program 20
Aim :- Program to check String.

Source Code :-
str1="Hello User"
str2="Welcome"
str3=""
[ "$str1" = "$str2" ]
echo $?
[ "$str1" != "$str2" ]
echo $?
[ -n "$str1" ]
echo $?
[ -z "$str3" ]
echo $?

Output :-

24