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

AMITY INSTITUTE OF INFORMATION AND TECHNOLOGY

UNIX PRACTICAL FILE

SUBMITTED TO :

SUBMITTED BY:

Q1. WHAT IS UNIX? DESCRIBE UNIX ARCHITECTURE?


UNIX is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna. The UNIX operating system was first developed in assembly language, but by 1973 had been almost entirely recoded in C, greatly facilitating its further development and porting to other hardware. Unix operating systems are widely used in servers, workstations, and mobile devices. In a strict sense, an operating system can be defined as the software that controls the hardware resources of the computer and provides an environment under which programs can run. Generally, we call this software the kernel, since it is relatively small and resides at the core of the environment. Figure 1.1 shows a diagram of the UNIX System architecture.

Figure 1.1. Architecture of the UNIX operating system

The interface to the kernel is a layer of software called the system calls. Libraries of common functions are built on top of the system call interface, but applications are free to use both. The shell is a special application that provides an interface for running other applications.

Q2. DESCRIBE THE FUNCTION OF AT LEAST TEN UNIX COMMANDS.

Ls command
ls command is most widely used command and it displays the contents of directory.

options

ls will list all the files in your home directory, this command has many options. ls -l will list all the file names, permissions, group, etc in long format. ls -a will list all the files including hidden files that start with . . ls -lt will list all files names based on the time of creation, newer files bring first. ls -Fxwill list files and directory names will be followed by slash. ls -Rwill lists all the files and files in the all the directories, recursively. ls -R | more will list all the files and files in all the directories, one page at a time.

Nohup command.
nohup command if added in front of any command will continue running the command or process even if you shut down your terminal or close your session to machine. For exmaple, if I want to run a job that takes lot of time and must be run from terminal and is called update_entries_tonight . nohup update_entries_tonight will run the job even if terminal is shut down in middle of this job.

Head command.
head filename by default will display the first 10 lines of a file. If you want first 50 lines you can use head -50 filename or for 37 lines head -37 filename and so forth.

Tail command.
tail filename by default will display the last 10 lines of a file. If you want last 50 lines then you can use tail -50 filename.

Wc command
wc command counts the characters, words or lines in a file depending upon the option.

Options

wc -l filename will print total number of lines in a file. wc -w filename will print total number of words in a file. wc -c filename will print total number of characters in a file.

Mv command.
mv command is used to move a file from one directory to another directory or to rename a file.

Some examples:

mv oldfile newfile will rename oldfile to newfile. mv -i oldfile newfile for confirmation prompt. mv -f oldfile newfile will force the rename even if target file exists. mv * /usr/bajwa/ will move all the files in current directory to /usr/bajwa directory.

Rm command.
To delete files use rm command.

Options:

rm oldfile will delete file named oldfile. rm -f option will remove write-protected files without prompting. rm -r option will delete the entire directory as well as all the subdirectories, very dangerous command.

Rmdir command.
rmdir command will remove directory or directories if a directory is empty.

Options:

rm -r directory_name will remove all files even if directory is not empty. rmdir sandeep is how you use it to remove sandeep directory. rmdir -p will remove directories and any parent directories that are empty. rmdir -s will suppress standard error messages caused by -p.

Sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents
zzz aaa 1234 yuer wer qww wwe

Then running sort testfile will give us output of


1234

aaa qww wer wwe yuer zzz

Options:

-b ignores leading spaces and tabs. -c checks whether files are already sorted. -d ignores punctuation. -i ignores non-printing characters. -n sorts in arithmetic order. -ofile put output in a file. +m[-m] skips n fields before sorting, and sort upto field position m. -r reverse the order of sort. -u identical lines in input file apear only one time in output.

PS command
ps command is probably the most useful command for systems administrators. It reports information on active processes. ps options options.

-a Lists all processes in system except processes not attached to terminals. -e Lists all processes in system. -f Lists a full listing. -j print process group ID and session ID.

Q3. WHAT IS THE PS4 PROMPT AND HOW IS IT USED?


The PS4 prompt is evaluated and displayed before each line when running an execution trace. If unset, a + and a <space> will be output before each line in the trace. Putting '$LINENO' inside PS4 will cause the line number to be displayed. Putting '$SECONDS' in the PS4 prompt will cause the elapsed time to be displayed before each line. Note that single quotes are used to prevent the expansion from happening when PS4 is defined. A great tool for debugging shell scripts is an execution trace. An execution trace lists each command before the shell runs it. You can enable execution trace for your script by simply adding set -x to the beginning of it. The default execution trace prompt is + (the plus sign), but can be changed by redefining the PS4 shell variable: Contents of test.debug script: #!/bin/ksh set -x for i in 1 2 3 do echo $i done exit Execution output: $ test.debug + echo 1 1 + echo 2 2 + echo 3 3 + exit $ A common practice is to use the LINENO variable within the PS4 prompt to display the line number in the script that corresponds with the trace line. For example: Updated test.debug script: #!/bin/ksh

set -x PS4='[${LINENO}]+' for i in 1 2 3 do echo $i done exit

Execution output: $ test.debug + PS4=[${LINENO}]+ [8]+echo 1 1 [8]+echo 2 2 [8]+echo 3 3 [11]+exit Notice that line numbers 8 and 11 from the test.debug script are identified in the execution trace output.

Q4. EXPLAIN MULTIPROGRAMMING, MULTI-USER AND MULTITASKING OPERATING SYSTEM WITH EXAMPLES AND DIAGRAMS?
Multiprogramming Multiprogramming is a rudimentary form of parallel processing in which several programs are run at the same time on a uni-processor. Since there is only one processor, there can be no true simultaneous execution of different programs. Instead, the operating system executes part of one program, then part of another, and so on. To the user it appears that all programs are executing at the same time.

Multitasking Multi tasking Operating system is the one which is capable of running multiple processes or tasks, by sharing the common resources like CPU. These operating systems use the concepts task-scheduling and context-switching for implementing the multitasking ability. Task scheduling algorithms will identify the task to be run at a given time and also which is the next task to run. These tasks will be queued for processing. Context switching is a process, to store and restore the state of a CPU. When a process is executed the state of the CPU with respect to that process will be restored and while completing the time slice, the present state will be stored.

Multi-user Multi-user is a term that defines an operating system or application software that allows concurrent access by multiple users of computer. Time-sharing systems are multi-user systems. Most batch processing systems for mainframe computers may also be considered "multi-user", to avoid leaving the CPU idle while it waits for I/O operations to complete. An example is a UNIX server where multiple remote users have access (such as via Secure Shell) to the Unix shell prompt at the same time.

Q5. WHAT IS KORNSHELL? WHAT IS THE HISTORY OF KSH?


The Korn shell (ksh) is a UNIX shell which was developed by David Korn (AT&T Bell Laboratories) in the early 1980. KornShell is a command and scripting language that is a superset of the System V UNIX shell, aka, BourneShell (or 'sh'). ksh is the name of the program that implements the KornShell language. ksh was written by David Korn at Bell Telephone Laboratories. David Korn is currently at AT&T Laboratories. The first version of ksh was in 1983. It was the first shell to have command line editing with both emacs and vi style interaction. The 1986 version was the first to offer multi-byte support. The 1988 version of ksh is the version that was adopted by System V Release 4 UNIX and was a source document for the IEEE POSIX and ISO shell standards. The 1993 version is a major rewrite of the 1988 version and focuses more on scripting.

Q6. EXPLAIN DIFFERENCES AMONG THE VARIOUS UNIX SHELLS?


Bourne Shell(sh) This is the original UNIX shell written by Steve Bourne of Bell Labs. It is available on all UNIX systems. This shell does not have the interactive facilities provided by modern shells such as the C shell and Korn shell. The Bourne shell does provide an easy to use language with which you can write shell scripts. C shell (csh) This shell was written at the University of California, Berkeley. It provides a C-like language with which to write shell scripts - hence its name. TC shell(tcsh) This shell is available in the public domain. It provides all the features of the C shell together with emacs style editing of the command line. Korn shell(ksh) This shell was written by David Korn of Bell labs. It is now provided as the standard shell on UNIX systems. It provides all the features of the C and TC shells together with a shell programming language similar to that of the original Bourne shell. It is the most efficient shell. Consider using this as your standard interactive shell.

Q7. WHAT IS THE PURPOSE OF THE GREP COMMAND? HOW GREP WORKS EXPLAIN?
grep command searches the given file for lines containing a match to the given strings or words. By default, grep prints the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p grep command syntax grep 'word' filename grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something'

Use grep to search file Search /etc/passwd for boo user: $ grep boo /etc/passwd You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option: $ grep -i "boo" /etc/passwd

Use grep recursively You can search recursively i.e. read all files under each directory for a string "192.168.1.5" $ grep -r "192.168.1.5" /etc/

Use grep to search words only When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word: $ grep -w "boo" /path/to/file

Use grep to search 2 different words use egrep as follows: $ egrep -w 'word1|word2' /path/to/file

Count line when words has been matched grep can report the number of times that the pattern has been matched for each file using -c (count) option: $ grep -c 'word' /path/to/file Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained: $ grep -n 'word' /path/to/file

Grep invert match You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar: $ grep -v bar /path/to/file

UNIX / Linux pipes and grep command grep command often used with pipes. For example print name of hard disk devices: # dmesg | egrep '(s|h)d[a-z]' Display cpu model name: # cat /proc/cpuinfo | grep -i 'Model' However, above command can be also used as follows without shell pipe: # grep -i 'Model' /proc/cpuinfo

How do I list just the names of matching files? Use the -l option to list file name whose contents mention main(): $ grep -l 'main' *.c

Q8. HOW TO CONCATENATE TWO OR MORE STRINGS INTO ONE? EXPLAIN IN DETAIL?
We can give the answer in two ways. 1. Using shell programming 2. Using the normal basic UNIX commands. 1. Shell programming $str1 = "Amity" $str2 = "University" read $str1 read $str2 $str3 = $str1 $str2 echo $str3 Output is: Amity University 2. Normal Basic UNIX commands f1 and f2 are two files having the data, "Amity" and "University" respectively. $cat f1 f2 We will get the desired output.

Q9. WHAT IS THE RELATIONSHIP BETWEEN THE ENVIRONMENT AND INHERITANCE AND THE SHELL?
Basic features common to all the UNIX shells include piping, here documents, command substitution, variables, control structures for condition-testing and looping and filename wildcarding. Both bourne shell and C shell have been used as coding base and model for many derivative and work-alike shells with extended feature sets. Every new shell is inherited from the old with certain new features. The UNIX shell is most people's main access to the UNIX operating system and as such any improvement to it can result in considerably more effective use of the system, and may even allow you to do things you couldn't do before. The primary improvement most of the new generation shells give you is increased speed. They require fewer key strokes to get the same results due to their completion features, they give you more information (e.g. showing your directory in your prompt, showing which files it would complete) and they cover some of the more annoying features of UNIX, such as not going back up symbolic links to directories. Hence, the relationship between the environment, inheritance and shell.

Q10. HOW DOES THE SHELL TREAT PROCESSES?


A process can be simply defined as an instance of a running program. It should be understood that a program is an entity that resides on a non-volatile media (such as disk), and a process is an entity that is being executed in RAM. In Unix-like computer operating systems, a pipeline is the original software pipeline: a set of processes chained by their standard streams, so that the output of each process (stdout) feeds directly as input (stdin) to the next one. All processes (except the very first one) have a parent which created them. Similarly, when a process is created, it is created as a child process, with the process responsible for its' creation being its parent. When a process creates a child process, it is said to have spawned a child. The following are the steps which show how the shell treats a process: 1. the shell is running (i.e. is memory resident) and is waiting for user input 2. the user enters a command followed by [Enter] 3. the shell interprets (decodes) any special characters in the command (e.g. meta-characters) The shell begins to search each directory location specified in the PATH variable for the program (file on disk) the user entered (unless it is built-into the shell; in that case, execution commences immediately). 4. if the program is not found in any directory specification in the PATH variable, the shell reports an error, and returns to step 1 5. if the program is found, the shell creates a copy of itself (via fork system call), almost identical except for a new PID, this becomes the CHILD of the initial shell 6. the requested program is loaded and its process image is overwritten (overlaid) onto the copy of the parent (via exec system call) and execution begins 7. the parent sleeps at this point (wait system call) as the child process runs. When the child process completes, the child dies and sends a termination signal to the parent, which causes the parent process (shell) to wake up and return to step 1

Вам также может понравиться