Sunteți pe pagina 1din 19

Unix Programming Lab

Amity University Rajasthan


Amity School of Engineering
UNIX PROGRAMMING LAB
INDEX

S. No. Date Lab Assignment Page No. Remark

Studying of commands of
1
Unix

2 Studying the features of Unix

3 Studying the modes of Unix

Studying some more


4
commands of Unix

5 Shell scripting Programs

1
Unix Programming Lab

Assignment -1

Explain the following commands:-

1. ls List a directory’s content , including the files and subdirectories

ls dirname

2. rm Removes a file

rm filename

3. rmdir Removes a directory that is empty

rmdir dirname

4. cat Concatenate or display file

cat filename

5. mkdir Make a new directory

mkdir dirname

6. touch Changes a file’s name and date stamp

7. kill Terminates a running process

8. pr Format a specified file before printing or viewing

2
Unix Programming Lab

9. cp Copy file from one directory to another

cp source filename destination filename

10. mv Move or rename file

mv source file destination file

11. cd Change directories

12. who To see who is log on to the unix

13. whoami Specified case of who , but don’t display all the users who have logged

into the system currently.

14. sort Sorts and merges multiple files.

15. wc word count , counts the no. of lines ,bytes or words.

16. tail displays the last line of the file.

17. head displays the first part of a file.

3
Unix Programming Lab

18. diff Compares two text files and show differences

19. comm Compares sorted files and show differences

20. ln Creates a link to a file.

21. man Displays manual or documentation for commands.

22. pwd Displays current path.

23. uniq Select unique lines or rows.

24. grep Select lines or rows that match a specific pattern

25. cal shows the system calendar

26. date Displays the system date.

27. echo Displays the specified arguments on the o/p device.

28. expr Gives the result of expression.

29. pr Displays the format file.

4
Unix Programming Lab

30. clear Clears the screen.

31. chmod Changes security mode of a file or directory

( r : read ; w : write ; x : execute )

32. file Determines file types ( e.g. shell script , executable etc.)

33. pine Send and receive e-mail and news.

34. tr Replace specified characters , like a translation filter.

5
Unix Programming Lab

Assignment -2

Q1. Explain the salient features of UNIX operating sytem?

Ans. UNIX is the multi user, multi tasking OS with built-in


Networking functions.

Multi User :- Many people Simultaneously access and share


the resources.

Multitasking:- A user executes more than one program at a


time.

Portable:- UNIX is also a portable OS. Its portability means


it can be used in a variety of computing environments. It also
runs on the internet , regulating popular programmed like
FTP , an internet protocol used for sending filters and telnet.

It can be used on dedicated server in a server based n/w.

6
Unix Programming Lab

Q2. What are the different ways of establishing a logical


connection with the host machine?

Ans. There are several types of terminals that can be attached to


the host .These are :-

1. Dumb Terminals :- These terminals consists of a keyboard


and a display unit with no memory or disk of its own .These
can never act as independent machine.

2. Terminal Emulation:- A PC has its own microprocessor,


memory and disk drivers. By attaching this PC, we can
emulate it to work as if it is a dumb terminal. At such times ,
the memory and the disk are not in use and the PC can’t carry
out any processing jobs to the host machine.

3. Dial in Terminal:- These terminals use telephone lines to


connect with the host machine . It is necessary to attach a unit
called modem to the terminal as well as to the host.

7
Unix Programming Lab

Q3. Which different types of shells are available in UNIX?

Ans. The different types of shells are:-

1. Bourne Shell :- Among all ,Steve Borne’s creation , known


after him as Bourne Shell is the most popular. Probably that’s
why it is bundled with every UNIX system .So it is very
popular .It is the shell used by many UNIX users.

2. C – Shell :- This shell is a bit with those who are seriously


involved in UNIX programming . It was created by Bill Joy.
It has two advantages over the Bourne Shell:-
a :- It allows aliasing of commands i.e. you can decide what
name you want to call command by.
b:- If you want to save even more on the typing work .C –
Shell has a command history feature.

3. Korn Shell :- The not so widely used this shell is very


powerful and is superset of Bourne-Shell . It offers a lot of
capabilities and is decidedly more efficient than the others.

8
Unix Programming Lab

Q4. Explain the UNIX architecture in brief. What is the role


of kernel and Shell in UNIX?

Ans. The functioning of UNIX is named in three levels. On the


outer most reside the application programs and other utilities,
which speak our language. At the heart of UNIX, is the KERNEL,
which interacts with the actual hardware in machine language.
The streamlining of these two modes of communication is done by
the middle layer called as the shell.

The shell or command interpreter as it is called, is the mediator,


which interprets the command that we give and then convey them
to the kernel, which ultimately executes them. We can imagine the
kernel as a monarch which has command of everything.

The kernel has the several functions. It manages files, carries out
all the data transfer b/w the file system and hardware. It manages
memory. It also handles any interrupts issued, as it is the entity that
has direct dealings with the h/w. The kernel program is usually
stored in a file ‘UNIX’, whereas the shell program is in a file
called ‘sh’.

9
Unix Programming Lab

Q5. Explain the four blocks of UNIX file system.


Ans. All belonging to the file system are logically divided into
four parts:-
1. The Boot Block:- This represents the beginning of the file
system . It contains a program called ‘boot strap loader’. This
program is executed when we ‘boot’ the host machine.
Although only one boot block is needed up the system. All
file systems contain one boot block.

2. Super Block: - The super block describe the state of the file
system – how large it is, how many maximum files it can
accommodate, how many more files can be created.

3. The Inode Table :- We know that all entities in UNIX are


treated as files . The information related to all the files is
stored in an Inode table on the disk. Each entity is made up
of 64 bytes, and contains the relevant details for that file.
The details are :-
a) Owner of the file.
b) Group to which the owner belongs.
c) Types of files.
d) File access permission.
e) Date and time of last access.
f) Date and time of last modification.
g) No. of links to the file.
h) Size of file.
i) Address of blocks where the file physically present.

4. Data Block:- These contain actual file contents . An


allocated block can belong to only one file in the file system.
This block can’t be used for storing any other files contents
unless the file to which it originally belonged is deleted.

10
Unix Programming Lab

Assignment -3,4
Q1. Explain the three modes of operation of vi-editor.

Ans. Three modes of operation of “vi” editor are:-

1. Command Mode: - In this mode all the keys pressed by the


user are interpreted to be the editor commands e.g. If we hit a
key the cursor is moved one position to the left. In command
mode the keys that are hit are not displayed on the screen.

2. Insert Mode: - This mode permits the insertion of text ,


editing of existing text or replacement of existing text.
Command mode is changed to insertion mode using
appropriate commands.

3. The ex-command Mode: - This mode permits us to give


commands at the command line. The bottom line of the vi-
screen is called the command line. “vi” uses the command
line to display message and commands. All commands
entered in the ex-command mode are displayed in the
command line.

Q2. Which vi-command will use to perform following cursor


move:-

1. Top of the screen H


2. Bottom of the screen L
3. Beginning of the current line 0 (zero)
4. End of current line $
5. beginning of the file G
6. Go to the nth line in the file nG

11
Unix Programming Lab

Q3. How will you perform the following deletions in vi editor?

1. Character of the cursor x


2. One word from the current position dw
3. Current Line dd
4. Next four lines 4 dd
5. Line to the left of the cursor d0
6. Line to the right of the cursor d$

Q4. What is the difference between following vi commands?

:w writes buffer to the file .

:q quit vi whether or not changes made to the buffer


were written to the a file

:wq quit vi with writing into the buffer.

Zz Writes the buffer to the file and quits vi.

Q5. (a) What is the difference between Yank and delete?

Ans. Unlike delete when we yank object (word , line etc.) vi


copies these objects into without removing them from
the file. These objects can then be pasted to another
location in a file or even to different filter.

12
Unix Programming Lab

(b) How could you replace all occurrences of the word


‘str1’ with the word ‘str2’?

Ans.

Command Function
:s/str1/str2 Replaces first occurrence of str1
with str2 in current line.

:s/str1/str2/g Replaces all occurrences of str1


with str2 in current line.

:m , n s/str1/str2/g Replaces all occurrences of str1


with str2 from line m to line n.

:1,$ s/str1/str2/g Replaces all occurrences of str1


with str2 from line 1 to current
line.

:1,$ s/str1/str2/g Replaces all occurrences of str1


with str2 from current line to
end of file.

(c) What is the effect of following commands? Why these


commands should ideally be stored?

Set nu set display of line numbers on.


Set ai set auto indent on.
Set nonum set display of line numbers off.
Set ic ignore case when searching a pattern

13
Unix Programming Lab

Assignment -5

Shell Script 1:- To add, subtract, multiply or divide two


numbers by a menu driven program
echo enter two numbers

read a b

echo enter the choice

echo 1.Add 2.Subtract 3.Multiply 4.Divison

read choice

case $choice in
1)
result=$(($a + $b))
echo "The answer is" $result
;;
2)
result=$(($a - $b))
echo "The answer is" $result
;;
3)
result=$(($a * $b))
echo "The answer is" $result
;;
4)
result=$(($a / $b))
echo "The answer is" $result
;;
esac

14
Unix Programming Lab

Shell Script 2:-To find greatest of three numbers

read a b c
if [ $a –gt $b ]
then
if [ $a –gt $c ]
then
echo $a is greatest
else
echo $c is greatest
fi
else
if [ $b –gt $c ]
then
echo $b is greatest
else
echo $c is greatest
fi

15
Unix Programming Lab

Shell Script 3 :-To print Fibonacci series upto a value entered


by user
echo "Enter How many numbers:"

read num

num1=0

num2=1

echo "Fibonacci series:"

echo $num1

echo $num2

count=2

while [ $count -le $num ]

do

num3=`expr $num1 + $num2`

echo $num3

16
Unix Programming Lab

num1=$num2

num2=$num3

count=`expr $count + 1`

done

17
Unix Programming Lab

Shell Script4:- To check whether the given number is prime or


not

echo enter a number

read num

i=2

while [ $i -lt $num ]

do

if [ `expr $num % $i` -eq 0 ]

then

echo "$num is not a prime number"

echo "Since it is divisible by $i"

exit

fi

i=`expr $i + 1`

done

echo "$num is a prime number "

18
Unix Programming Lab

19

S-ar putea să vă placă și