CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

Similar documents
CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

CSE 374 Programming Concepts & Tools. Laura Campbell (thanks to Hal Perkins) Winter 2014 Lecture 6 sed, command-line tools wrapup

Shells & Shell Programming (Part B)

Chapter 4. Unix Tutorial. Unix Shell

More Scripting Techniques Scripting Process Example Script

9.2 Linux Essentials Exam Objectives

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Linux Command Line Interface. December 27, 2017

CS Unix Tools & Scripting

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Shell Programming (bash)

Shells and Shell Programming

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Bash command shell language interpreter

Shells and Shell Programming

Simple Shell Scripting for Scientists

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

The Online Unix Manual

CSE 391 Lecture 5. Intro to shell scripting

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Output with printf Input. from a file from a command arguments from the command read

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell Scripting. Jeremy Sanders. October 2011

ACS Unix (Winter Term, ) Page 21

CSE 490c Autumn 2004 Midterm 1

Lab 4: Bash Scripting

Vi & Shell Scripting

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

CSE 390a Lecture 5. Intro to shell scripting

Unleashing the Shell Hands-On UNIX System Administration DeCal Week 6 28 February 2011

A shell can be used in one of two ways:

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

COMS 6100 Class Notes 3

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Shell Start-up and Configuration Files

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

CSE 15L Winter Midterm :) Review

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Review of Fundamentals

Goals for This Lecture:

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

More Scripting Todd Kelley CST8207 Todd Kelley 1

Table of Contents EVALUATION COPY

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

CMPS 12A Introduction to Programming Lab Assignment 7

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Simple Shell Scripting for Scientists

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

LING 408/508: Computational Techniques for Linguists. Lecture 5

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

5/20/2007. Touring Essential Programs

Pod::Usage, pod2usage() - print a usage message from embedded pod documentation

C Shell Tutorial. Section 1

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

do shell script in AppleScript

Lab 4: Shell Scripting

Basic Linux (Bash) Commands

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Invent Your Own Computer Games with Python

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

5/8/2012. Specifying Instructions to the Shell Chapter 8

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

My Favorite bash Tips and Tricks

Introduction to UNIX Shell Exercises

Bash Check If Command Line Parameter Exists

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

Math 25 and Maple 3 + 4;

COMP 4/6262: Programming UNIX

Shell Programming Overview

5/8/2012. Exploring Utilities Chapter 5

Creating a Shell or Command Interperter Program CSCI411 Lab

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

Do not start the test until instructed to do so!

Last Time. on the website

Bash Programming. Student Workbook

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Lecture 3 Tonight we dine in shell. Hands-On Unix System Administration DeCal

Python for Non-programmers

CST Algonquin College 2

Useful Unix Commands Cheat Sheet

DAVE LIDDAMENT INTRODUCTION TO BASH

UNIX System Programming Lecture 3: BASH Programming

CS Unix Tools & Scripting

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Linux shell scripting Getting started *

bash Data Administrative Shell Scripting COMP2101 Fall 2017

CSE 303 Lecture 4. users/groups; permissions; intro to shell scripting. read Linux Pocket Guide pp , 25-27, 61-65, , 176

Bourne Shell Reference

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Transcription:

CSE 374 Programming Concepts & Tools Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

test / if Recall from last lecture: test (not built-in) takes arguments that look like a predicate doesn t do anything other than return an exit code if then fi (built-ins) if <command>; then <commands> fi 2

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. 3

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 bash manual 3.3) Roughly all variables are global (visible everywhere) 4

Why variables? Variables are useful in scripts, just like in normal programming. Special variables affect shell operation. 3 of the most common: PATH (tells shell where to find programs) PS1 (determines the shell prompt) HOME (determines what ~ means) Some variables make sense only when the shell is reading from a script: $#, $n (where n is an integer), $@, $*, $? 5

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 processs with its own state, environment, etc. export -p OR printenv see all exported variables 6

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 when a variable is empty 7

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 for a range of integers look at man seq 8

Quoting Does x=* set x to string-holding-asterisk or string-holdingall-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)... 9

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. Using ls to list files to iterate over in for loop (just use string expansion *) 8. Many, many more 10

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: verbose syntax none of the previous gotchas local variables, modularity, typechecking, array-checking,... real data structures, libraries, more common syntax Rough rule of thumb: Don t write shell scripts over 200 lines? 11

Treatment of strings Suppose foo is a variable that holds the string hello Java Bash Use variable (get hello ) foo $foo The string foo foo foo Assign variable foo = hi foo=hi Concatenation foo + oo ${foo}oo Convert to number library call silent and implicit Moral: In Java, variable-uses are easier than stringconstants Opposite in Bash Both biased toward common use 12

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 10000 lines? Gets you hooked on short programs. Picking the bash shell was a conscious decision to emphasize the interactive side and because it is commonly used despite its terribleness Next: Regular expressions, grep, sed, others. 13

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 (ruby, python, perl) try to give the best of both worlds you now have seen two extremes that don t (Java and bash) 14