CSE 303 Concepts and Tools for Software Development

Slides:



Advertisements
Similar presentations
2006-Jan-231 Shell Scripts Jacob Morzinski
Advertisements

1 CSE 303 Lecture 4 users/groups; permissions; intro to shell scripting read Linux Pocket Guide pp , 25-27, 61-65, , 176 slides created by.
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.
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.
Introduction to Linux Workshop February Introduction Rob Lane & The HPC Support Team Research Computing Services CUIT.
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.
– 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
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
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
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
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.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Chapter Four I/O Redirection1 System Programming Shell Operators.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Administration Working with the BASH Shell.
Introduction to the bash Shell (Bourne-Again SHell)
Introduction to the Linux Command Line Interface Research Computing Systems Bob Torgerson July 19, 2016.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CIRC Summer School 2016 Baowei Liu
Exploring Shell Commands, Streams, and Redirection
Implementation of a simple shell, xssh
CIRC Winter Boot Camp 2017 Baowei Liu
CSE 374 Programming Concepts & Tools
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Implementation of a simple shell, xssh (Section 1 version)
CIRC Winter Boot Camp 2017 Baowei Liu
Implementation of a simple shell, xssh
Part 1: Basic Commands/Utilities
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Some Linux Commands.
Shell Script Assignment 1.
stdin, stdout, stderr Redirection
Exploring Shell Commands, Streams, and Redirection
Basic UNIX OLC Training.
LING 408/508: Computational Techniques for Linguists
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
Chapter 3 The UNIX Shells
CSC 4630 Meeting 4 January 29, 2007.
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Exploring Shell Commands, Streams, and Redirection
LPI Linux Certification
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

CSE 303 Concepts and Tools for Software Development CSE 303 Lecture 3 10/2/2006 CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/2/2006 Lecture 3 – I/O and Scripting

Administravia Office hours after lecture Post Homework questions on Wiki Homework questions Restarting in Problem 1 man script – find option to read from file! How do I tell which commands I’ve used? Pay attention in lecture today! 10/2/2006 CSE 303 Lecture 3

Emacs command of the day M-q = word wrap Useful if you’re writing text Also see M-x auto-fill-mode 10/2/2006 CSE 303 Lecture 3

Last Time Shell as an interpreter Shell model (Files, Users, Processes) Shell Expansions/Substitutions File names Braces History Aliases 10/2/2006 CSE 303 Lecture 3

Today Command-line editing Input/Output Variables Quoting and escaping Rdirection Pipes Logic Expressions Variables Quoting and escaping Shell scripting introduction 10/2/2006 CSE 303 Lecture 3

Command Line Editing Shortcuts for navigating/editing commands Extremely helpful when typing Especially in terminals (e.g. when no arrow keys) No help in scripts Bash similar to emacs Interesting cases Up/Down arrows: access command history Tab: Filename completion (space in emacs) C-s: Stops displaying output until C-q 10/2/2006 CSE 303 Lecture 3

Input/Output Simple View More complete view Input: command line arguments Output: Return code (a.k.a. “exit status”) Convention: 0 = “success” More complete view Input: stdin stream (0) Output: stdout stream (1) Error message output: stderr stream (2) 10/2/2006 CSE 303 Lecture 3

Streams in the Shell Default Examples stdin: read from keyboard stdout, stderr: printed on command line Examples cat (no args): sends stdin to stdout (^d) mail : message body from stdin (^d) ls : files to stdout and “No match” to stderr 10/2/2006 CSE 303 Lecture 3

File Redirection Tell shell to use files, not keyboard/screen Redirect stdin: cat < file Redirect stdout (overwrite): history > file Redirect stdout (append): cat >> file Redirect stderr: man 2> file Redirect stdout and stderr: ls &> file Short for ls > file 2>&1 Yes, syntax is arcane; we’re stuck with it 10/2/2006 CSE 303 Lecture 3

Pipes: Where the Fun Begins “cmd1 | cmd2” cmd1 stdout goes to new stream cmd2 stdin comes from this new stream Very powerful idea! Create larger commands from smaller ones Each command does one thing well 10/2/2006 CSE 303 Lecture 3

Pipe/Redirection Examples Silly Equivalences (like 1 * y = y) cat y = cat < y x < y = cat y | x X | cat = x More useful examples ls –help | less djpeg tur.jpg | pnmscale -xysize 100 150 | cjpeg >thumb.jpg grep -e '^bash-3.1\$' problem1.txt | sort 10/2/2006 CSE 303 Lecture 3

Using stdout on Command Line “Backquoting” can sent output as argument Useless example: cd `pwd` Useful example: mkdir `whoami` 10/2/2006 CSE 303 Lecture 3

Using the Return Code Commands can be combined cmd1 ; cmd2 (sequence) cmd1 || cmd2 (OR: stop if any succeed) cmd1 && cmd2 (AND: stop if any fail) Particularly useful in scripts Previous line’s return code is in $? 10/2/2006 CSE 303 Lecture 3

Shell Variables Used to control shell state Assignment printenv Assignment i=42 (no spaces!) Q=“What is the answer?” Retrieval uses $ echo $PATH, echo ${PATH} A=“The answer is $i” 10/2/2006 CSE 303 Lecture 3

Variables Details By default, other shells can’t see variables bash : lose variables (see scripts) export var: to keep them unset i: note, no $! Gotcha: Trying to used undefined variable No error! Empty String Variables aren’t aliases 10/2/2006 CSE 303 Lecture 3

Quoting and Escaping Lots of special characters ` ' " ! % & * ~ ? [ ] { } \ < > | $ Getting these chacters into input " : Does some substitutions ("12 * ${i}") ' : Does no substitutions ('$100') \ : Don’t substitute next char ("$N has \$5") 10/2/2006 CSE 303 Lecture 3

Moving Toward Scripts Shell has state We can write programs! Working directory User Collection of aliases History Variables We can write programs! 10/2/2006 CSE 303 Lecture 3

Sripting Part I Put all commands in file “script” source script All commands executed! Problem State of shell is changed 10/2/2006 CSE 303 Lecture 3

A better approach to scripting Method Start a new shell Share stdin, stdout, stderr exit Advantages Shell script is like any other program Shell commands are programming language But it’s a bad language for anything else! 10/2/2006 CSE 303 Lecture 3

Scripting Part II Process Why this works First line of script = #!/bin/bash Get “execute” permission: chmod u+x script Run it: script (or ./script if . not in $PATH) Why this works The shell consults the first line If shell program is there, launch and run script Else, it’s a “real” executable, so run it 10/2/2006 CSE 303 Lecture 3

Command Line Arguments Special Variables $0 program name $1 first argument … What if you want to handle many args? shift: shifts argument numbers down 10/2/2006 CSE 303 Lecture 3

Summary Command-line editing Input/Output Variables Rdirection Pipes Logic Expressions Variables Quoting and escaping Shell scripting introduction 10/2/2006 CSE 303 Lecture 3

Reading Linux Pocket Guide Bash Reference Manual p166-170, 176-179 (Programming with Shell Scripts) Bash Reference Manual 4.1 (shift command) 10/2/2006 CSE 303 Lecture 3

Next Time Advanced Scripting Control structures Arrays 10/2/2006 CSE 303 Lecture 3