INTERNAL ASSESSMENT TEST - 2 Date : 20/09/2016 Max Marks : 0 Subject & Code : Unix Shell Programming (15CS36) Section : 3 rd Sem ISE/CSE Name of faculty : Prof Ajoy Time : 11:30am to 1:00pm SOLUTIONS 1 a Explain the significance of these two special files /dev/null and /dev/tty. Answers:- /dev/null: if we want to execute a command but don t like to see its contents on the screen, we may wish to redirect the output to a file called /dev/null. It is a special file that can accept any stream without growing in size. It s size is always zero. /dev/tty: this file indicates one s terminal. In a shell script, if we wish to redirect the output of some select statements explicitly to the terminal. In such cases you can redirect these explicitly to /dev/tty inside the script. b Explain the usage of wild card characters in - i) [A Z]????* iii) *[0 9]* ii) *[!0 9] iv) *[!s] [!h] Answers : - i) Refers to all the files or names starting with any alphabet from A to Z (in uppercase) followed by characters and further followed by any number of characters in any form (numbers, characters, special characters etc) ii) iii) iv) It refers to all the files or names starting with any character and containing any number of characters but should not end with a number i.e anything from 0 to 9 should not be there at end. It refers to all the files or names that can contain any character, any number of times in the beginning as well as in the end but should contain one number between 0 to 9 in the name. It refers to all the files or names that can be beginning with any character and can contain any number of characters but should not contain s as second last character and h as last character. 2 What is a process? Mention briefly the role of fork - exec mechanism in process creation. Answers:- A process is a program in execution. A process is said to be born when the program starts ececution 8
and remains alive as long as the program is active. After execution is complete, the process is said to die. The kernel is responsible for the management of the process. Two important attributes of process are:- a) The process-id (PID) : each process is uniquely identified by a unique integer called the PID, that is allocated by the kernel when the process is born. The PID can be used to control a process. b) The parent PID (PPID) : the PID of the parent is available as a process attributes. There are three types of process :- a) Interactive b) Batch c) Daemon There are three distint phase in the creation of a process and uses three important system calls i.e. fork, exec, and wait. The three phases are discussed below: a) Fork: a process in unix is created with the fork system call which creates a copy of the process that creates it. The process image is identical to that of the calling process, except for a few parameters like the PID. The child gets a new PID. b) Exec: the forked child overwrites its own image with the code and data of the new program. This mechanism is called exec, and the child process is said to exec a new program, using one of the family of exec system calls. The PID and PPID of the exec d process remains unchanged. c) Wait: the parent then executes the wait system call to wait for the child to complete. It picks up the exit status of the child and continues with its other functions. A parent need not to decide to wait for the child to terminate. The important attributes that are inherited from parent process to child process are ;- The real UID and real GID of the process The effective UID and GID of the process 3 What are file attributes? Explain how to change basic file permissions, with an example. Answers: - File attributes are the features of file which on combination help us uniquely identify a file. Every file is associated with a table that contains all that we could possibly need to know about a file except its name and contents. This table is called the inode and is accessed by the inode number. The inode contains Unix the following attributes :- a) File type b) Permissions c) Links d) User e) Group f) File Size in bytes g) Last modified h) Date and time of last accessed i) An array of pointers that keep track of all disk blocks used by the file Most file systems have methods to assign permissions or access rights to specific users and groups of users. These systems control the ability of the users to view, change, navigate, and execute the contents 8
of the file system. Permissions on Unix-like systems are managed in three distinct scopes or classes. These scopes are known as user, group, and others. Files and directories are owned by a user. The owner determines the file's user class. Distinct permissions apply to the owner. Files and directories are assigned a group, which define the file's group class. Distinct permissions apply to members of the file's group. The owner may be a member of the file's group. Users who are not the owner, nor a member of the group, comprise a file's others class. Distinct permissions apply to others. Permissions :- Unix-like systems implement three specific permissions that apply to each class: The read permission grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory, but not to find out any further information about them such as contents, file type, size, ownership, permissions. The write permission grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files. The execute permission grants the ability to execute a file. This permission must be set for executable programs, including shell scripts, in order to allow the operating system to run them. When set for a directory, this permission grants the ability to access file contents and metainformation if its name is known, but not list files inside the directory, unless read is set also. Each of the three characters represent the read, write, and execute permissions: r if reading is permitted, - if it is not. w if writing is permitted, - if it is not. x if execution is permitted, - if it is not. In some permission systems additional symbols in the ls -l display represent additional permission features. Another method for representing Unix permissions is an octal (base-8) notation as shown by stat -c %a. This notation consists of at least three digits. Each of the three rightmost digits represents a different component of the permissions: owner, group, and others. Each of these digits is the sum of its component bits in the binary numeral system. As a result, specific bits add to the sum as it is represented by a numeral: The read bit adds to its total (in binary 100), The write bit adds 2 to its total (in binary 010), and The execute bit adds 1 to its total (in binary 001). Changing File Permissions
To change the file permission on any of the files you own, you can use the chmod command. The command syntax consists of the group/person to which permission is being granted or revoked, and the type of permission being granted or revoked. The letter u is for the owner, g for the group, and o for others (public). It can be done in two ways: relative and absolute To change permission on a file or directory, specify the the group, then + or - to indicate whether the permission is to be granted or revoked, then the access type. For example, to grant your group read permission to the files directory, you could enter chmod g+r files. Or, to remove public executable access to script, you could enter chmod o-x script a What are hard links and soft links? Explain with examples. Answers: - Hard links: indistinguishable from other directory entries, because every directory entry is hard link "original" can be moved or deleted without breaking other hard links to the same inode only possible within the same filesystem permissions must be the same as those on the "original" (permissions are stored in the inode, not the directory entry) ln command is used for making hard links to a file files have the same inode number in hard link can only be made to files, not directories Symbolic links (soft links) simply records that point to another file path. (ls -l will show what path a same link points to) will break if original is moved or deleted. (In some cases it is actually desirable for a link to point to whatever file currently occupies a particular location) can point to a file in a different file system ln s command is used to create soft links to files. can point to a directory on some file system formats, it is possible for the same link to have different permissions than the file it points to (this is uncommon) b Write a short note on find command Answers :- Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory
hierarchy. The syntax of find command is find [pathnames] [conditions][action] By using conditions like name, -perm, -size etc, we can make searches according to name, permissions, size etc. Path name is provided to search the file in that specified path. Action determines what to do with the found files like to print them as output etc. example:- $ find perm 777 print This example will find all files in the current directory with rw*rw*rw permissions and print those files as output.
5 What is grep. Give some options used by grep? Locate lines longer than 100 and smaller than 150 characters using grep. Answers: - Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. The options used by grep command is :- -r reversing the searching criteria -n it gives no of lines -i (ignore-case)ignore case distinctions in both the PATTERN and the input files. -c Suppress normal output; instead print a count of matching lines for each input file. With the -v, -- invert-match option There are many more options used by grep command. Example: - $ grep manager emp.lst (searches for manager in the file and outputs the corresponding lines) $grep ^[A-Z] emp.lst (outputs line beginning with capital letters) 8 Grep command for locating lines longer than 100 and smaller than 150 is: - grep -E '^.{101,19}$' infile or, grep x.\{100,150\} -E this is the same as sed's -r option but for grep command. So if you don't want to use is just escape the curly braces.