David Notkin Autumn 2009 CSE303 Lecture 3 Dictionary.com, “shell," in Dictionary.com Unabridged. Source location: Random House, Inc.

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
Introduction to UNIX CSE 2031 Fall May 2015.
Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
CSE 303 Lecture 2 Introduction to bash shell
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Unix Files, IO Plumbing and Filters The file system and pathnames Files with more than one link Shell wildcards Characters special to the shell Pipes and.
UNIX Filters.
Introduction to Linux and Shell Scripting Jacob Chan.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
The “File System” Under UNIX, (almost) everything is a “file”: –Normal files –Directories –Hardware –Sockets –Pipes Things that are not files: –Users –Groups.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CS 2061 Shells Also known as: Unix Command Interpreter.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Guide to Linux Installation and Administration, 2e1 Chapter 7 The Role of the System Administrator.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
1 CSE 303 Lecture 3 bash shell continued: processes; multi-user systems; combining commands read Linux Pocket Guide pp , , , 118, 122,
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Four I/O Redirection1 System Programming Shell Operators.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Linux Commands C151 Multi-User Operating Systems.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
1 CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
Foreground and background processes
Exploring Shell Commands, Streams, and Redirection
Lesson 5-Exploring Utilities
slides created by Marty Stepp, modified by Josh Goodwin
The UNIX Shell Learning Objectives:
Exploring Shell Commands, Streams, and Redirection
CSE 374 Programming Concepts & Tools
CSE 303 Concepts and Tools for Software Development
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
slides created by Marty Stepp, modified by Jessica Miller
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, Redirection, and Processes
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
Linux Shell Script Programming
slides created by Marty Stepp, modified by Jessica Miller
CSE 391 Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 303 Concepts and Tools for Software Development
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 390a Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
CSE 391 Lecture 3 bash shell continued: processes; multi-user systems; remote login; editors slides created by Marty Stepp, modified by Jessica Miller.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
LPI Linux Certification
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

David Notkin Autumn 2009 CSE303 Lecture 3 Dictionary.com, “shell," in Dictionary.com Unabridged. Source location: Random House, Inc. Available: Accessed: October 04, a hard outer covering of an animal, as the hard case of a mollusk, or either half of the case of a bivalve mollusk any of various objects resembling such a covering, as in shape or in being more or less concave or hollow the hard exterior of an egg a hard, protecting or enclosing case or cover an attitude or manner of reserve that usually conceals one's emotions, thoughts, etc. a hollow projectile for a cannon, mortar, etc., filled with an explosive charge designed to explode during flight, upon impact, or after penetration small pieces of pasta having the shape of a shell the lower pastry crust of a pie, tart, or the like, baked before the filling is added Shell (nouns, selected)

Today Combining commands –input/output redirection –pipes Processes and basic process management

I/O streams: standard Three I/O streams: stdin, stdout, stderr The program itself has statements that read or write to these streams #include main() { printf(“Hello 303\n"); } printf is defined to write to stdout So the program doesn’t know or care where it is writing output Similarly, for reading input or writing errors (using, for example, scanf and fprintf ) CSE303 Au093 program stdin stdoutstderr

Output redirection This standard allows the shell to provide user-level redirection of I/O command > filename Run command and write its output to filename –That is, hook filename to stdout of command instead of defaulting to the console –Take care: existing files are overwritten >> appends to filename rather than overwriting it Again, the program representing command doesn’t manage – or even know anything about – such redirection

Examples ls -l > myfiles.txt java Foo >> Foo_output.txt cat > somefile.txt (writes console input to the file until you press ^D ) –Easy way to create a simple file without using an editor CSE303 Au095

Miscellaneous command > /dev/null suppresses the output –Why might you want to do this? –What is /dev/null ? Redirecting stderr : Same idea, with silly syntax (RTFM – Read The Manual) How might you do the equivalent of output redirection in a Windows environment? In any GUI-based environment? CSE303 Au096

Input redirection: same idea for stdin command < filename Run command and use filename as stdin –If the program reads from stdin, instead of awaiting input from the console, it will instead read the input from a file Only works for programs written in terms of stdin – if a program explicitly reads input from a specific file, that cannot be overridden by the shell Remember: arguments/parameters are passed in through the command line, and are unaffected by any redirection

Combine input and output redirection sort -r rev.dict CSE303 Au098

Combining commands wc /usr/share/dict/linux.words > t grep 0 < t When the output of one command is used as the input to the next command, there is a lovely shorthand – pipes (or sometimes pipelines) wc /usr/share/dict/linux.words | grep 0 This connects the stdout of wc to the stdin of grep CSE303 Au099

Examples ls –l | more grep free /sources/gnu/less/*.c | uniq | sort grep free /sources/gnu/less/*.c | uniq | sort | wc grep free /sources/gnu/less/*.c | sort | uniq | wc grep free /sources/gnu/less/*.c | grep -v freelist CSE303 Au0910

Multiple commands Less important than pipes, you can also run multiple unrelated commands in the shell command1 ; command2 –run command1 and then command2 afterward – there is no connection between the programs or their input/output streams command1 && command2 –run command1, and if and only if it succeeds, run command2 afterward Question: what does it mean for a command to “success” or “fail”? CSE303 Au0911

An unfair, but interesting, comparison “Given a text file and an integer k, print the k most common words in the file (and the number of their occurrences) in decreasing frequency.” –Jon Bentley, Programming Pearls ~1986 Donald Knuth solution –CACM, Programming Pearls, June 1986 (Bentley with Knuth and McIlroy) –Literate Programming –Key data structure: trie –Roughly eight pages, including documentation, index, etc. CSE303 Au0912

McIlroy’s quotations “I found Don Knuth’s program convincing as a demonstration of [literate programming] and fascinating for its data structure, but I disagree with it on engineering grounds.” “A first engineering question to ask is: how often is one likely to have to do this exact task’? Not at all often, I contend. It is plausible, though, that similar, but not identical, problems might arise. A wise engineering solution would produce – or better, exploit – reusable parts.” “The following shell script was written on the spot and worked on the first try.” CSE303 Au0913

McIlroy’s solution tr -cs A-Za-z\' '\n' | tr A-Z a-z | sort | uniq -c | sort -k1,1nr -k2 | sed ${1:-25}q Make one-word lines by transliterating the complement of the alphabet into newlines and squeezing out multiple newlines. Transliterate upper case to lower case. Sort to bring identical words together. Replace each run of duplicate words with a single representative and include a count Sort in reverse numeric order. Pass through a stream editor; quit after printing the number of lines designated by the script’s first parameter (default is 25) CSE303 Au0914 No, I don’t expect you to be able to do this! It’s to show some of the power.

Common misuses: pipes and cat bad: cat filename | command good: command < filename bad: cat filename | more good: more filename bad: command | cat good: command

Processes A set of Unix commands deal with processes – examples include ps, fg, bg, kill, … What is a process? Is it the same as a program? Actually, what is a program? –hello.c, hello.s, a.out, …

Rough idea: process A process is a running execution of a program –Lots of details about processes vary across operating systems – beyond the scope of 303 When you execute a command, a process is created, the program is instantiated and executed – when the program completes, the process is killed If you execute one command twice simultaneously – how would you do this? – then each execution takes place in its own process –Each has its own variables, own stdin/stdout, can take different branches, doesn’t know about the other, etc. CSE303 Au0917

Processes: a bit more The operating system has its own processes, too –Some manage disks, other manage processes, … –In Unix, OS processes are owned by root and each process has a unique ID (PID) And other users sharing the same operating system have their own processes The OS makes sure that each process gets its chance to execute on the CPU(s) – this is called scheduling CSE303 Au0918

Process commands commanddescription ps list processes being run by a user; each process has a unique integer id (PID) top show which processes are using CPU/memory; also shows stats about the computer Keeps executing until killed! kill terminate a process by PID killall terminate processes by name use kill or killall to stop a runaway process (infinite loop) similar to ^C hotkey

Background processes You would like some processes to continue while you are doing other things – maybe your editor, maybe a browser, etc. You can do this by running some processes “in the background”, so the shell doesn’t have to wait until those processes finish; ex: $ emacs & If you forget to use &, suspend your process with ^Z, then run bg commanddescription & (special character) when placed at the end of a command, runs that command in the background ^Z (hotkey) suspends the currently running process fg bg resumes the currently suspended process in either the foreground or background

Searching and sorting: repeat grep is a very powerful search tool; more over time commanddescription grep search a file for a given string sort convert an input into a sorted output by lines uniq strip duplicate lines find search for files within a given directory locate search for files on the entire system which shows the complete path of a command

Keyboard shortcuts: repeat ^KEY means hold Ctrl and press KEY keydescription Up arrow repeat previous commands Home / End or ^A / ^E move to start/end of current line " quotes surround multi-word arguments and arguments containing special characters * "wildcard", matches any files; can be used as a prefix, suffix, or partial name Tab auto-completes a partially typed file/command name ^C or ^\ terminates the currently running process ^D end of input; used when a program is reading input from your keyboard and you are finished typing ^Z suspends (pauses) the currently running process ^S don't use this; hides all output until ^G is pressed

File system: repeat CSE303 Au0923 directorydescription / root directory that contains all others (drives do not have letters in Unix) /bin programs /dev hardware devices /etc system configuration files  /etc/passwd stores user info  /etc/shadow stores passwords /home users' home directories /media,/mnt,... drives and removable disks that have been "mounted" for use on this computer /proc currently running processes (programs) /tmp, /var temporary files /usr user-installed programs

Questions? CSE303 Au0924