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

Similar documents
Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

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

Common File System Commands

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Module 8 Pipes, Redirection and REGEX

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

5/20/2007. Touring Essential Programs

Shells and Shell Programming

Useful Unix Commands Cheat Sheet

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

Introduction: What is Unix?

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Shells and Shell Programming

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Basic Linux (Bash) Commands

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Consider the following program.

Command-line interpreters

Computer Systems and Architecture

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

Introduction to Linux

UNIX System Programming Lecture 3: BASH Programming

The Unix Shell. Pipes and Filters

Unix Introduction to UNIX

Basics. I think that the later is better.

bash, part 3 Chris GauthierDickey

Shell Programming Systems Skills in C and Unix

UNIX files searching, and other interrogation techniques

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

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

Perl and R Scripting for Biologists

Unix System Architecture, File System, and Shell Commands

Lab 2: Linux/Unix shell

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

Operating Systems. Copyleft 2005, Binnur Kurt

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

CST Algonquin College 2

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.

Linux Command Line Primer. By: Scott Marshall

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

LOG ON TO LINUX AND LOG OFF

Computer Systems and Architecture

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Shell Programming Overview

Introduction to Linux

PESIT Bangalore South Campus

ITST Searching, Extracting & Archiving Data

2) clear :- It clears the terminal screen. Syntax :- clear

A Brief Introduction to the Linux Shell for Data Science

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

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

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

Assignment clarifications

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Chapter 9. Shell and Kernel

Files and Directories

5/8/2012. Exploring Utilities Chapter 5

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

Introduction to UNIX Part II

Linux shell programming for Raspberry Pi Users - 2

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

for more :-

Introduction To. Barry Grant

Scripting Languages Course 1. Diana Trandabăț

LAB 8 (Aug 4/5) Unix Utilities

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Lec 1 add-on: Linux Intro

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

CS395T: Introduction to Scientific and Technical Computing

System Administration

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

CST Lab #5. Student Name: Student Number: Lab section:

Introduction to the Shell

Linux Text Utilities 101 for S/390 Wizards SHARE Session 9220/5522

LAB 8 (Aug 4/5) Unix Utilities

CSE 15L Winter Midterm :) Review

(a) About Unix. History

Part 1: Basic Commands/U3li3es

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

Introduction to the UNIX command line

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

ScazLab. Linux Scripting. Core Skills That Every Roboticist Must Have. Alex Litoiu Thursday, November 14, 13

Processes and Shells

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

UNIX shell scripting

Recap From Last Time:

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

BGGN 213 Working with UNIX Barry Grant

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

The input can also be taken from a file and similarly the output can be redirected to another file.

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Review of Fundamentals

Transcription:

Summer 2010 Department of Computer Science and Engineering York University Toronto June 29, 2010 1 / 36 Table of contents 1 2 3 4 2 / 36 Our goal Our goal is to see how we can use Unix as a tool for developing programs 4 / 36

Processes Each running program on a Unix system is called a process Processes are identified by a number (process id or PID) Usually many processes running simultaneously Each process has a unique PID 5 / 36 Current Working Directory Every process has a current working directory In a shell, the command ls shows the contents of the current working directory pwd shows the current working directory cd changes the current working directory 6 / 36 Path names A path name is a reference to something in the filesystem A path name specifies the set of directories you have to pass through to find a file Directory names are separated by / in Unix Path names beginning with / are absolute path names. Path names that do not begin with / are relative path names (Start search in current working directory) 7 / 36

Special characters. means the current directory.. means the parent directory ~ means home directory Try 1 l s. 2 l s.. 3 l s 8 / 36 Devices in /dev contains devices Look like files but really communicate with devices. For example: /dev/tty the terminal (or virtual terminal) you are currently using /dev/zero an input stream which returns an endless stream of null bytes ( \0 ) /dev/null the bitbucket discards any input, generates no output (empty) 9 / 36 Use of dev/null To discard stdout of a command: cat hello.c >/dev/null To provide no input to a command: cat </dev/null 10 / 36

intro What is shell? Ordinary program which acts as a command interpreter and offers multiple benefits including Filename shorthand I/O redirection Personalizing the environment Programming language 12 / 36 There are many programs which are shells The most common Unix shells are: Bourne shell (sh) C shell (csh) Korn shell (ksh) Also: Bourneagain shell (bash). In this course we are mostly concerned with the Bourne shell (sh) 13 / 36 How does it work? When a command is entered shell does Process metacharacters Command line substitution Command execution 14 / 36

Special Characters > >> < IO redirection *? [...] Filename shorthand command Command substitution && Conditional execution (...) Group commands & Background processing <<tok Here document $ Expand value \ # ; Escape, comment, terminator " Single/double quotes 15 / 36 Basic tools: ls, cp, mv,... Advanced tools: grep, sort, cut, uniq, tr, find, xargs, sed, awk 17 / 36 Basic Unix (1) ls list directory content cp file copy mv file renaming, moving rm delete files mkdir create a new directory cd change directory pwd print current working directory cat print text files more print text files page by page less view text files head print first part of a text file tail print last part of a text file 18 / 36

19 / 36 Basic Unix (2) expr evaluate an expression echo display a line of text date print and set system date and time ps process status kill kill a process (send a signal) top display top CPU processes od octal dump of a file du disk usage chmod change file access permission chgrp change group ownership ln link files diff difference of two files basename base name of a full path name Combining If we just run a command, e.g. wc then the terminal is used for stdin, stdout, and stderr by default. However, we dont need to use the terminal. We can control what stdin, stdout, and stderr refer to. The simple case is redirection using a file wc <foo/bar/file i.e. use the contents of the file "foo/bar/file" instead of the terminal for stdin wc >foo/bar/file i.e. put the output of stdout into "foo/bar/file" instead of the terminal 20 / 36 Pipes We can also redirect stdout or stdin to other programs (instead of files) cat myfile wc this takes the stdout of the cat myfile command and makes it the stdin of the wc command 21 / 36

Shell Scripting A shell provides Basic interactive shell Programming environment 22 / 36 Unix Filters Filters are a large family of programs that: Read text input line by line Perform some transformation Write some output Simple filters: grep,cut,sort,uniq,tr $ grep tom /etc/passwd print lines containing tom Programmable filters: awk, sed $ awk /tom/ {print} /etc/passwd 23 / 36 Design of Filters Each processes argument files or stdin if arguments are missing Each writes to stdout. Arguments never specify stdout, unless there is an option (e.g. -o ) Some optional arguments (I.e. options, -a -n... ) may precede input filename(s) Error messages are written to stderr 24 / 36

grep: searches text files for pattern and prints all lines that contain that pattern egrep (expression grep): same as grep but supports full regular expressions fgrep (fast grep): searches for a string, instead of pattern 26 / 36 grep Prints out all lines in the input that match the given regular expression grep [options] pattern [file... ] e.g. grep hello Prints out all lines of stdin containing hello Exit status grep exits with a value: 0 if pattern found 1 if pattern not found 2 if file not found Can be used in scripts! 27 / 36 Frequently Used Options -i ignore case of letters (case insensitive search) -v invert search (print lines that dont match) -c displays count of matching lines -w search for expression as distinct word -n precede each line with line number -l list only input filenames where matches occur -h do not display filenames -s work silently (suppress error messages) 28 / 36

Regular Expressions A regular expression is a special string (like a wildcard pattern) A compact way to represent text patterns A compact way of matching several text lines with a single string Provide a mechanism to select specific strings from a set of character strings 29 / 36 Basic RE vs. Extended RE Basic Letters and numbers are literal that is they match themselves: e.g. "foobar" matches "foobar". matches any single character (i.e. exactly one) [xyz] matches any character in the set (ranges via - ) [^xyz] matches any character not in the set * matches 0 or more occurrences of last char? matches 0 or 1 occurrences of last char + matches 1 or more occurrences of the last char ^ matches the beginning of the line $ matches the end of the line "\<" and "\>" match begin and end of a word \{n\} matches exactly n occurrences \{n,\} matches at least n occurrences \{n,m\} matches occurrences between n and m 30 / 36 Interesting uses grep -v ^#! Removes all lines beginning with # grep -v ^[ ]*$! Removes all lines which are either empty or contain only spaces (all empty lines) ls -l grep "^[^d]"! List only files that are not directories 31 / 36

fgrep Like grep, fgrep searches for things but does not do regular expressions just fixed strings. fgrep hello.*goodbye Searches for string "hello.*goodbye" does not match it as a regular expression 32 / 36 cut Used to split lines of a file A line is split into fields Fields are separated by delimiters A common case where a delimiter is a space or tab character Default delimiter is tab Syntax cut [-ffields] [-ccolumns][-dcharacter] [filename...] 33 / 36 sort Syntax sorts lines in a file Like before, if no files are given, sorts stdin and writes result to stdout By default sorts lines in ascending alphabetical order sort [options... ] [file... ] 34 / 36

sotr Options -r sort in reverse order (descending) -n treat each line as a number and sort numerically -kn sort based on the Nth field, e.g. -k2 or -k4 -t: specify field separator (default space and tab) 35 / 36 uniq Removes repeated lines in a file Syntax uniq [-c] [input [output]] Notice difference in args: 1st filename is input file 2nd filename is output file 36 / 36