PESIT Bangalore South Campus

Similar documents
PESIT Bangalore South Campus

Unix Internal Assessment-2 solution. Ans:There are two ways of starting a job in the background with the shell s & operator and the nohup command.

5/20/2007. Touring Essential Programs

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

UNIX Shell Programming

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

5/8/2012. Exploring Utilities Chapter 5

UNIX shell scripting

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Vi & Shell Scripting

Scripting Languages Course 1. Diana Trandabăț

FILTERS USING REGULAR EXPRESSIONS grep and sed

Unix Processes. What is a Process?

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

Bourne Shell Reference

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Basics. I think that the later is better.

Introduction to UNIX Part II

UNIX II:grep, awk, sed. October 30, 2017

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

Shell scripting Scripting and Computer Environment - Lecture 5

CSC UNIX System, Spring 2015

Understanding Regular Expressions, Special Characters, and Patterns

Shell Programming Overview

System Programming. Unix Shells

Grep and Shell Programming

Chapter 9. Shell and Kernel

Std: XI CHAPTER-3 LINUX

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Part III. Shell Config. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

BASH SHELL SCRIPT 1- Introduction to Shell

PERL Bioinformatics. Nicholas E. Navin, Ph.D. Department of Genetics Department of Bioinformatics. TA: Dr. Yong Wang

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Systems Programming/ C and UNIX

5/8/2012. Encryption-based Protection. Protection based on Access Permission (Contd) File Security, Setting and Using Permissions Chapter 9

Essential Unix and Linux! Perl for Bioinformatics, ! F. Pineda

Shells and Shell Programming

COMP 4/6262: Programming UNIX

Basic Linux (Bash) Commands

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

Shells and Shell Programming

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Shell Start-up and Configuration Files

A Brief Introduction to the Linux Shell for Data Science

Module 8 Pipes, Redirection and REGEX

COMS 6100 Class Notes 3

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

AC109/AT109 UNIX & SHELL PROGRAMMING DEC 2014

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Introduction to Linux

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Regular Expressions. Regular expressions are a powerful search-and-replace technique that is widely used in other environments (such as Unix and Perl)

Implementation of a simple shell, xssh

CSC209 Review. Yeah! We made it!

Shell Programming (bash)

CS Unix Tools & Scripting

CS160A EXERCISES-FILTERS2 Boyd

Lecture 5. Essential skills for bioinformatics: Unix/Linux

do shell script in AppleScript

Chapter 4. Unix Tutorial. Unix Shell

Shell Programming an Introduction

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

5/8/2012. Specifying Instructions to the Shell Chapter 8

St. MARTIN S ENGINEERING COLLEGE Dhulapally,Secunderabad DEPARTMENT OF INFORMATION TECHNOLOGY Academic year

Shells & Shell Programming (Part B)

Shells and Processes. Bryce Boe 2012/08/08 CS32, Summer 2012 B

Pattern Matching. An Introduction to File Globs and Regular Expressions

UNIX files searching, and other interrogation techniques

Project 1: Implementing a Shell

Pattern Matching. An Introduction to File Globs and Regular Expressions. Adapted from Practical Unix and Programming Hunter College

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Operating Systems, Unix Files and Commands SEEM

CST Algonquin College 2

Getting Started. Logging In and Out. Adapted from Practical Unix and Programming Hunter College

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Regular Expressions Explained

Introduction to Linux

Introduction to TURING

System Programming. Introduction to Unix

Bamuengine.com. Chapter 7. The Process

My Favorite bash Tips and Tricks

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

ECE 364 Software Engineering Tools Lab. Lecture 2 Bash II

Perl and R Scripting for Biologists

More Scripting Techniques Scripting Process Example Script

Introduction to Linux

Essential Linux Shell Commands

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Mastering Linux. Paul S. Wang. CRC Press. Taylor & Francis Group. Taylor & Francis Croup an informa business. A CHAPMAN St HALL BOOK

User Commands sed ( 1 )

Transcription:

INTERNAL ASSESSMENT TEST - III Date : 09-11-2015 Marks : 0 Subject & Code : USP & 15CS36 Class : III ISE A & B Name of faculty : Prof. Ajoy Kumar Note: Solutions to ALL Questions Questions 1 a. Explain the string handling function of PERL with appropriate examples Marks Perl provides a very useful set of string handling functions: The first set of functions that we'll look at are those that deal with strings. These functions let you determine a string's length, search for a sub-string, and change the case of the characters in the string, among other things. Here are Perl's string functions: chomp(string) OR chomp(array) -- Uses the value of the $/ special variable to remove endings from STRING or each element of ARRAY. The line ending is only removed if it matches the current value of $/. chop(string) OR chop(array)-- Removes the last character from a string or the last character from every element in an array. The last character chopped is returned. chr(number) -- Returns the character represented by NUMBER in the ASCII table. For instance, chr(65) returns the letter A. crypt(string1, STRING2) -- Encrypts STRING1. Unfortunately, Perl does not provide a decrypt function. index(string, SUBSTRING, POSITION) -- Returns the position of the first occurrence of SUBSTRING in STRING at or after POSITION. If you don't specify POSITION, the search starts at the beginning of STRING. join(string, ARRAY) -- Returns a string that consists of all of the elements of ARRAY joined together by STRING. For instance, join(">>", ("AA", "BB", "cc")) returns "AA>>BB>>cc". lc(string) -- Returns a string with every letter of STRING in lowercase. For

instance, lc("abcd") returns "abcd". lcfirst(string) -- Returns a string with the first letter of STRING in lowercase. For instance, lcfirst("abcd") returns "abcd". length(string) -- Returns the length of STRING. rindex(string, SUBSTRING, POSITION) -- Returns the position of the last occurrence of SUBSTRING in STRING at or after POSITION. If you don't specify POSITION, the search starts at the end of STRING. split(pattern, STRING, LIMIT) -- Breaks up a string based on some delimiter. In an array context, it returns a list of the things that were found. In a scalar context, it returns the number of things found. substr(string, OFFSET, LENGTH) -- Returns a portion of STRING as determined by the OFFSET and LENGTH parameters. If LENGTH is not specified, then everything from OFFSET to the end of STRING is returned. A negative OFFSET can be used to start from the right side of STRING. uc(string) -- Returns a string with every letter of STRING in uppercase. For instance, uc("abcd") returns "ABCD". Ucfirst(STRING) -- Returns a string with the first letter of STRING in uppercase. For instance, ucfirst("abcd") returns "Abcd". b. What is Shell Script? What are the two ways of running a Shell Script. There are four ways to execute a shell script. Each way has it s own meaning as explained in this article. 1. Execute Shell Script Using File Name Use the shell script file name to execute it either by using it s relative path or absolute path as shown below. $ cd /home/sathiya $./scriptfile (or) $ /home/sathiya/scriptfile

2. Execute Shell SCript by Specifying the Interpreter You can also execute a unix shell script by specifying the interpreter in the command line as shown below. Execute using sh interpreter $ sh scriptfile Execute using bash interpreter $ bash scriptfile 3. Execute Shell Script Using../ (dot space dot slash) While executing the shell script using dot space dot slash, as shown below, it will execute the script in the current shell without forking a sub shell. $../scriptfile In other words, this executes the commands specified in the scriptfile in the current shell, and prepares the environment for you. dot space dot slash Usage Example: Typically we use this method, anytime we change something in the.bashrc or.bash_profile. i.e After changing the.bashrc or.bash_profile we can either logout and login for the changes to take place (or) use dot space dot slash to execute.bashrc or.bash_profile for the changes to take effect without logout and login. $ cd ~ $../.bashrc $../.bash_profile 2 a. Explain the Shell s features of for loop giving the suitable examples. The for loop operate on lists of items. It repeats a set of commands for every item in a list.

Syntax for var in word1 word2... wordn do Statement(s) to be executed for every word. done Here var is the name of a variable and word1 to wordn are sequences of characters separated by spaces (words). Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordn. Example Here is a simple example that uses for loop to span through the given list of numbers #!/bin/sh for var in 0 1 2 3 5 6 7 8 9 do echo $var done This will produce following result 0 1 2 3 5

6 7 8 9 b. Write a Shell Script to accept pattern and a file and search for the pattern in the file. The UNIX Grep command searches files for a user-specified text pattern. It returns a list of the matching words or shows each line of text that contains them. You can broaden the results by using wildcards. Grep also has the ability to count instances of a search phrase that appear in a file. The syntax for this UNIX command includes three parameters, but only the text pattern is required: grep [options] pattern [filename] If it contains multiple words or special characters, be sure to place single quotation marks before and after the pattern. The UNIX shell does not interpret dollar signs, brackets, parentheses, pipes or asterisks in the same way as letters. For example, an asterisk is treated as a wildcard when you don't use quotes. If you leave out the filename, UNIX will collect data from the standard input device. This is usually the keyboard. (1) Example:- Basic search with no options The following example shows what happens if you use grep to search for the word "unix" in a text file: $ cat examplefile.txt this is line 1 UNIX UNIX this is line 2 unix this is line 3 Unix Unix

this is line hello $ grep unix examplefile.txt this is line 2 unix By default, it displays the only line that contains "unix" in lowercase letters. (2) Example:- Ignore upper and lower case distinction One of the grep options allows you to search for words in a case-insensitive manner: $ grep -i unix examplefile.txt this is line 1 UNIX UNIX this is line 2 unix this is line 3 Unix Unix All lines containing the word unix, regardless of if it's lowercase, uppercase or a mix of both, are returned. 3 a Explain line addressing and context addressing in sed with example. Sed is a multipurpose tool which combines the work of several filters. It is derived from ed the original UNIX editor. Sed performs non interactive operations on a data stream. sed can select contiguous group of lines from anywhere in the file. $ sed n 9,11p emp.lst (it selects lines 9 through 11) sed can select multiple group of lines from anywhere in the file. $ sed n 1,2p 7,9p $p emp.lst The second form of addressing, called context addressing lets us specify one or

b two patterns to locate lines. The pattern must be bounded by / on either side. $sed n /director/p emp.lst We can specify a comma separated pair of context addresses to select a group of lines. Line and context addresses can also be mixed. Explain the expr command applicable to computation and string functions. expr command in unix or linux system is used for doing arithmetic operations and performing string functions. The syntax of expr command is expr [expression] Arithmetic Operator Examples: 1. Sum of numbers $ expr 5 + 3 8 $ expr 1 + 2 + 3 6 2. Multiplying numbers $ expr 7 \* 9 63 String Function Examples: 1. Length of string The length function is used to find the number of characters in a string. $ expr length linux

5 $expr length linux\ system 12 $expr length "linux system" 2. Find Substring You can extract a portion of the string by using the substr function. The syntax of substr function is substr string position length Here position is the character position in the string. length is the number of chracters to extract from the main string. An example is shown below: $ expr substr unixserver 5 6 server a Explain the chop() and split() functions with example 3 chop This function removes the last character of a string and returns that character. If given a list of arguments, the operation is performed on each one and the last character chopped is returned. #chop() EXAMPLES $a = "abcdefghij"; chop($a); print $a; #this would return 'abcdefghi' $a = "abcdefghij";

$b = chop($a); print $b; #this would return 'j' split This function splits a string expression into fields based on the delimiter specified by PATTERN. If no pattern is specified whitespace is the default. An optional limit restricts the number of elements returned. A negative limit has the same effect as no limit. This function is often used in conjunction with join() to create small text databases. Syntax Following is the simple syntax for this function split /PATTERN/, EXPR, LIMIT split /PATTERN/, EXPR split /PATTERN/ split Return Value Return Value in Scalar Context: Not recommended, but it returns the number of fields found and stored the fields in the @_ array. Return Value in Array Context: A list of fields found in EXPR or $_ if no expression is specified. Example Following is the example code showing its basic usage #!/usr/bin/perl -w @fields = split(/:/, "1:2:3::5"); print "Field values are: @fields\n"; When above code is executed, it produces the following result

Field values are: 1 2 3 5 b Explain the three distinct phases of process creation. How is shell created? Process is simply an instance of a running program. A process is said to be born when the program starts execution and remains alive as long as the program is active. After execution is complete, the process is said to die. There are three distinct phases in the creation of a process and uses three important system calls viz., fork, exec, and wait. The three phases are discussed below: Fork: A process in UNIX is created with the fork system call, which creates a copy of the process that invokes 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. 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 remain unchanged. 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. Note that a parent need not decide to wait for the child to terminate. 5 5 Explain the terms: i Zombies On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the "Terminated state". This occurs for child processes, where the entry is still needed to allow the parent process to read its child's exit status: once the exit status is read via the wait system call, the zombie's entry is removed from the process table and it is said to be "reaped". A child process always first becomes a zombie before being removed from the resource table 2

ii iii iv Umask Return, or set, the value of the system's file mode creation mask. Description On Linux and other Unix-like operating systems, new files are created with a default set of permissions. Specifically, a new file's permissions may be restricted in a specific way by applying a permissions "mask" called the umask. The umask command is used to set this mask, or to show you its current value. umask Syntax umask [-S] [mask] Options -S = Accept a symbolic representation of a mask, or return one. Mask = If a valid mask is specified, the umask is set to this value. If no mask is specified, the current umask value is returned. Signal Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control. Pipe You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe. To make a pipe, put a vertical bar ( ) on the command line between two commands. When a program takes its input from another program, performs some operation on that input, and writes the result to the standard output, it is referred to as a filter. 2 2 2