Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.

Slides:



Advertisements
Similar presentations
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
Advertisements

UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Chapter 9: The TC Shell Everything you thought you knew is now wrong.
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.
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 4: More Command Line Interface (CLI) Chapter 7: The Linux Shell By Fred R. McClurg Linux.
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 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Understanding bash Prof. Chris GauthierDickey COMP 2400, Fall 2008.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
The UNIX Shell Software Tools. Slide 2 Basic Shell Syntax command [-[options]] [arg] [arg] … l The name of the command is first l Options are normally.
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
CS 497C – Introduction to UNIX Lecture 20: - The Shell Chin-Chih Chang
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
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.
Introduction to Linux and Shell Scripting Jacob Chan.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
Exam Revision. Exam Details Time 90 minutes (1hour 30 minutes). Six questions! How long per question? Five parts per question. How long for each part?
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
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.
Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
1 Unix Seminar #1 T.J. Borrelli Lecturer for CS and NSSA February 6th, 2009.
Hands On UNIX II Dorcas Muthoni. Processes A running instance of a program is called a "process" Identified by a numeric process id (pid)‏  unique while.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 1 of 44 Copyright ©2008 by Pearson Education, Inc. Upper.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
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.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Linux+ Guide to Linux Certification, Second Edition
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Agenda Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Learning Unix/Linux Based on slides from: Eric Bishop.
IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Introduction to Shells
CSE 374 Programming Concepts & Tools
Some Linux Commands.
Basic UNIX OLC Training.
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Chapter 3 The UNIX Shells
LPI Linux Certification
Presentation transcript:

Chapter 5: The Shell The Man in the Middle

In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion Builtins

Let us recall … Shell is an interpreter Sits between the user and the kernel We’ll be using bash as our point of reference

The Command Line It is what allows us to talk to the shell A command line is a string of commands and arguments ending with Enter Shell takes the command line, interprets it, then instructs the kernel

Syntax command [arg1] [arg2] … [argn] Spaces separate command and arguments Arguments can be optional Some arguments are called Options, and are proceeded by one or more hyphens (aka switches or flags)

Do I Need Arguments? Some commands work without any arguments specified (ex., ps) Others need one or more If a command expects at least one argument, often it will display a usage message if you execute the command without any arguments

Tokens Each space-delimited word in a command line is called a token and are numbered from left to right starting at zero That means the command is token 0, first arg is token 1, etc. Token can be a filename, a string, a number

Options An argument that modifies the effect or execution of the command Traditionally, options precede other arguments Most utilities use a single hyphen; some (such as several GNU utilities) also use double hyphens Sometimes you can combine options together with a a single preceding hyphen

Command Line Example ls -R -l -h public_html Could also be written as: ls –Rlh public_html LiSt Contents list directories recursively display long listing human readable format for sizes directory to perform this ls command upon

Mechanics Once enter is pressed, command line is parsed by the shell Reads the first token and tries to find the executable or builtin associated with it Passes remaining tokens to the called program, doing expansion and manipulation as necessary

Where’s the program? If not given absolute pathname, shell searches PATH variable for location of first token Can’t assume PATH includes your current working directory If it can’t locate it, shell returns command not found If no execute permissions, returns access denied

Execution Shell begins a new process and hands control over to called program Shell then goes to sleep, and waits for called program to finish running and return control Program also passes its exit status to shell

Streams Streams contain data (usually text) Three standard streams –Input –Output –Error Each stream can be associated with any number of things (remember, everything is a file!)

Standard Input Contains information to be passed to a program (a utility, a shell, etc) Most commonly is the keyboard Also commonly a file Because everything is treated as a file, programs never know exactly where input truly comes from

Standard Output Contains output from a program Again, program never sure where it’s sending output to Most commonly the screen Could also go to a printer, a file, or directly into standard input (to another program)

Standard Error Another output stream Contains error messages Usually also goes to screen You can redirect both standard out and standard error to different places

Fun with cat cat displays the contents of files Try calling cat with no argument It takes input from standard input (instead of a file) Will echo back every line you type Hit CTRL-D (EOF) to quit

Redirection Temporarily changes where standard streams point to After doing a redirection, streams return to normal There are ways to permanently change streams – beyond our scope

Redirecting Output Syntax: command [arguments] > output Output is usually a file, but can be anything (for example, a printer) The greater-than sign instructs the shell to redirect standard out to whatever is to the right If output exists, it will be overwritten – be careful!

Redirecting Output con’t To prevent overwriting, we can set the noclobber variable You can override noclobber using >| To append to the end of a file, use >> You can’t redirect output to a file you’re using as input – the shell allocates the file for the output stream before calling the program

Redirecting Errors Syntax: command [args] 2> errors You’re redirecting the second output stream (standard error) Standard output can also be written 1> You can redirect both streams: –find whizzbang > results 2> /dev/null

Redirecting Input Syntax: command [arguments] < input Input usually is a file Not all that useful if the command supports a filename for one of the arguments Can be handy if the command prompts for input … you can automate the process with a file filled with the answers

Pipes Redirects the output of one program to be the input of another Reduces the need for intermediary steps Handy for when you have several quick steps to perform upon some data Great for getting output and quickly filtering it (ie. command | grep search_string )

Getting Crazy tee allows you to send output to two different locations at once It sends output to a file, while still passing the output onto standard output Cascading tee commands can go on indefinitely

Running Jobs in the Background Thus far we’ve always run commands in the foreground Have to wait until it finishes before you can access the command line again To run in the background, place a & at the end of your command line before hitting enter

Background, con’t Once you hit enter, it will display the job number, and the process ID number (PID) You may resume issuing commands Once the job finishes, the job number and the word Done will appear on your screen Remember, to check on things, use jobs

Back and Forth Recall that pressing CNTRL-Z suspends a process, and it gives it a job number To start it again in the foreground, use: fg job_num To start it again in the background, use: bg job_num

kill Revisited Recall we could do: kill %job_num You can also: kill PID Forgot the PID? Just use ps Won’t die? Don’t forget kill -KILL

Metacharacters Special characters interpreted by the shell Also called wildcards Shell expands ambiguous file references into a list of files matching that criteria Process is called globbing or expansion

? Character Used to represent any single character For example: foo? –Would match food, fool, foot –Would NOT match fooey, footsie Can be anywhere in the filename Ex: b??kk??pper On exception – doesn’t match leading periods (for hidden files)

* Character Similar to ?, but matches any number of characters This includes zero characters Ex: foo* –Would match food, foot, footsie, foo Also does not match leading periods

[ ] Characters Used to match a list of possible characters Also known as a character class Ex: [aeiou].jpg –Matches a.jpg, e.jpg, i.jpg, etc Can also do ranges of letters / numbers –Ex: [0-9] [a-z] [A-Z]

[ ] Characters con’t You can negate a character set by placing a ! or ^ after the first bracket Ex: [^0-9] –Does not match the numbers 0-9

Metacharacter Tips You can combine multiple metacharacters of different types to get really specific The shell does all the expanding, not the program or utility Programs never see the metacharacters To let the program see them, you must quote them or escape them

Builtins Commands that are built into the shell Shell does not create a new process to run a builtin Run very quickly Shell will still do metacharacter expansion for builtins We’ll cover them in greater detail in Ch 10