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

UNIX SOLUTION

1. How is Unix different from other operating systems? Ans: UNIX is different from other operating systems in two ways i.e. one of its internal implementation and another is its interface that is used by users. 2. What is the difference between internal command and external command? Ans: Internal commands are built in shell. Shell does not run any other process to run built in commands. They take less time in their execution where as external commands are not built in shell. Shell runs other process for their execution. They take more time in their execution. 3. The commands are separated by semicolons (;). the semicolon in this context is called as metacharacters 4. How do you stop and the scrolling of the screen display Ans: Ctrl + S is used to stop scrolling and Ctrl + u is used to resume the scrolling. 5. uname v command is used to display the name of the operating system 6. Where is the password stored and in what form? Ans: The password is stored in /etc/passwd and in the binary form. 7. What is the difference between absolute path name and relative path name. Give one example. Ans: Absolute pathname is associated with root. Absolute pathname gives the location of a file in relation to the root. The pathname starting from the root to destination is specified by using a / between each directory name. Relative pathname is associated with the current directory. A relative pathname starts with the current directory. In a relative pathname a single dot represents current directory and double dot represents the parent. Example of absolute pathname: /etc/passwd. Example of relative pathname: /etc/passwd. Where cd ../.. represents root as working directory. 8. What is the output of is command used with -F option? Ans: ls F command marks directory with /, executables with * and symbolic links with @. 9. What is multi tasking? Ans: Multitasking is the ability of an operating system to run more than one program at the same time.

AshuRaj

UNIX SOLUTION
10. If file has 100 bytes, write command to split this file it into files of 20 bytes each. Ans: split b 20b file.txt file 11.File attributes of each file in the file system are stored in inode table. 12. How are hard links created? 1 Marks Ans: Hard links are created using ln command. The syntax for creating hard links is ln oldfile newfile 13. 14. List any four Shells wild cards. 2 Marks What the use of tee command. 1 Marks Ans: ? * [a-z] [!a-d] [abc] Ans: tee command is used to save the output of a command in a file or to pipe it an input for other command. tee is an external command and can be placed anywhere in a pipe. With tee command a user can display the output on the screen and can save the output to a file. 15. Define Shell variable. 1 Marks Ans: Shell variable are memory space for storing the value. Shell variables can be user variable and Environmental variable. 16. What is pipe? What is the use of this. 2 Marks Ans: Pipe is most useful and inter communication process. It is based on two file descriptors i.e. input and output. It is like a pipe where output of one command acts as input for another command. This means output of a command is piped from one end of pipe and it comes out of other end of pipe. 17. 18. Which command is used to check the process status of all users. 1 Marks What is the function of Unix Kernel? 2 Ans: ps Ans: Kernal performs following functions: it manages memory. It schedule process and decides priorities, it communicates with hardware. 19. How does the type command work? 2 Ans: The type command searches for the command name in search path. Search path is searched in environmental variable called PATH which works in order means that the if two command have same name then command that is found first is executed first. 20. What is a secondary prompt? 2 Ans: When shell identifies an incomplete command then it provides a secondary prompt. Long commands can be entered using secondary prompt. AshuRaj 2

UNIX SOLUTION
21. 22. How do you lock your terminal for 20 minutes? How to record your login session into a file ? 2 Ans: lock v 20 Ans: login session can be recorded in a file using script command. The default filename is typescript in case no filename is given. script a filename is used to append to an existing script file. Syntax: script filename 23. 24. 25. What are the different types of files in Unix ? 2 How to remove the file lessons from the directory ? 2 What are the uses of hard links? 2 Ans: Different types of files in unix are ordinary, directory, special and links files. Ans: rm lessons Ans: Hard links are created using ln command. The syntax for creating hard links is ln oldfile newfile. Hard link prevents accidental deletion of a file means if the original file is deleted then hard link file can be used. 26. 27. How do you yank and paste lines ? 2 How do you replace all the occurrences of the word printf with fprint in a Ans: yank and paste lines is performed using yy command. file? 2 Ans: tr [printf] [fprint] < filename 28. 29. Write a command to pipe the output of cot into the more command. What is the command to search all files in your current directory for the Ans: cot filename | more word computers? Ans: find . name computers -print 30. System administration tasks must generally be performed by which username? 1 Mark Ans: root 31.Write the UNIX command to display the number of characters, lines and words in the files myfile1. txt, myfile2.txt and myfile3.txt. 1 Mark Ans: wc clw myfile1 myfile2 myfile3 32. 33. What is the generic syntax for all UNIX commands? 1 Mark Which of the following represents all absolute path?3 Ans: command name [option] [argument]

AshuRaj

UNIX SOLUTION
a. ../home/file.txt b. bin/cat. c. cs2005/ d. /usr/bin/cat e. None of the above 1 Mark 34. Write a UNIX command to display only the lines common to two files. 1 Mark Ans: comm. -3 file1 file2 35.Which command will print the contents of all files in the current directory whose names start with the character ,a and end with a period (.) followed by any two characters followed by a number? 2 Marks Ans: find . name a.??[0-9] -print 36. What happens when you use the 1n command? 1 Mark Ans: ln command is used to create a hard link and a soft link. 37.What is the command to set the permissions on file textfile to read and write for the owner, read for the group, and nothing for everyone else? 1 Mark Ans: chmod 640 textfile 38.Make a copy of file upper in the directory two levels up. 1 Mark Ans: cp upper cd ../.. 39.Create a new file new.txt that is a concatenation of filel.txt and file2.txt. 1 Mark Ans: cat file1.txt file2.txt >> new.txt 40.What is a shell in UNIX? 1 Mark Ans: Shell is a command interpreter in UNIX. For every logged users a shell keeps running. Shell executes all the command entered by users. 41. Write a command to count the number of users logged into the system. 2 Marks Ans: who | wc -l 42. Display the current date in dd/mm/yyyy and mm-dd-yyyy formats. 2 Marks Ans: date +%D , date +%m-%d-%Y 43. How do you change the user password and where is the password stored? 2 Marks AshuRaj 4

UNIX SOLUTION
Ans: user password is changed using passwd command and is stored in /etc/passwd. 44. 45. 46. What is the command to display output one screenful at a time. 1 Mark Display the calender for the year 2006. 1 Mark Find all the directories in the home directory with permissions 777. 2 Ans: more Ans: cal 2006 Marks Ans: find $HOME perm 777 type d -print 47.Using the grep command, count the number of lines in which the word lecturer occurs in the file emp.lst. 1 Mark Ans: grep c lecturer emp.lst 48. 49. Name three ways of exiting a vi session after saving your work. 1 Mark Can you have the same login more than once in the who output? 1 Mark Ans: ZZ , :wq , :x Ans: yes 50.How do you run Is to mark directories and executables separately. 1 Mark Ans: ls -F 51. What happens if a directory has permissions 777 ? 1 Mark Ans: It means everyone has read, write and execute permissions for the given directory. 52. 53. Frame a wild card model where the last character is not numeric. 1 Mark Write a command to count number of sub directories in a directory. 1 Ans: ls *[!0-9] Mark Ans: find . type d print | wc l 54. Write a command to assign execute permissions to all types of users. 1 Mark Ans: chmod a+x filename 55. With which commands do you display file names and processors? 2 Marks 56.If mkdir test fails, what could be the possible reasons? 2 Marks Ans: mkdir test fails then the reason may be existence of same directory and there may be no permission. 57. What is topmost directory called in UNIX? How is it represented? 2 Marks Ans: root and is represented using slash (/) AshuRaj 5

UNIX SOLUTION
58.Name three modes of vi editor. 2 Marks Ans: command, input, ex or last line mode. 59.What does following characters signifies as first character in output of ls - 1 command. -, d, b, c. 4 Marks - = ordinary file d = directory file c = special file b = special file 60. Give a command to display only the running processes. 1 Mark a. Give the commands to accept and refuse messages. 2 Marks mesg +n mesg n 61.What does / dev / tty and / dev / null represents? 2 Marks Ans: /dev/tty represents the terminal which can be used for redirection and argument in some file whereas /dev/null represents empty. Anything redirected to /dev/null disappears. 62.What is PATH variable? What is its use? 2 Marks Ans: PATH is an environmental variable. It contains the list of directories where shell looks for command. 63.How you do the following in Job control a) Running process in Background b) Stopping process executing in foreground c) Taking stopped job to background d) Bringing background job to foreground 64. Identify the relative and absolute path names in the following. 2 Marks a) ../progs / x1.c b) / usr / bin / ls 2 Marks 65. Which are the internal commands in the following list: 66.cd, pwd, data, ls 2 Marks 67.What is the output of / s / wc - l command? 1 Mark 68. Illustrate three vi modes with a neat sketch. 3 Marks

AshuRaj

UNIX SOLUTION
69. Which important file attributes changes when you copy a file from some other user account?

1. Describe history of UNIX operating system. 5 Marks Ans: UNIX operating system is an increasingly popular operating system. In 1960s AT&T and Bell laboratory were developing an operating system to provide large scale computing services known as multics. But in 1969 the Bell lab withdrew from this project. But AT&T continued their work with few of its developer namely Kep Thompson, Denis Ritchie and others and they came with the UNIX. The main achievement of UNIX was its deployment for text formatting at the patent department of AT&T. In 1973 C language was developed and UNIX was written in C language. AT&T was not allowed commercial license so UNIX could not came in the market. But it became popular by its internal use at AT&T itself. The universities were granted permission for use of UNIX. In 1977 commercial licenses were given and it came to the market. The two popular version of UNIX were developed known as SVR3.2 and SVR4. While AT&T through its research developed UNIX and the universities which was granted permission for its use came with their own version developed at Universities of California, Berkley known as BDS. 2. Explain architecture of UNIX. 5 Marks Ans: UNIX like other operating system is a layer between hardware and application that run on the computer. UNIX has function that manages hardware and application. The main difference between UNIX and other operating system is its implementation and its interface. The part of the UNIX that manages hardware and executing process is called kernel which is a collection of program written in C. Kernel communicates with hardware and application uses the services of the kernel to communicate with hardware. Kernel manages memory, decides priorities and schedules process. In UNIX each hardware device is a file. It maintains simple method of reading and writing file for accessing hardware. Many command used to access ordinary file works to access these file also. For every logged user a shell runs. Shell is an interpreter which executes all the command entered by the user. Shell forms the interface between user and application. Shell communicates with the kernel for execution of command using the function called system call. UNIX provides application portability means it allow the execution of same program on different type of computer hardware without modification. It can be achieved if the operating system is UNIX. The UNIX layer architecture hides the application from hardware 3. Explain uname command with two examples. 5 Marks Ans: uname command is used to know the details about the operating system. It gives detail information about name of operating system, version, operating system release, machine node, kernel ID, processor type, bus type, serial number, number of users license, OEM number, origin number and number of CPUs. Few of the option used with uname command are following: uname a gives all information. AshuRaj 7

UNIX SOLUTION
uname r gives the release date. uname v gives the version. uname s gives the name of operating system. uname X gives information about the name of operating system, machine node name, release date, kernelID, Machine name, BusType, Serial, Users, OEM, Origin ,number of CPUs. Two examples of uname command: uname v gives the version number. 5.0.5 Uname r gives the operating system release 3.2 4. Explain the different types of files in UNIX operating system. Discuss how to create those files. 10 Marks Ans: An UNIX operating system has following four kinds of files namely ordinary files, directory files, special files and link files. Ordinary files: They contain text, data and program information. They cannot contain another file. Ordinary file can be text file and binary file. Text file contains the lines of characters where each line is terminated with new line characters. Binary file contains ASCII values. Most of the UNIX files are binary files. Directory files: Directories are container that contains files and other sub directory. A directory has a line for every item stored in a directory. It contains the name of item and a reference to the location of the item. The numerical reference is called i-number. Special files: It represents input and output devices like tty (terminal) disk drive or printers. UNIX treats every device as a file so many commands used to access an ordinary file can also be used to access a device. Special files can be either character files that deals with stream of character or block files that has a larger size of data. The sizes can be 512, 1024, 2048 bytes. Link files: A link is a pointer to a file. Links are of two types one is hard link and another is soft link. The link to a file is created using ln command. During the creation a hard link no new file is created and the idone numbers of two are same. It is not created across file system. . During the creation a soft link a new file is created and the idone number of two different. It is created across file system. 5. Explain UNIX file system structure. 5 Marks Ans: The UNIX file system is installed at the time of installation of UNIX. The root which is represented using a slash (/) exists at the top of file system. Root contains many sub directory that contains many vital information. /bin and /usr/bin contains the command used by the user. /sbins and /usr/sbins contains the command used only by the administrator. /etc contains the configuration files. Login names and passwords are stored in /etc/passwd and /etc/shadow directory. /dev contains device file. It does not occupy system space. /lib and /usr/lib contains library in a binary form.

AshuRaj

UNIX SOLUTION
/usr/include contains header files used by C programs. /tmp is used to create temporary files. /var is used to contains variable data like print request, incoming and outgoing mail. 6. Explain cmp command with suitable examples with options. 5 Marks Ans: cmp command is used to compare two compare two files. By default cmp is silent means it produces no output on the screen if two files are same. But if the file differs then byte number and line number at which difference occurs is displayed. The numbering of line and bytes starts with one. It has option l by which the displays the byte number in decimal and differing byte in octal. Example of cmp command using options: cat fruits1 apple mango pineapple orange cat fruits2 apple grapes pineapple orange $ cmp fruit1 fruit2 fruit1 fruit2 differ: char 7,line 2 7. Explain chmod command with suitable examples. 7 Marks Ans: chmod command is used to change the permission of a directory or a file. The permission can be read, write and execute which is represented using r,w and x respectively. The user who is the owner of file or directory has rights to change the permission. Chmod has two types. One is symbolic mode which is made up of letters and operators. The other is absolute mode which is made up of numbers. It has the option R which recursively changes the file permissions. The syntax of chmod command in symbolic mode: chmod [R] [category] [operator] [permission] file In the symbolic mode the category can be one of the following: a stands for all users. u stands for the owner of file g stands for the group to which the owner of file belongs. o stands for group other than user. In absolute mode each type of permission is an octal number. For each category, the permissions are assigned by adding the octal numbers representing the following: 4 sets read permission. 2 sets write permission. 1 sets execute permission. Examples of chmod in symbolic mode:

AshuRaj

UNIX SOLUTION
To set execute permission for every one for file emp.lst in symbolic mode chmod a+x emp.lst To set execute permission for every one for file emp.lst in absolute mode chmod 111 emp.lst 8. What are the three level of file protection that determine the file access rights. 3 Marks Ans:. The three level of file protection that determine the file access rights are read, write and execute which is represented using r,w and x respectively. The user who is the owner of file or directory has rights to change the permission. These file access rights can be changed using chmod command. Chmod has two types. One is symbolic mode which is made up of letters and operators. The other is absolute mode which is made up of numbers. It has the option R which recursively changes the file permissions. 9. What is the difference between Hard link and symbolic link. 5 Marks. During the creation a hard link no new file is created and the idone numbers of two are same. It is not created across file system. It prevents the accidental deletion of a file. If the source file is deleted that can be recovered using hard link file. During the creation a soft link a new file is created and the idone number of two different. It is created across file system. If the source file is deleted then the link file acts as a dangling file. 10. List and describe in one sentence of key commands used to add or replace text in the input mode. 5 Marks command function i Insert text to before cursor a Append text after cursor I Insert text before first non blank character A Append text at the end of line o Open new line below O Open new line above r Replace single char under cursor R Replace single character from cursor onwards s Replace single character with characters S Replace entire line

11. With any five examples, explain the use of wild cards in UNIX. 5 Marks Ans: The wild cards are used to match a given pattern. The different types of wildcard characters used in UNIX are following: * Represents any number of character any number of times. ? Represents a single character. [abc] Represents a single character either a, b, c

AshuRaj

10

UNIX SOLUTION
[a-d] [!abc] [!a-d] Represents a single character in the range of a or b or c or d Represents a single character that is not a, b, c Represents a single character that is not in the range of a or b or c or d

ls file* list all file which begins with file and ends with anything. ls file? list all files which name has five character but first four character is file. rm file* removes all file whose name starts with file and conatins anything after that. rm *.txt removes all the files ending with .txt. ls file[1-4] list all file beginning with file and ending within the range of 1 to 4 ls file[143] list all file beginning with file and ending with either 1 or 4 or 3. 12. Write a shell script to delete a file interactively, as exactly how it is done rm-i command. Ans: vi delfile echo Enter the name of file to be deleted read file if test ! f $file then echo $file is not a file else echo Do u want to delete the file read choice if test $choice=Y o y then rm $file echo $file deleted else echo $file not deleted fi fi

13. Explain the different control structure in shell programming. 15 Marks Ans: The different control structure in shell programming is following: There are two types of selection structures: if/then/elif/else/fi case There are three types of iteration structures; for while until

AshuRaj

11

UNIX SOLUTION
if and test: When shell finds a if and then structure then first of all it evaluates the condition. If the condition evaluates true then the commands between test and fi is executed. If condition is false then shell does not execute the commands between then and fi. Example of if AND test statement: set `date` if test $1=Sun then echo Sunday fi elif and else: It is an extended form of if and test statement where more condition can be specified. Example of elif and else statement: echo "Enter the name of file or directory" read str if test -f $str then echo "$str is a file" elif test -d $str then echo "$str is a directory" else echo "My name is kalpana" fi case statement: It is a selection structure. The general form of case statement is case word in pattern -1) commands;; pattern -2) commands;; pattern -n) commands;; esac The case statement compares the word with pattern if they match the shell runs the command. If the pattern does not match then the case compares the pattern one by one. Example of case statement: echo "Enter any command" read cmd case $cmd in who) echo "`who`";; cal) echo " `cal`";; ls) echo "`ls`";; ps) echo " `ps`";;

AshuRaj

12

UNIX SOLUTION
*) echo "My name is kalpana" esac for: It is used for executing a command again and again. This is called iteration. The general form of for loop is for variable in list do command(s) done example of for loop: for name in $* do ps u $name done while: The while loop is another iterative loop. It has general form like while condition do command(s) done if the condition is true then the while loop executes the commands between do and done until the condition gets false. Example of while loop: sum=0 count=1 while test $count -le 100 do sum=`expr $sum + $count` count=`expr $count + 1` done echo $sum until: The until loop is another iterative loop. It has general form like until condition do command(s) done It executes the commands between do and done until the condition is true. Example of until loop: sum=0 count=1 until test $count -le 100 do sum=`expr $sum + $count`

AshuRaj

13

UNIX SOLUTION
count=`expr $count + 1` done echo $sum 14. With a neat diagram explain different layers of UNIX operating system? Also explain interaction between shell and kernel using any suitable command. 15 Marks Ans: UNIX like other operating system is a layer between hardware and application that run on the computer. UNIX has function that manages hardware and application. The main difference between UNIX and other operating system is its implementation and its interface. The part of the UNIX that manages hardware and executing process is called kernel which is a collection of program written in C. Kernel communicates with hardware and application uses the services of the kernel to communicate with hardware. Kernel manages memory, decides priorities and schedules process. In UNIX each hardware device is a file. It maintains simple method of reading and writing file for accessing hardware. Many command used to access ordinary file works to access these file also. For every logged user a shell runs. Shell is an interpreter which executes all the command entered by the user. Shell forms the interface between user and application. Shell communicates with the kernel for execution of command using the function called system call. UNIX provides application portability means it allow the execution of same program on different type of computer hardware without modification. It can be achieved if the operating system is UNIX. The UNIX layer architecture hides the application from hardware.

Interaction between shell and kernel: When a user login a shell start running for that user. When a command is entered by the user the shell scans the command for Meta characters which has special meaning to shell. After processing Meta characters the shell transfers the command to kernel for execution. When a type cat is entered on $ prompt then shell interprets the command and search the search path in system variable called PATH. After searched is finish it is given to the kernel for the execution. 15. Explain the output of ls - l command. 8 Marks The command ls-1 lists seven attributes of all the files in the working directory. The top the list gives the number or blocks occupied by the files (a block is 512 bytes). The first column consists of a string of 10 characters. The first character indicates the file type. The nine characters after the file type are file permission, which arte display in three sets of three characters each. The first set refers to the ownership permission, the 2nd set to the group permission and the 3rd to the permission given to the users. In each set of permission given to the users. In each set of permission the three characters can be r (read), w (write), x (execute) or (no permission is set). The x permission for a directory means it can searched. In UNIX, a single copy of a file can be referred to by several

AshuRaj

14

UNIX SOLUTION
filenames. The 2nd column gives the number of filenames or links associated with the file. If the link count is greater than one it indicates that the file has more than one name. The 3rd column gives the file ownership. When a user creates a file, the file automatically belongs to the user. The 3rd column displays the owner of the file. The owner has the right to change file permissions, alter the contents of the file and remove the file. The 4th column shows the group ownership of each file. At the time of user account creation, the system administrator assigns the user to as group. All the users belonging to this group will have same privileges to the files with the same group id. The 5th column displays the file size in bytes. This doesnt mean the amount of disk space occupied by the files. The space occupied by the files is measured in blocks 10214 bytes each. Directories show smaller sizes because a directory maintains the list of filename, & their identification no. for each file within directory is therefore the size of this of filenames. The 6th column gives the data and time of modification of the files. The last column gives the name of the file and the list is arranged in the alphabetical order. 16. What are three different modes of vi editor? Explain those. 8 Marks Ans: Three different modes of vi editor are command mode, input mode and ex or last line mode. The command mode is the default mode of vi editor. Every key pressed in default mode is interpreted as command to be executed on text. The key pressed in command mode is not displayed on the screen. The input mode is used to enter text. The key pressed in input mode is displayed on the screen. After text entry is completed the cursor is positioned at the last character of last line. This line is called current line and cursor position is called current cursor position. The ex or last line mode is used to run commands, read file, text replacement and save and exiting vi editor. 17. List 10 commands and their usage that transit vi editor from command mode to input mode. h Cursor moves left j Cursor moves down k Cursor moves up l Cursor moves right w Word forward b Word backware e End of word ^ First character $ End of line 0 Beginning of line 18. What are standard input, standard output and standard error? Give a command to redirect standard error to working terminal and no where. 7 Marks Ans: standard input by default is the keyboard where command expects to read its input from the standard input. The standard input can be three files The default source is keyboard. Input can be read from a file. AshuRaj 15

UNIX SOLUTION
Input can be from another file using a pipe command. Standard output by default is the screen where command writes the standard output. Standard output can be three types Default source is screen Output can be redirected to a file. Can be used an input to another command. Standard error is the error message when a command generates error. The default source for standard error is the screen. The standard error can be redirected to a file, /dev/null and /dev/tty. Command to redirect standard error to no where is: $ ls l myfile 2 >/dev/null Command to redirect standard error to /dev/tty is: $ ls l myfile 2 >/dev/tty 19. ctrl ctrl ctrl ctrl ctrl ctrl ctrl ctrl What happens when the following keys are pressed? 15 h = erases text c = interrupts a command d = terminates a login session s = stop the screen scrolling q = resumes the screen scrolling u = kills a command line without executing it \ = kills a command being executed creates a core file z = suspends a command execution and returns to shell prompt.

20. List out the guidelines to form passwords and explain how to prevent unauthorized access to a login account with syntax. 15 Ans: The command used to change the password of a user is passwd. The password and login names are stored in directory /etc/passwd and /etc/shadow in binary form. The general guidelines to form passwords are: A password should be at least six characters long. Passwords can be combinations of numbers, upper case and lower case letters and punctuation. Obvious passwords like common names, word from dictionary, name of places etc should be avoided as password. There should be significant difference between old and new password. To prevent unauthorized access to a login account it is advisable to change the password frequently and username and password should be unique which should not be easily guessed. It is advisable not to tell anybody their password and username. The syntax for changing the password is passwd. When user enters the command the system prompts the user to enter the old password after that new password is entered. The new password is entered again for verification and the password matches. The password gets changed. 21. Write syntax to check your spellings in UNIX and also explain the actions taken for a basic commands used in ispell. 15

AshuRaj

16

UNIX SOLUTION
Ans; Syntax to check spelling in UNIX is Ispell filename where filename is the name of the file Some of the basic commands used in ispell are given below: command action R Replaces the word completely Spacebar Accepts the word this time only A Accept the word for rest of this ispell session I Accept the word, capitalized as it is in the fil 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) Looks up word in the dictionary 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 (letter) Redraw the screen Ctrl z Suspend Ispell ? Show the help screen 22. -1 -a -A -d -F -I -l -p -r -R -t -u -x Explain the options and associated actions for Is command. 15 One filename in each line All files including those beginning with a dot(.), current directory(.) and directory above All those file beginning with a dot(.),does not list the current directory and directory above If an argument is a directory then lists only its name not its contents. Marks directory with a /, executable with a * and symbolic link with a @ Shows the inode number List in long form, giving mode, number of links, owner, group, size in bytes, time and date of modification of each file was last modified. Puts a slash (/) after each directory Shows filename in reverse order Recursive list Sort by time last modified. Sort by last access time. Lists files in columns with entries sorted across

23. Describe briefly the UNIX architecture explaining the role played by the kernel and shell in sharing the workload. 15 Marks

AshuRaj

17

UNIX SOLUTION
Ans: UNIX operating system is a layer between hardware and application that runs on the computer. UNIX has function that controls the hardware and the application. The only difference between UNIX and other operating system is its internal implementation and the interface used by the users. The part of the UNIX that controls the hardware and the executing processes is called kernel. The kernel is the collection of a program written in C communicates with the hardware. The application communicates with hardware using the services of kernel. Besides memory management kernel decides priorities, schedule processes, allocate resources. Shell is an interpreter which runs for every user who is currently logged on. When user enters any command then shell scans the command for meta characters and after processing the meta characters shell transfers the command to kernel for the execution. The function used by shell to communicate with the kernel is called function calls. Shell forms the outer part of the operating system and interface between user and the kernel. 24. With examples, explain the following commands ps: The ps command is used to display some of process attributes. The ps command reads the kernel data structures and process tables to get the characteristics of processes. Without any option ps displays the processes associated with a user at the terminal.

$ ps PID TTY TIME CMD 3472 ttyp9 00:00:00 sh The PID column gives the process ID, the TTY column names the terminal where the command was started, the TIME column shows the amount of CPU time used by the command and the CMD column shows the name of the command. The option of ps are following: option explanation -f gives full list showing the PID of each process. -e or -a gives all user and system process -u user1 process of only user1 -a processes of all users. -l long list of information related to memory. -t term processes running on the term terminal. In UNIX system processes display a ? in the TTY column because they are not associated with any of the terminal. These processes are called daemons. A daemon is usually defined as a background process that does not belong to a terminal session. Daemons cannot be interrupted and become active only when they receive input. They are many system services are performed by daemons namely network services, printing etc.

AshuRaj

18

UNIX SOLUTION
25. With examples, explain the different types of shell variables. 15 Marks Ans: Shell variables are memory space used to store the value. Shell variable can be three types. Environmental variable User defined variable Positional parameter Environmental variable: Environmental variable tell the shell about the way an user account is set up. Some of the commonly used shell variables are HOME, SHELL, PATH, TERM, USER, MAIL etc. Some of the environmental variables like HOME and SHELL are set automatically when the users log in. Other variables like TERM are set by user. If the user is working on a terminal of type vt100 the variable TERM is set to vt100. variabl e HOME SHELL PATH TERM USER MAIL Value held Pathname of the home directory Pathname of the shell (/bin/sh) List of directories where shell looks for a command. Terminal type User name Pathname of the system mailbox.

User defined variable: User can create their own variable by giving the variable name and value. User defined variable name should begin with a letter and can contain letters, numbers and the underscore character. Variable names are case sensitive and are all of type string. Default value of a shell variable is null string. The syntax to assign value to a variable is variable=value. There should be no space on either side of = character. To display the value of a variable the name of the variable is preceded by a $. A variable can be removed by using the shell internal command unset. Variables can be concatenated. Value assigned in one variable can be assigned to other variable. Examples: $ firstname=kalpana $ lastname=kumari $ name=$firstname $ echo My name is $firstname $ echo My name is $firstname $lastname $ unset lastname. Positional parameter: The positional parameter is used to store the value of command line arguments. Positional parameters are also known as read only variables or automatic variable. The shell stores the value of command line argument in two ways. The two ways are implicitly and explicitly. When shell stores the command line argument automatically the process is implicitly and when user stores the command line argument himself using set command the process is explicitly. The positional parameter uses $1$2..$9 and $*

AshuRaj

19

UNIX SOLUTION
to store the command line arguments. The $# contains the number of argument user typed. $ echo my name is kalpana $ echo $4 prints kalpana. $ echo $* prints my name is kalpana $ echo you typed $# arguments prints you typed 4 arguments. 26. What is meant by recursive behavior of a command? Name four commands, along with a suitable example of each that can operate recursively. 5 Marks Ans: Recursive behavior of a command means that the command calls itself again and again. The command starts works on the basis of top down approach. It uses R option for performing recursive action. Four commands that work recursively are ls, cp, rm and chmod. Example of recursive commands: ls R: The command lists the contents recursively. It lists the file and sub directory of a directory in a hierarchy. $ ls R kalpana lists the contents of directory kalpana in a tree hierarchy. cp R: The command copies all files and sub directories in a specified directory. $ cp R saurabh sa copies all the files and subdirectories of saurabh to the directory sa. rm R: The command recursively deletes the contents of the directory specified and the directory itself. rm r sa deletes the contents of directory sa and the directory sa itself. chmod R: The command is used to change file mode characters recursively. For a specified directory, chmod will change the file mode characters of the file in the directory and the directory. chmod R u+xr myfolder gives read and write permission for the files and subdirectory and the directory itself to the user. 27. When the shell finds Meta characters in the command line what does it do? When the command does is finally executed. 5 Marks Ans: The commands are separated by a semicolon which is known as metacharacter in unix. When the shell finds a meta character in the command line it executes on either side of semicolon separately. The semicolon has special meaning to the metacharacter. The command is first interpreted by the shell and after processing the metacharacter the shell transfers the command to the kernel for the execution. 28. Which of these commands work? Explain with reasons. i) mkdir a/b/c ii) mkdir aa/b iii) rmdir a/b/c iv) rmdir aa/b v) mkdir/bin/foo 10 Marks

AshuRaj

20

UNIX SOLUTION
Ans: mkdir a/b/c does not work because the absolute pathname is incorrect. The absolute pathname should start with the root. mkdir aa/b does not work because the absolute pathname is incorrect. The absolute pathname should start with the root. rmdir a/b/c does not work because the absolute pathname is incorrect. The absolute pathname should start with the root and the path given has not started with the root. rmdir aa/b does not work because the absolute pathname is incorrect. The absolute pathname should start with the root and the path given has not started with the root. mkdir /bin/foo works because the absolute pathname is correct. The absolute pathname should start with the root and the path given has started with the root. 29. If you have a file with filename chap*. Give a command to remove that file. Also give reasons for not to have special characters like * ? etc in filename. 7 Marks Ans: The command to remove a file with filename chap* will be $ rm chap\* The file cannot contains the special symbols like ?, *, #, &, \, |,( ), [], { },$,<, >, etc as filename because these characters have special meaning to the shell. If any file contains these special symbols as filename then shell should take the special character literally. There are two ways of doing this precede the special character by a backslash (\) or enclose the special character in a single quote (). 30. What is redirection? Explain the mechanism redirecting standard input, Standard output and standard error. 8 Marks Ans: Redirection is the process of reading, saving the input of commands into files or sending the output of commands to another location is known as redirection. The command expects to read input from the standard input. The file representing the standard input can have three sources: The default source is the keyboard. The input can be read from a file. Input can be read from another command using pipes. The program writes its output to standard output. The file representing standard output can have three sources. The default source is the screen. The output can be redirected to another file. Can be used as input to another file. The program writes its error message to standard output which is by default is the keyboard. A command usually reads input from the keyboard and writes its output to the screen but if a command reads input from a file or writes output to a file or both. Then input and output for the command can be selected using the <, >, >> redirections symbols where < reads input from a file. > Writes output to a file. >> append the output to the end of a file.

AshuRaj

21

UNIX SOLUTION
The three standard files are represented using a number called file descriptor. 0 standard input, default is the keyboard. 1 standard output, default is the screen. 2 standard error, default is the screen The general format of standard error redirection is command 2> errorfile. The error can be redirected to a new file, existing file. The error and output can be redirected to the same file. $ ls l file1 >outfile writes the output to the outfile. $ ls l file1 2> error writes the error to the file error. $ ls l file1 >outfile 2>errorfile the output is written to the outfile and if error comes it will be redirected to errorfile. $ ls l file1 >outfile 2> &1 the output and error are written to same file outfile. 31. Explain the use of pipes and tees to connect commands. 5 Marks Ans: pipe command is defined by two file descriptor one is open for output and another for input. The output from one process goes an input to another process which means that data goes from one end of the pipe an comes out of the other. The data flowing in a pipe are directly managed by the kernel. The shell connects the input and output a special pipe character |tee command is used to save the output of a command in a file or to pipe it an input for other command. tee is an external command and can be placed anywhere in a pipe. With tee command a user can display the output on the screen and can save the output to a file. Use of pipe and tee to connect command: $ ls l | tee listfile passes the output of ls l command as an input to tee command where tee command saves the output to a file listfile and displays the output to the screen. sort myfile | tee sortfile the sort command sorts file myfile and pipe the output to the tee command as an input where tee command saves the output in a file sortfile and displays the output to a file sortfile. 32. What are internal and external commands? Give a reason why command cd cannot be an external command. 5 Marks Internal commands are built in shell. Shell does not run any other process to run built in commands. They take less time in their execution where as external commands are not built in shell. Shell runs other process for their execution. They take more time in their execution. cd cannot be an external command because it is built in shell that means the shell interprets the command and change the current directory. 33. What is Shell programming? Write a shell program to display list of users logged in to the system. 5 Marks Ans: When shell is used as a high level programming language then this is called as shell programming. Instead of entering command one at a time in response to shell prompt a number of commands are put in a file which are executed once by the shell. A program consists of shell commands is called a shell script. vi user AshuRaj 22

UNIX SOLUTION
press i to enter input mode echo The logged users are `who | cut d f 1` press esc to enter the ex mode then press :x to save the file and then execute the file. 34. Write a shell program to find biggest of three numbers. Ans: echo "Enter first number" read num1 echo "Enter second number" read num2 echo "Enter third number" read num3 if test $num1 -gt $num2 -a $num1 -gt $num3 then echo "$num1 is greater than $num2 and $num3" elif test $num2 -gt $num1 -a $num2 -gt $num3 then echo "$num2 is greater than $num1 and $num3" elif test $num3 -gt $num1 -a $num3 -gt $num1 then echo "$num3 is the greatest" else echo " " fi 35. Explain the following filters Write short notes on the following: pr: The pr command prepares a file for printing by adding suitable headers, footers and formatted text. This command has to be used with a file name as argument. pr adds five lines of margin at the top and five at the bottom. The header shows the date and time of last modification of the file along with the file name and page number. There is one option that uses a number prefixed by a + to print from a specific page number. Another option l set the page length. Example: $ pr emp.lst will shows date and time, file name and page number as header. $ pr l 54 emp.lst sets the printing of file from 54 lines $ pr +5 emp.lst starts printing from page 5 cat: cat command is used to concatenates or display the contents of a file. The syntax of cat is cat [-v] filename The cat command normally used to display the contents of text files. With v option, non printing characters can also be displayed. cat command can be used to create files. cat command is used to append to an existing file. Example: $ cat file1

AshuRaj

23

UNIX SOLUTION
Displays the contents of a file. $ cat > file2 Creates a file2 and when entered is pressed cat waits for user to type in the file. The file is saved and closed using ctrl-d. $ cat >> file2 Appends to the file2 head: The head command is used to show the top of a file. When head command is used without any argument then it displays top 10 lines of the file by default. head command uses option n to display first n number of lines of a file. Example: $ head file1 shows first ten lines from file file1. $ head n 5 kalpa shows first 5 lines from file kalpa.

tail : The tail command is used to show the end of a file. when tail command is used without any argument then it displays last 10 lines of the file. tail can be used to display lines from the beginning of the file instead of the end. The + count option allow to do that where count represents the line number from where the selection should begin. tail command uses option n to display last n number of lines of a file. example of tail command: $ tail kalpana displays last 10 lines by default. $ tail n 5 kalpana displays last 5 lines by default. $ tail +5 kalpana displays from fifth line to the end of the file. grep: grep command is used to search for a pattern in a given file. it displays the selected pattern, the line numbers or the filenames where the patterns occurs. The syntax of gerp is grep options pattern filename(s) grep searches for a pattern in a given file. The first argument is pattern and other is filename. gerp options: ignore case(i): it searches for a pattern ignoring the case of the searching pattern. Example: grep i kumar emp.lst output: KUMAR deleting lines(-v): grep can play inverse role too, the v option selects all excepts lines containing the pattern. Displaying line number (-n): The n option displays the line numbers and line of searched pattern. Counting lines containing pattern(-c): The c counts the number of line containing the pattern.

AshuRaj

24

UNIX SOLUTION
tr: The tr (translate) filter manipulates individual characters in a line. It translates characters based on expression specified. The general form is tr expression1 expression2 standard input tr takes input only from standard input. It does not take filename as argument. By default it translates each character in expression1 to its mapped counterpart in expression2. sort: sort command sort a file based on a condition. The sort uses space as default field separator. Sorting on primary key(-k): To sort second field of a file shortlist the following command can be written sort t | -k 2 shortlist To sort in reverse order r option is used. sort t | r -k 2 shortlist Sorting on secondary key(-k): Sorting can be done on more than one key. If the primary key is third column and secondary key is the second field then following command is written Sort t | k 3,3 k ,2,2 shortlist Numeric sort(-n): When sort acts on numerals then ASCII collating sequence place 1 above 2, and 2 above 4 and so on. sort numfile produces output 10 2 4 But sort n numfile will produce output like 2 4 10 -t char use delimiter char to identify fields -k n sorts on n field -k m,n starts sort on m field and ends sort on n field -k m.n sort on n column of m field -n numeric sort -r reverse sort -m list merge sorted list -c check if file is sorted -o filename places output in a file Uniq: uniq simply writes unique copy of each line from a file to standard output. Uniq requires a sorted file as input. The general procedure is to sort the file and pipe it to uniq. set: Positional parameter are sometimes called read only variables because shell sets their values when argument is typed. But value of argument can be set by the user itself using set command. As for example #!/bin/sh set `date`

AshuRaj

25

UNIX SOLUTION
echo Time: $4$5 here the backquotes runs the date command and catches the output and stores it in the positional parameter. Pine: pine is a character and menu base mailer. To create a mail using pine letter C is pressed then the address of the recipient is entered and if it is to be sent to more than one recipient then the address in entered. Any file can be attached then after subject of mail is entered which should be small. The message body is entered and it is send by pressing ctrl-c. A confirmation is asked before the delivery of mail. Pressing Y will send the mail and N cancels the delivery. The first column shows a + or blank specifying that a copy is sent or not. The second column gives the status of the message. N for new, A for answered and S for saved. The third column gives sender name and fourth column gives the date and time whereas fifth column specifies the size. To read a mail the mail is selected first then V is pressed this opens the mail for reading. To view an attachment V is pressed this list the attachment and to read an attachment, the attachment is first selected then V is pressed again. To exit attachment viewer and attachment index E is pressed twice. To reply a message R is pressed. To save a message S is pressed. To quit a pine Q is pressed. Mailx: mailx is an command line tool which can run interactively from shell scripts. Mailx can run in sending as well as running mode. Typing mailx user1 prompts user for subject of message. The message body is then entered. Ctrl-d denotes the end of message body. Mailx can be used to send mail interactively with option s. To receive mail using mailx the command is used without any argument. The first column gives the status of message where N denotes new mail, U denotes an unread old mail. The second column gives the mail number. Third column gives sender name, fourth column gives the date and time of mail received and fifth gives the lines and character the mail contains. script: script command is used to record a login session of a user. The different option that script command uses are following: syntax of script command: script [option] [argument] when script command is used without any option and argument then script is saved to a default file called typescript. script filename: writes the login session to the given filename. script a filename records the login session to the existing filename. passwd: passwd command is used to change the password of a user. System administrator can change the password of any one. The system prompts the user for old password and if the old password is entered correctly. The system prompts the user for new password and prompts the user to reenter the new password. If the new password entered gets matched then password is changed otherwise rejected. who: who command without any option returns the users logged currently on the system with their name, terminal number, date and time of login. Who am i returns the name of a user logged on a system along with terminal name, date and time.

AshuRaj

26

UNIX SOLUTION
Who q list the name and number of user logged. tput query the terminfo database. tput cols : prints the number of columns for the current terminal. tput clear: clears the screen. lock is used to lock a user terminal. The syntax for lock command is lock [-v] [number]. Lock command prompts the user for password and request it again for confirmation and then locks the terminal until password is reentered. If the number is given in locked command then terminal automatically logs out after given time. $ lock Password: Re-enter password: Terminal locked by user 0 minutes ago. lock v specifies verbose operation. $ lock -v Password: Re-enter password: Terminal locked by user 0 minutes ago. Terminal is logged out after 10 minutes. $ lock -10 Password: Re-enter password: Terminal locked by user 0 minutes ago. Absolute pathname: Absolute pathname is associated with root. Absolute pathname gives the location of a file in relation to the root. The pathname starts at the root and list each directory to destination. An absolute pathname uses a slash (/) between each directory name in the pathname to indicate different directory. A full pathname becomes very long and tedious. Example: /bin/usr Relative pathname: Relative pathname is associated with the current directory. Relative pathname depends on the current directory. A relative pathname starts with the current directory. In a relative pathname a single dot represents current directory and double dot represents the parent. For a file or directory in current directory the relative path of the file or directory is the name of file or directory itself. Example: $ pwd /user/user1/cprog $ cd .. $ pwd /user/user1

AshuRaj

27

UNIX SOLUTION
Pipes: A pipeline is the simplest and most used inter process communication mechanism in UNIX. A pipe is a communication channel defined by two file descriptors, one is open for output and one is for input. The output from one process goes an input to another process which means that data goes from one end of the pipe an comes out of the other. The data flowing in a pipe are directly managed by the kernel. The shell connects the input and output a special pipe character |. Pipes are used to avoid writing temporary files to communicate between programs. Example: $ ls la | more which takes the standard output from the ls command and transfers it to the standard input of the more command. The more command list the file one page at a time. 36. Write a shell script private that uses chmod to change the access permissions on a file so that only the owner may read, write, or execute it. Be sure to label the output to show what was done to the file. 15 Marks Ans: echo "Enter the name of the file whose permission needs to be changed" read file if test ! -f $file then echo "$file is not a file" else echo "Do u want to set the read, write and execute permission for the owner of file $file" echo "Enter ur choice" read choice if test $choice=Y -o $choice=y then chmod u+rwx $file echo "File permission changed" else echo "File permission not changed" fi fi LABEL OUTPUT: NAM E private changes the file permission of a file that only user can read write and execute. SYNOPSIS Filename Description Here is an outline for changing the file permission that an owner only can read, write and execute Select sh Enter the filename If the file does not exists print an error message

AshuRaj

28

UNIX SOLUTION
Otherwise Ask if user wants to change the file permission of the file Read the user choice (Y/y) If the choice is yes (Y/y) Then change the file permission and print a confirmation message Otherwise Print an error message 1. Use the echo command to display the line UNIX is fun to learn. Redirect the displayed line to a file. Append the outputs of the commands to show the users logged into your system, your login account and the current date in the dd-mm-yyyy format to the file. Ans: echo "UNIX is fun to learn" | cat > myfile who | cat >> myfile; who am i|cat >> myfile; date |cat >> myfile ....................................................................................................................... .................................. 2. Assuming you have a directory containing the following files: Sprite, Cola, Choc, orange, book, lemon, lotus, apple Use the ls command to list only those files that a) Consists of four letters b) Begin with the letter c c) End with the letter e d) Have the second letter o Ans: a) ls ???? b) ls c* c) ls *e d) ls [a-zA-Z][0]* ....................................................................................................................... .................................. 3. For a file named myfile in the working directory, do the following: a) Give everyone permission to read myfile. No other privilege is to be changed b) Allow the owner and group members to read and write the file. All privileges are to be removed for everyone else. c) Remove write privileges from everyone except the owner d) Allow the owner and group members to execute myfile and give only the owner permission to read or write to it. Ans: a).chmod a+r myfile b). chmod ug+rw,a-x,o-rw myfile c). chmod u+w,og-w myfile d). chmod ug+x,u+rw,og-rw,o-x myfile ....................................................................................................................... .................................. 4. For a directory myfolder in your working directory, do the following: a) Allow everyone to list files in myfolder. No other privileges are to be changed. b) Allow the owner to and group members to list, remove or add files. All privileges are to be removed from everyone else. c) Give write privileges to everyone except the owner

AshuRaj

29

UNIX SOLUTION
d) Allow the owner and group members to execute myfolder. Only the owners get read or write permission. Ans: a). chmod a+r myfolder b). chmod ug+rw,a-x,o-rw myfolder c). chmod u-w,og+w myfolder d). chmod ug+x,u+rw myfolder Put a long-running process in the background and check the accuracy of the sleep command. Ans: vi psall set `who | cut d f 1 | sort | uniq` for i in $* do sleep 10 echo ps u echo done :x ....................................................................................................................... .................................. 6. The ls command with the R option lists the files in the specified directory and also subdirectories, if any. Use this with a suitable pip-and-filter arrangement to determine whether a file exists in the account logged in. Ans: kalpana@ubuntu:~$ cat > kp apple zebra boy cat girl ls -R |sort kp | tee Sort apple boy cat girl zebra ....................................................................................................................... .................................. 8. Write a shell program to extract and sort all the users in the file system. Use the /etc/passwd file as input. Ans: cut -d ":" -f 1 /etc/passwd| sort ....................................................................................................................... .................................. 9. Write a shell program to translate the /etc/passwd file into uppercase and translate the : delimiter into tab characters. Ans:

AshuRaj

30

UNIX SOLUTION
tr "[a-z]"":" "[A-Z]""\t" < /etc/passwd ....................................................................................................................... .................................. 10. Write a shell program using if-the-else to test whether a variable name is a directory or a file. Ans: echo "Enter the name of file or directory" read str if test -f $str then echo "$str is a file" elif test -d $str then echo "$str is a directory" else echo "My name is kalpana" fi ....................................................................................................................... .................................. 12. Write a background command to edit all the files in the directory replacing the word while with until. tr 'while' 'until' < myfile & 13. Write a shell program to test for arguments arg1, arg2 and arg3. If they are not present, prompt the user for them. Ans: if test -z $1 then echo "No argument is passed" echo "Please enter three arguments" elif test -z $2 then echo "Only one argument is passed" echo "Please pass three argument"cho "Enter any command" elif test -z $3 then echo "Only two argument is passed" echo "Please pass three argument" else echo $1 echo $2 echo $3 fi ....................................................................................................................... .................................. 14. Write a shell program that adds the integers 1 to 100 and displays the result with a suitable message. Ans: sum=0 count=1

AshuRaj

31

UNIX SOLUTION
while test $count -le 100 do sum=`expr $sum + $count` count=`expr $count + 1` done echo $sum ....................................................................................................................... .................................. 16. Using a case statement, write a shell program to read a command (eg., who, cal, ls, ps) from the user and execute Ans: echo "Enter any command" read cmd case $cmd in who) echo "`who`";; cal) echo " `cal`";; ls) echo "`ls`";; ps) echo " `ps`";; *) echo "My name is kalpana" esac What is general purpose utilities and how to display the calander using GPU ? Explain with syntax and example. 15 What is a process? Explain the mechanism of process creation in UNIX. 8 Marks Explain the mechanism of executing job periodically using cron. 7 Marks How will you store the total size of all C source files (*.c) in the variable count? 5 Marks Using suitable commands explain the process of reading file lines in reverse order. 10 Write shell program to convert degree in Celsius to Fahrenheit. 5 Marks

AshuRaj

32

Оценить