CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 4 – Shell Variables, More Shell Scripts.

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Operating Systems Review. User Computer, including HW and SW.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
1 CSE 390a Lecture 5 Intro to shell scripting slides created by Marty Stepp, modified by Jessica Miller
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
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 Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Python.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
– 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
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.
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.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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 11: Shell.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Day 18 Bash and the.files. 2 The.files ls shows you the files in your directory –Or at least most of them. –Some files are hidden. Try: ls –a –This.
Files Tutor: You will need ….
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 5 – Regular Expressions, grep, Other Utilities.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
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.
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.
CSE 374 Programming Concepts & Tools
CIRC Winter Boot Camp 2017 Baowei Liu
CSE 374 Programming Concepts & Tools
Agenda Korn Shell Functions Korn Shell Variables
CSC 352– Unix Programming, Fall 2012
CSE 390a Lecture 5 Intro to shell scripting
CSE 374 Programming Concepts & Tools
The Linux Operating System
CSE 303 Concepts and Tools for Software Development
Writing Shell Scripts ─ part 3
CSE 390a Lecture 2 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
Lab 4: Introduction to Scripting
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 4 – Shell Variables, More Shell Scripts

Where we are We understand most of the bash shell and its “programming language”. Final pieces we’ll consider: –Shell variables Defining your own Built-in meanings Exporting –Arithmetic –For loops End with: –A long list of gotchas (some bash-specific; some common to shells) –Why long shell scripts are a bad idea, etc. 2

Shell variables We already know a shell has state: current working directory, streams, users, aliases, history. Its state also includes shell variables that hold strings. –Always strings even if they are “123” – but you can do math Features: –Change variables’ values: foo=blah –Add new variables: foo=blah or foo= –Use variable: ${foo} (braces sometimes optional) –Remove variables: unset foo –See what variables “are set”: set Omitted feature: Functions and local variables (see manual) Roughly “all variables are global (visible everywhere)” Only assignment is similar to mainstream “real” programming languages 3

Why variables? Variables are useful in scripts, just like in “normal” programming. “Special” variables affect shell operation. 3 most (?) common: –PATH –PS1 –HOME Some variables make sense only when the shell is reading from a script: –$#, $n (where n is an integer), $*, $? 4

Export If a shell runs another program (perhaps a bash script), does the other program “see the current variables that are set”? –i.e., are the shell variables part of the initial environment of the new program? It depends. –export foo – yes it will see value of foo –export -n foo – no it will not see value of foo –Default is no If the other program sets an exported variable, does the outer shell see the change? No. –Somewhat like “call by value” parameters in conventional languages –Remember, each new program (and shell) is launched as a separate process with its own state, environment, etc. 5

Arithmetic Variables are strings, so k=$i+$j is not addition But ((k=$i+$j)) is (and in fact the $ is optional here) So is let k="$i + $j” The shell converts the strings to numbers, silently using 0 as necessary 6

For loops Syntax: for v in w 1 w 2... w n do body done Execute body n times, with v set to w i on i th iteration(Afterwards, v=w n ) Why so convenient? –Use a filename pattern after in –Use list of argument strings after in: Not “$*” – that doesn’t handle arguments with embedded blanks the way you (usually) want 7

Quoting Does x=* set x to string-holding-asterisk or string-holding- all-filenames? If $x is *, does ls $x list all-files or file named asterisk? Are variables expanded in double-quotes? single-quotes? Could consult the manual, but honestly it’s easier to start a shell and experiment. For example: x="*" echo x echo $x echo "$x" (Double quotes suppress some substitutions) echo ’$x’ (Single quotes suppress all substitutions)... 8

Gotchas: A very partial list 1.Typo in variable name on left: create new variable oops=7 2.Typo in variable use: get empty string – ls $oops 3.Use same variable name again: clobber other use HISTFILE=uhoh 4.Spaces in variables: use double-quotes if you mean “one word” 5.Non-number used as number: end up with 0 6.set f=blah: apparently does nothing (assignment in csh) 7.Many, many more… 9

Shell programming revisited How do Java programming and shell programming compare? The shell: –“shorter” –convenient file-access, file-tests, program-execution, pipes –crazy quoting rules and syntax –also interactive Java: –none of the previous gotchas –local variables, modularity, typechecking, array- checking,... –real data structures, libraries, regular syntax Rough rule of thumb: Don’t write shell scripts over 200 lines? 10

Treatment of strings Suppose foo is a variable that holds the string hello Moral: In Java, variable-uses are easier than string- constants Opposite in Bash Both biased toward common use JavaBash Use variable (get “hello”)foo$foo The string foo“foo”foo Assign variablefoo = hi Concatenationfoo + “oo”${foo}oo Convert to numberlibrary callsilent and implicit 11

More on shell programming Metapoint: Computer scientists automate and end up accidentally inventing (bad) programming languages. It’s like using a screwdriver as a pry bar. HW3 in part, will be near the limits of what seems reasonable to do with a shell script (and we’ll end up cutting corners as a result) There are plenty of attempts to get “the best of both worlds” in a scripting language: Perl, Python, Ruby,... Personal opinion: it raises the limit to 1000 or lines? Gets you hooked on short programs. Picking the bash shell was a conscious decision to emphasize the interactive side and see “how bad programming can get”. Next: Regular expressions, grep, sed, others. 12

Bottom line Never do something manually if writing a script would save you time Never write a script if you need a large, robust piece of software Some programming languages try to give the “best of both worlds” – you now have seen two extremes that don’t (Java and bash) 13