CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
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.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
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.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
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.
CS 403: Programming Languages Lecture 21 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
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
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Chapter Four I/O Redirection1 System Programming Shell Operators.
40 Years and Still Rocking the Terminal!
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Rexx for Shell Scripting or “We don't need no stinkin' bashes...”
CIRC Summer School 2016 Baowei Liu
Lesson 5-Exploring Utilities
Shell Control Structures
CIRC Summer School 2017 Baowei Liu
Part 1: Basic Commands/Utilities
CSE 374 Programming Concepts & Tools
Shell Scripting March 1st, 2004 Class Meeting 7.
Chapter 6 Filters.
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
Shell Script Assignment 1.
CS 403: Programming Languages
CSE 303 Concepts and Tools for Software Development
UNIX Reference Sheets CSE 2031 Fall 2010.
More advanced BASH usage
Linux Shell Script Programming
Shell Control Structures
CSC 4630 Meeting 4 January 29, 2007.
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 202 Overview Announcements Story Hour, Houser 108, 3PM Friday MP2 Shell Programming

Lecture 203 Concepts of shell programming Prerequisites—Operating system provides or can be made to provide the following: Files are streams of bytes i.e. not records Multiple processes Processes have associated with them a standard input, standard output, and standard error

Lecture 204 Standard I/O Streams standard input or files command, options standard error standard output C++CJava inputcinstdinSystem.in outputcoutstdoutSystem.out errorcerrstderrSystem.err

Lecture 205 Concepts of shell programming Commands are executed by their name Not with special command “run command” Shell is not a special program directly supported by the operating system Lots of small programs that do one thing only Regular expressions are used by the shell and many of the small programs I/O can be redirected Shell is an interpreted programming langauge

Lecture 206 Commands General syntax: command flags arguments Searched for using PATH variable Colon separated list of directories PATH=/bin:/usr/bin:~/bin Searched in order

Lecture 207 Lots of small, single purpose programs wc Counting words, characters lines pr Formatting text to fit on a page cat Direct contents of file(s) to standard output ls List contents of directories grep Search files or standard input for pattern sort Sort contents of file or standard input tail print last lines of file or standard input diff print differences between two files sed print, substitute files or standard input

Lecture 208 Regular Expressions are used by shell and many small programs Shell’s regular expressions are simplified * match any string of zero or more characters in file names ? match any single character in file names \c take character c literally ‘…’take … literally [ccc] match any single character from ccc in file names, ranges like 0-9 or a-z are legal Commands like grep are more sophisticated (re | re ) matches first or second r.e.

Lecture 209 I/O Redirection Files > standard output to file, overwriting >> standard output to file, appending < standard input to file <<str standard input from shell until str 2> standard error to file Pipes | direct standard output of command on left to standard input of command on right

Lecture 2010 Shell is an interpreted programming language Control Flow (useful interactively) p1 & runs p1 without waiting for completion Also called running in “background” p1 ; p2 sequencing, run p1, then p2 p1 && p2 conditional execution run p1, if successful, run p2 p1 || p2 conditional execution, run p1 if unsuccessful, run p2 `…` run commands in … ; output replaces `…` (…) run commands in sub-shell Useful for running commands in different directory E.g. (cd ~/foo; grep i *.c)

Lecture 2011 Shell is an interpreted programming language (cont.) Variables var=value variable assignment $1, $2, $3 arguments to shell script $var value of shell variable var ${var} values of var, useful when concatenating Comments # if # starts word, rest of line is a comment

Lecture 2012 Shell is a programming language also Control flow if command then commands else commands fi for var in stuff do commands done case expr in pat) command … esac

Lecture 2013 Example - which command # which cmd: which cmd in PATH is executed, version 1 case $# in 0) echo ‘Usage: which command’ 1>&2; exit 2 esac for i in `echo $PATH | sed ‘s/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g’` do if test -f $i/$1 then echo $i/$1 # found it exit 0 fi done exit 1 # not found

Lecture 2014 Example which command What happens if there is a program called test in the current directory? (assuming that test isn’t a shell built-in)

Final version of which # which cmd: which cmd in PATH is executed, final version opath=$PATH PATH=/bin:/usr/bin case $# in 0) echo ‘Usage: which command’ 1>&2; exit 2 esac for i in `echo $opath | sed ‘s/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g’` do if test -f $i/$1 then echo $i/$1 # found it exit 0 fi done exit 1 # not found