CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.

Slides:



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

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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.
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.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Guide To UNIX Using Linux Third Edition
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.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Introduction to Linux and Shell Scripting Jacob Chan.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
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.
Guide To UNIX Using Linux Fourth Edition
BIF703 stdin, stdout, stderr Redirection. stdin, stdout, stderr Recall the Unix philosophy “do one thing well”. Unix has over one thousand commands (utilities)
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Workbook 6 – Part 2 The Bash Shell
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
40 Years and Still Rocking the Terminal!
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 5 – Regular Expressions, grep, Other Utilities.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2 – Processes, Programs, the Shell (& emacs)
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 4 – Shell Variables, More Shell Scripts.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
1 Week 8 Creating Simple Shell Scripts. 2 Chapter Objectives  In this chapter, you will :  Learn how to create Shell Scripts  Commenting / Making Portable.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Learning Unix/Linux Based on slides from: Eric Bishop.
Linux Administration Working with the BASH Shell.
Introduction to the bash Shell (Bourne-Again SHell)
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
CIRC Summer School 2016 Baowei Liu
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
CIRC Winter Boot Camp 2017 Baowei Liu
CSC 352– Unix Programming, Fall 2012
Part 1: Basic Commands/Utilities
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Some Linux Commands.
The Linux Operating System
CSE 303 Concepts and Tools for Software Development
Sarah Diesburg Operating Systems CS 3430
What is Bash Shell Scripting?
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Lab 5: Complex Inputs.
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
LPI Linux Certification
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts

News Reminder: HW1 due Thursday night (basic shell commands) Be sure you’re reading the Linux Pocket Guide and looking at Linux man pages – lectures/slides won’t include enough details to understand everything Check the web calendar: slides & sample code/files posted night before most lectures, shell history after –Check out Friday’s slides: extra set with summary of common linux commands (won’t flip through in class) 2

Standard I/O streams and redirection Recall: every command has 3 standard streams: stdin (0, input), stdout (1, output), stderr (2, error messages) Default is keyboard (stdin), screen (stdout, stderr) Can redirect to a file with echo hello > there cat here Can “pipe” output (stdout) of one command to input (stdin) of another with | man bash | less Done entirely in the shell – programs are oblivious; they just use streams 0,1,2 3

File redirection in (more) detail Somewhat cryptic; some common usages: –redirect input: cmd < file –redirect output, overwriting file: cmd > file –redirect output, appending to file: cmd >> file –redirect error output: cmd 2> file –redirect output and error output to file: cmd &> file –... See bash manual sec. 3.6 for other variations Useful special file: /dev/null –Immediate eof if read; data discarded if written 4

Pipes cmd1 | cmd2 Change the stdout of cmd1 and the stdin of cmd2 to be the same, new stream! Very powerful idea: –In the shell, larger command out of smaller commands –To the user, combine small programs to get more usefulness Each program can do one thing and do it well! Examples: –ps aux | less –djpeg me.jpg | pnmscale -xysize | cjpeg > thumb.jpg 5

Combining commands Combining simpler commands to form more complicated ones is very programming-like. In addition to pipes, we have: cmd1 ; cmd2 (sequence) cmd1 || cmd2 (or, using int result – the “exit status” – run cmd2 if cmd1 “fails”) Example: do_something || echo “Didn’t work!” cmd1 && cmd2 (and, like or; run cmd2 only if cmd1 “succeeds” – i.e., “returns” 0) Example: check_if_ok && launch_missles cmd1 ‘cmd2‘ (use output of cmd2 as input to cmd1). (Note cmd2 surrounded by backquotes, not regular quotes) Useless example: cd ‘pwd‘ Non-useless example: mkdir ‘whoami‘A‘whoami‘ 6

(Non)-alphabet soup List of characters with special (before program/built-in runs) meaning is growing: ‘ ! % & * ~ ? [ ] " ’ \ > < | $ (and we’re not done) If you ever want these characters or (space) in something like an argument, you need some form of escaping; each of " ’ \ have slightly different meaning First approximation: –"stuff" treats stuff as a single argument but allows some substitutions for $variables example: cat "to-do list" # filename with spaces(!) –’stuff’ suppresses basically all substitutions and treats stuff literally 7

Shell Expansion and Programs Important but sometimes overlooked point: shell metacharacter expansion, I/O redirection, etc. are done by the shell before a program is launched –The program usually never knows if stdin/stdout are connected to the keyboard/screen or files –Program doesn’t see original command line – just expanded version as a list of arguments –Expansion is uniform for all programs since it’s done in one place – the shell 8

Shell as a programming language The shell is an interpreter for a strange programming language (of the same name). So far: –“Shell programs” are program names and arguments –The interpreter runs the program (passing it the arguments), prints any output, and prints another prompt. The program can affect the file-system, send mail, open windows, etc. –“Builtins” such as cd, exit give directions to the interpreter. –The shell interprets lots of funny characters differently, rather than pass them as options to programs. It’s actually even more complicated: –(two kinds of) variables –some programming constructs (conditionals, loops, etc.) 9

Toward Scripts… A running shell has a state, i.e., a current –working directory –user –collection of aliases –History –Streams (files, etc.) –... In fact, next time we will learn how to extend this state with new shell variables. We learned that source can execute a file’s contents, which can affect the shell’s state. 10

Running a script What if we want to run a bunch of commands without changing our shell’s state? Answer: start a new shell (sharing our stdin, stdout, stderr), run the commands in it, and exit Better answer: Automate this process –A shell script as a program (user doesn’t even know it’s a script). –Now we’ll want the shell to end up being a programming language –But it will be a bad one except for simple things 11

Writing a script Make the first line exactly: #!/bin/bash Give yourself “execute” permission on the file (chmod +x) Run it –Probably need to precede filename with./ if current directory isn’t normally searched for commands (i.e., ‘.’ is not normally included in $PATH – and it shouldn’t be for security reasons) Note: The shell consults the first line of the file: –If a shell-program is there, launch it and run the script (similar trick works for perl, python, etc.) –Else if it’s a “real executable” run it (more later) Example: listhome 12

More expressions bash expressions can be: –math or string tests (e.g., -lt) –logic (&&, ||, !) (if you use double-brackets) –file tests (very common; see Pocket Guide) –math (if you use double-parens) Gotcha: parens and brackets must have spaces before and after them! Example: dcdls (double cd and ls) can check that arguments are directories Exercise: script that replaces older file with newer one Exercise: make up your own 13

Accessing arguments The script accesses the arguments with $i to get the ith one (name of program is $0) –Example: make thumbnail1 Also very useful for homework: shift (manual Section 4.1) –Example: countdown We would like optional arguments and/or usage messages. Need: –way to find out the number of arguments –a conditional –some stuff we already have –Example: make thumbnail2 14

Review The shell runs programs and builtins, interpreting special characters for filenames, history, I/O redirection Some builtins like if support rudimentary programming A script is a program to its user, but is written using shell commands So the shell language is okay for interaction and “quick-and-dirty” programs, making it a strange beast. For both, shell variables are extremely useful 15

Preview: Variables i=17 # no spaces set echo $i set | grep i echo $i unset i echo $i f1=$1 (The last is very useful in scripts before shifting) Enough for next homework (arithmetic, conditionals, shift, variables, redirection,...) Gotcha: using undefined variables (e.g., because of typo) doesn’t fail (just the empty string) 16