assignment b43 kuvempu university

24

Click here to load reader

Upload: siddharth-jain

Post on 19-Jul-2016

23 views

Category:

Documents


3 download

DESCRIPTION

KUniversity assignments

TRANSCRIPT

Page 1: Assignment b43 Kuvempu University

Subject: UNIX & Shell Programming Subject Code: BSIT - 43

Assignment: TB (Compulsory)

PART - A

1. What are the different standard given by POSIX? What is POSIX?

Ans: The different standard given by POSIX are: -

POSIX.1

POSIX.1B

POSIX.1C

POSIX.2

POSIX:2001

POSIX:2004

POSIX:2008

POSIX is the Portable Operating System Interface, the open operating interface standard accepted

world-wide. It is produced by IEEE and recognized by ISO and ANSI.

2. On which variable terminal setting is done? Name any three terminal setting keys.

Ans: Using STTY

three terminal setting key are:-

quit, kill, erase.

The environment variable TERM is used to identify the type of terminal (or terminal emulator) to the

curses library and programs that use the curses library to display text. It is also associated with the

TERMINFO and (in archaic systems) TERMCAP environment variables

3. Explain key features of UNIX.

Ans: The UNIX system is supported by the file and the process. Directories and devices are treated

as files and there are many text manipulation tools to edit these files. When a file is executed as a

program, it is called a process. There are tools to control processes like sending a processes into

background or even terminating it.

UNIX is a multitasking operating system, which allows the computer to run several programs at the

same time. By going quickly from one task to another and performing a little bit of each task every

Page 2: Assignment b43 Kuvempu University

time, the operating system gives an impression of doing many things at the same time. UNIX uses

this technique of time-sharing. UNIX is also able to interact with more than one user at a time. This

feature of UNIX makes it a multitasking system.

4. What is script command, with command explain how to create script file of a session?

Ans: A "script" is typically a "screen capture" of commands being executed in the UNIX operating

environment. Creating a script file generally means that you are trying to capture in a text file, the

interaction between the operating system (UNIX) and the user. Most often this text file or "script" is

then printed and turned in with a program assignment so that the instructor can see that the program

executed correctly.

To create a basic script file, do the following:

1. Turn the script ON (The default name of the file the script will record in is always named typescript unless you name it something else as shown later) at the command linescript. You will see a message back stating: Script command is started. File is typescript.

2. Execute the command sequence that you want to capture (typically this means "run your program"). java Birthday

3. Turn off the script exit

4. Now you should see a message: Script command is complete. File is typescript.5. Now you may print the file containing your script, typescript, using the a2ps command, or

you may want to look at it on the screen using the more command.a2ps    typescript    to print the script filemore    typescript    to view the contents of the script file on the screen

6. That's all!

Here is an example of a script file creation:/seu/cs/home/user/l/lbaker/www/HTML/cosc1123 # script output.txt Script started, file is output.txt/seu/cs/home/user/l/lbaker/www/HTML/cosc1123 # java Birthday Happy Birthday to you,Happy Birthday to you,Happy Birthday dear Dr. CamdenHappy Birthday to you!/seu/cs/home/user/l/lbaker/www/HTML/cosc1123 # exitScript done, file is output.txt/seu/cs/home/user/l/lbaker/www/HTML/cosc1123 # more output.txtScript started on Tue 26 Aug 2008 01:43:55 PM CDT/seu/cs/home/user/l/lbaker/www/HTML/cosc1123 # java BirthdayHappy Birthday to you,Happy Birthday to you,

Page 3: Assignment b43 Kuvempu University

Happy Birthday dear Dr. CamdenHappy Birthday to you!/seu/cs/home/user/l/lbaker/www/HTML/cosc1123 # exit

Script done on Tue 26 Aug 2008 01:44:02 PM CDT/seu/cs/home/user/l/lbaker/www/HTML/cosc1123 #

5. What happens if a directory permission charged?

Ans: Every time a file or a directory is created, default permissions are established for it. These

default permissions are initially assigned either by the operating system or the program being run.

Setting default permissions saves us the trouble of specifying permission codes explicitly every time

a file or directory is created. The operating system assigns the default permission values of 777 for

executable files and 666 for all other files.

To put further restrictions on the permissions assigned by a program when it creates a file or

directory, a user mask is specified with the umask command. The user mask is a numeric value that

determines the access permissions when a file or directory is created. Consequently, when a file or

directory is created, its permissions are set to the permissions specified by the creating program

minus the permission values denied by the umask value.

Example

The command umask without arguments gives the octal value of the user mask.

$ umask

022

6. How do you yank and paste lines?

Ans: The command 'Y' or 'yy' copies (yanks) one or more lines. To copy one line, two lines, 10 lines,

and all lines to the end of the file, respectively:

Y 2Y 10Y yG

to paste the text contained in the buffer above (uppercase P) or below the current cursor position

(lowercase p), respectively:

P p

it is also possible to yank text within a line. The following commands yank text from the current

cursor position to the end of the word and the end of the line, respectively:

yw y$

The same commands paste the text within a line. Lower case p pastes after the cursor position and

upper case P pastes before.

Page 4: Assignment b43 Kuvempu University

Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any

other commands prior to pasting as this will empty the buffer.

7. List out the different attributes of a file.

Ans: Listing File Attribute:-

-rw-r----- 1 sibnas sibgrp 4562 sep 22 10:30

-rw-r----- 1 sibnas sibgrp 8976 sep 22 10:30

-rw-r----- 1 sibnas sibgrp 10234 sep 22 10:30

-rw-r----- 1 sibnas sibgrp 4352 sep 22 9:30

-rw-r--r--r- 1 sibnas sibgrp 87632 aug 2 11:30

-rw-r--r--r- 1 sibnas sibgrp 6528 june 20 10:30

-rw-r--r--r- 1 sibnas sibgrp 9234 may 22 10:00

File type link User id group id file size date &

Count in bytes of modi.

8. What are the different ways of coming out of vi after saving the file?

Ans: There are three different ways to quit vi after saving the file:

a. :wq

b. :x

c. ZZ

9. Write general syntax of CASE statements.

Ans: The case statement compares word with pattern; if they match, the shell runs the command on

the first line. Otherwise the shell checks the remaining patterns, one by one, until it finds one that

matches the word; it then runs the command on that online.

Syntax for case statement:-

#!/bin/sh

Set „date‟

Case $1 in

Fri) echo “thank goodness it‟s Friday!”;;

Sat | Sun) echo “you should not work on week-ends”;

Echo “log off and go home!”;;

*) echo “it is not yet the weekend.”;

Page 5: Assignment b43 Kuvempu University

Echo “get to work! “;;

esac

10. What is a process? Name two important attributes of a process.

Ans: Process is born when a program starts execution and exists as long as the program is running.

After execution the process is usually the name of the program being executed.

The two important attribute of a process are:-

1) The process-id(PID).Each process is identified by a unique number called the process-id which is

allotted by the kernel when the process is born.

2) The parent PID(PPID) the PID of the parent is also a process attribute. When many processes have

the same PPID it is easier to kill the parent process rather than the children separately.

PART - B

1. a) Explain layered architecture of Unix operating system. With a suitable Command

explain the interaction between Shell and Kernel.

Ans: UNIX, like other operating systems, is a layer between the hardware and the applications that

run on the computer. It has functions that manage the hardware functions that manage executing

applications. So what’s the difference between UNIX and any other operating system? Basically two

things: internal implementation and the interface that is seen and used by users.

The part of UNIX that manages the hardware and the executing processes is called the kernel. The

kernel is collection of programs written in c which directly communicate with the hardware.

Application programs communicate with the hardware by using the services of Kernel. Along with

the memory management, the kernel also schedules processes and decides their priorities.

In the UNIX system, each hardware device is viewed as a file and is called a device file .this allows

the same simple method of reading and writing files to be used to access each hardware device.

The user commands are translated in to action by the shell which acts as interpreter. The shell forms

the outer part of the operating systems and forms the interface between the user and kernel. For each

user logged in, there is shell in action. When a command is given by the user, it is examined by the

shell and communicated to the kernel for execution.

Application portability is the ability of a single application to be executed on various types of

computer hardware without being modified. This can be achieved if the application uses the UNIX

interface to manage its hardware needs.

Page 6: Assignment b43 Kuvempu University

b) Explain uname command with different options.

Ans: uname – displays the name of the operating system

Syntax

uname [options]

Examples:

Displays all the information

$uname –a

SCO_SV sco5 3.2 5.0.5 i386

Displays the machine’s node name in the communication network.

$uname-n

Sco-5

Displays the operating system release.

$uname-r

3.2

Displays the name of the operating system

$uname –s

SCO_SV

Displays the operating system version

$uname –v

5.0.5

Displays the information about system name, node name, operating system

release number, kernel ID, processor type, serial number, number of users

license, OEM number, origin number and number of CPUs.

2. a) Explain the advantages of ispell, list out the basic commands used in ispell.

Ans: Ispell is a spell-checking program available for many UNIX implementations. To use it, at your UNIX shell prompt, enter:ispell filenameReplace filename with the name of the file you wish to check.

Basic Ispell commands include:

r Replace the misspelled word completely.

Page 7: Assignment b43 Kuvempu University

Spacebar Accept the word this time only.

a Accept the word for the rest of this Ispell session.

i Accept the word, capitalized as it is in the file, and update the private dictionary.

u Accept the word, and add a lowercase version to the private dictionary.

0, 1, ... n Replace with the suggested word corresponding to that number.

l (the letter L) Look up words in system dictionary (controlled by the WORDS compilation option).

x Write the rest of this file, ignoring misspellings, and start the next file.

q Exit immediately and leave the file unchanged.

! Escape to the Unix shell.

Ctrl-l(the letter L)

Redraw the screen.

Ctrl-z Suspend Ispell.

? Show the help screen.

b) Explain unix file system and give the difference between relative and absolute

pathname.

Ans: UNIX file system can be defined as belonging to one of four possible types:-

Ordinary files: ordinary files can contain text, data, or program information. An ordinary file cannot

Page 8: Assignment b43 Kuvempu University

contain another file, or directory. An ordinary file can be a text file or binary file. Most of the UNIX

command are binary files.

Directory files: - Directories are containers that can hold files, and other directories. A directory is

actually implemented as a file that has one line for each item contained within the directory. Each

line in a directory file contains only the name of the item, and a numerical reference to the location of

the item.

Special files: - Special files represent I/O devices like, a tty, a disk drive, or a printer. Because UNIX

treats such devices as files. Some of the commands used to access ordinary files will also work with

devices files. This allows more efficient use of software.

Links:- A link is a pointer to another file. Since a directory is a list of the names and i-numbers of

files, directory entry can be a hard link. In which the i-number points directly to another file. A hard

link to a file cannot be distinguished from the file itself.

Difference between ABSOULATE AND RELATIVE PATHNAMES

ABSOULATE PATHNAMES

RELATIVE PATHNAMES

An Absolute pathname specifies the location of a file .

Relative pathname specifies a file in relation to the current directory.

An Absolute pathname starts at the / root directory.

A Relative pathname starts from the current directory.

An Absolute pathname uses a slash (/) between each directory name in the path to indicate different

directories.

In a Relative pathname, a single dot (.) represents the current working directory and two dots (..)

represent the parent of the current working directory.

3. a) Explain how to split file into multiple files. Give suitable example.

Ans: we used “split” command to split file into multiple files.

split – split large files into “smaller files”

syntax –

split[options] filename prefix

where file name is the name of the large file to be split ,prefix is the name to be given the small

output files and options can either be excluded or can be one of the or can be one of the following:-

-l = linenumber

Page 9: Assignment b43 Kuvempu University

-b = bytes

If –l option is used, linenumber will be the number of the lines to be put in each of the smaller files

(the default is 1000). If the –b option is used, bytes will be the number of bytes to be put in each of

the smaller files.

The split command will give each output file created the name prefix with an extension attached to

the end to indicate its order

EXAMPLE:

Assuming that file.txt is 3000 lines long , it will output three files, xaa, xab, and xac, and each one

will be 1000 lines long.

$ splitfile.txt

This will output six soo-line files: fileaa, fileab, fileac,filead, fileae, and fileaf.

$split-1500 file.txt file

Assuming that file.txt has 200 kb, this will output five 40 kb files: fileaa, fileab, fileac ,filead, fileae,

and fileaf.

$ split –b 40k .txt file

b) Give the difference between Hard Link and Symbolic Link.

Ans: Soft Link:

- Soft links are links to a file but not the inode.

- Created using

ln -s file1 file2

=> ls -il

131135 lrwxrwxrwx 1 user user 5 Jul 10 09:04 file2 -> file1

131137 -rw-r--r-- 1 user user 35 Jul 10 09:03 file1

- The inode for file1 is 131137 and inode for file2 is 131135.

- If you see the permission bits, there is 'l' in the front for a soft link.

- If file1 is deleted, the link still exists. But if you try to view file2, its empty. This means that once

the main file is deleted the data is gone.

Hard Link:

- Hard links are links to inode

- Created using

ln file1 file2

Page 10: Assignment b43 Kuvempu University

=> ls -il

131136 -rw-r--r-- 2 user, user 48 Jul 10 09:27 file1

131136 -rw-r--r-- 2 user, user 48 Jul 10 09:27 file2

- The inode for file1 and file2 is the same (131136).

- If you see the output above for "ls -i", file2 does not show that it is linked to file1. In reality it is not

linked to file1 but it is linked to the inode.

- If you see that there is number '2' before the username 'user'. This shows the number of hard links to

the inode.

- If file1 is deleted, the data is not deleted. If you view file2 the data is still there. Deleting file1 only

deletes a link. The data is gone once the last hard link is deleted.

4. a) What is a process? Explain the mechanism of creation in UNIX.

Ans: A process is a collection of interrelated work tasks initiated in response to an event that

achieves a specific result for the customer of the process. Adding more specific detail to that general

definition:

That achieves a specific result:

Must deliver a specific result

this result must be individually identifiable and countable

a good process name clearly indicates the result or end state of the process

For the customer of the process:

A customer receives the result or is the beneficiary of it

the customer can be a person or an organization

customer can be identified and can pass judgment on the result and process

customer point of view helps identify and name the process accurately

Initiated in response to a specific event:

The process must be initiated in response to a specific event

multiple events can initiate a process

having an event AND a result allows the tracing of the sequence of tasks that turns the event into the

result

Work tasks:

A collection of actions, activities, steps or tasks make up a business process

a step in the initial workflow will probably be divided into more detailed steps later

Page 11: Assignment b43 Kuvempu University

A collection of interrelated:

The process steps must relate to each other

interrelationship is through sequence and flow...the completion of one step leads to (flows into) the

initiation of the next step

also interrelated by dealing with the same work item

steps related by being traceable back to the same initiation event

b) List out the navigation keys for the cursor movement.

Ans: in the command mode ,navigation keys for the movement of the cursor by characters, words

and lines are as follows:

Keys

Movement of cursor

H

Cursor moves left

J

Cursor moves down

K

Cursor moves up

L

Cursor moves right

W

Word forward

B

Word backward

E

End of word

^

First character

$

End of line

0

Beginning of line

Page 12: Assignment b43 Kuvempu University

The screen display can be controlled by scrolling the display forward and backward using the

following keys:-

Keys

Screen display

[ctrl-f]

Scroll display forward

[ctrl-h]

scroll display backward

[ctrl-d]

Scroll half-screen forward

[ctrl-u]

Scroll half-screen backward

5. a) Explain grep command with at least 5 examples with different options.

Ans: 1) A simple Linux grep example - searching for a text string in one file

This first grep command example searches for all occurrences of the text string 'fred' within the

"/etc/passwd" file. It will find and print (on the screen) all of the lines in this file that contain the text

string fred, including lines that contain usernames like "fred" - and also "alfred".

grep 'fred' /etc/passwd

In a simple grep example like this, the quotes around the string fred aren't necessary, but they are

needed if you're searching for a string that contains spaces, and may be needed when you get into

using regular expressions (search patterns).

2) Linux grep command - searching for a string in multiple files

Our next grep command example searches for all occurrences of the text string joe within all files of

the current directory:

grep 'joe' *

The '*' wildcard matches all files in the current directory, and the grep output from this command will

show both (a) the matching filename and (b) all lines in all files that contain the string 'joe'.

As another example, you can also use grep to search all files in the current directory that end in the

file extension ".txt", as shown here:

grep 'joe' *.txt

3) Case-insensitive file searching with the Unix grep command

Page 13: Assignment b43 Kuvempu University

To perform a case-insensitive search with the grep command, just add the -i option, like this:

grep -i score gettysburg-address.txt

This grep search example matches the string "score", whether it is uppercase, lowercase, or any mix

of the two.

4) Reversing the meaning of a grep search

You can reverse the meaning of a Linux grep search with the -v option. For instance, to show all the

lines of my /etc/passwd file that don't contain the string fred, I'd issue this grep command:

grep -v fred /etc/passwd

5) Using grep in a Unix/Linux command pipeline

The grep command is often used in a Unix/Linux pipeline. For instance, to show all the Apache httpd

processes running on my Linux system, I can use the grep command in a pipeline with the 'ps'

command:

ps auxwww | grep httpd

b) Explain Uniq command.

Ans: The default output is to display lines that only appear once and one copy of lines that appear

more than once. It is also useful to filter out multiple blank lines from unsorted output of other

commands. For example, the dircmp command displays its output using pr; thus the output usually

scrolls off your screen before you can read it. But if you pipe the output of the dircmp command

through the uniq command, the blank lines are reduced and the output is more compact.

-u Print only lines which are not repeated (unique) in the original file

-d Don't output lines that are not repeated in the input.

-c Generate an output report in default style except that each line is preceded by a count of the

number of times it occurred. If this option is specified, the -u and -d options are ignored if either or

both are also present.

-i Ignore case differences when comparing lines

-f Ignore a number of fields in a line

-s Skips a number of characters in a line

-w Specifies the number of characters to compare in lines, after any characters and fields have been

skipped

--help Displays a help message

--version Displays version number on stdout and exits.

Page 14: Assignment b43 Kuvempu University

6. a) Explain the mechanism of executing job periodically using cron.

Ans: A task can be automatically run in the background at regular intervals by a Unix utility called

cron. The cron daemon takes care of running these background jobs, which are called cron jobs.

crontab or the crontable is a file which contains the schedule of cron entries to be run and at specified

times. cron checks the crontable at regular tables to see if there are any jobs scheduled. A user can

execute crontab if the user‟s name appears in the file /usr/lib/cron/cron.allow. If the cron.allow does

not exist, the cron.deny file in /usr/lib/cron is checked. If the user‟s name is not in this file the user is

allowed to use crontab. If only cron.deny exists and is empty, all users can use crontab. If neither file

exists, only the root user can use crontab. This allows and deny files contain one user name per line.

b) Write a shell script to add two numbers by using Expr utility.

Ans: To use it in a shell script, you simply surround the expression with backquotes. For

example, let‟s write a simple script called add that adds two numbers typed as arguments:

# /bin/sh

# Shell Script to Add two numbers

sum=„expr $1 + $2„

echo $sum

Here we defined a variable sum to hold the result of the operation. (Note the spaces around the plus

sign, but not around the equals sign). To run this script, we might type the following line: (assume the

script

add is executable)

$ add 4 3

The first argument (4) is stored in $1, and the second (3) is stored in $2. The expr utility then adds

these quantities and stores the result in sum. Finally, the contents of sum are echoed on the screen:

7

$

The expr command only works on integers (i.e., whole numbers). It can perform addition (+),

subtraction

(-), multiplication (*), integer division (/), and integer remainder (%).

Page 15: Assignment b43 Kuvempu University

7. a) What are positional parameters? Explain the command used to set the positional

parameters.

Ans: These are used by the shell to store the values of command-line

arguments.

The command which used to set the positional parameter

#!/bin/sh

# Demonstrate the set command

set „date„

echo “Time: $4 $5”

echo “Day: $1”

echo “Date: $3 $2 $6”

Assuming that setdate has been made executable with the chmod command, we can run the script by

typing the command

$ setdate

The output will look something like this:

Time:10:56:08 EST

Day: Fri

Date: 20 Aug 2004

What happened? Consider the command line

set „date„

The backquotes run the date command, which produces output something like this:

Fri Aug 20 10:56:08 EST 2004

This does not appear on the screen. Instead, the set command catches the output and stores it in the

positional parameters $1 through $6:

$1 contains Fri

$2 Contains Aug

$3 contains 20

$4 contains 10:56:08

$5 contains EST

$6 contains 2004

b) Explain Head Command with example.

Page 16: Assignment b43 Kuvempu University

Ans: The head command, as the name implies, displays the top of the file. When used without an

option, it

displays the first ten lines of the file.

You can use the –n option to display the first „n‟ number of lines.

$ head emp.lst Shows first ten lines of the emp.lst file

The head command, as the name implies, displays the top of the file. When used without an option, it

displays the first ten lines of the file.

You can use the –n option to display the first „n‟ number of lines.

$ head emp.lst Shows first ten lines of the emp.lst file

1001|T.N.Raju |Professor |Information Science|14/06/61|30000

1004|D.S.Raghu |Lecturer |Information Science|05/12/75|17000

1005|S.K.Anantha |Asst.Prof. |Information Science|20/07/63|24000

1009|M.P.Rajendra |Sr.Lecturer |Computer Science |13/03/66|20000

1002|Mallu |Lecturer |Information Science|20/07/74|15000

8. Explain the options and associated actions for ls command.

Ans: The Is command shows the contents of a directory , and a view of basic information (like

size ,ownership, and access permission) about files and directories.

The options and associated actions for Is command are:

OPTION

ACTION

-1

One file name in each line

-a

All files including those beginning with a dot(.), current directory (.) and directory above (..)

-A

All files including those beginning with a dot(.).does not list current directory(.) and directory above.

-d

If an argument is a directory ,lists only its name (not its contents)

-F

Marks directories with a /,executables with a * and symbolic links with a @

-I

Page 17: Assignment b43 Kuvempu University

Shows the inode number

-l

Lists in long format , giving mode, number of links, owner, group, size in bytes, the time that each

file was last modified.

-p

Puts a slash (/) after each directory.

-r

Sorts the filenames in the reverse order

-R

Recursive list

-t

Sorts by time modified (latest first)

-u

Sorts by last access time