EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Similar documents
Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Linux for Beginners. Windows users should download putty or bitvise:

Useful Unix Commands Cheat Sheet

Perl and R Scripting for Biologists

Basic Linux (Bash) Commands

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

NASA Lecture 2 (with lab) 2018/03/05 Michael Tsai Kai-Ling Lo

Introduction: What is Unix?

Welcome to Linux. Lecture 1.1

A shell can be used in one of two ways:

Shells and Shell Programming

ScazLab. Linux Scripting. Core Skills That Every Roboticist Must Have. Alex Litoiu Thursday, November 14, 13

COMP 4/6262: Programming UNIX

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction to Linux

UNIX System Programming Lecture 3: BASH Programming

9.2 Linux Essentials Exam Objectives

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

UNIX. The Very 10 Short Howto for beginners. Soon-Hyung Yook. March 27, Soon-Hyung Yook UNIX March 27, / 29

Bourne Shell Reference

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

Shells and Shell Programming

Linux at the Command Line Don Johnson of BU IS&T

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

CS246 Spring14 Programming Paradigm Notes on Linux

Vi & Shell Scripting

Introduction to the shell Part II

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

Linux shell programming for Raspberry Pi Users - 2

Introduction To Linux. Rob Thomas - ACRC

UNIX Kernel. UNIX History

Unix/Linux: History and Philosophy

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

EECS2301. Lab 1 Winter 2016

Introduction. Let s start with the first set of slides

Lab 2: Linux/Unix shell

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Shells & Shell Programming (Part B)

Introduction of Linux. Huang Cheng-Chao Dept. of Comput. Sci. & Tech. East China Normal University

CSE 390a Lecture 1. introduction to Linux/Unix environment

Unix Handouts. Shantanu N Kulkarni

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

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Introduction to Linux

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Name: Peter Lemieszewski Venue: Education

Practical Computing-II. Programming in the Linux Environment. 0. An Introduction. B.W.Gore. March 20, 2015

Bash command shell language interpreter

Assignment clarifications

Overview LEARN. History of Linux Linux Architecture Linux File System Linux Access Linux Commands File Permission Editors Conclusion and Questions

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Introduction to Linux

CS Unix Tools & Scripting

CSE 391 Lecture 1. introduction to Linux/Unix environment

Linux Shell Script. J. K. Mandal

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Unix System Architecture, File System, and Shell Commands

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Lecture 01: welcome and intro what LSD and Unix have in common

Introduction to Linux

Introduction To. Barry Grant

Brief Linux Presentation. July 10th, 2006 Elan Borenstein

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Introduction to Linux

Introduction of Linux

Computer Systems and Architecture

CST Algonquin College 2

UNIX Concepts COMPSCI 386

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

CISC 220 fall 2011, set 1: Linux basics

CSCI2467: Systems Programming Concepts

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

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

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Essentials for Scientific Computing: Bash Shell Scripting Day 3

UNIX Basics. UNIX Basics CIS 218 Oakton Community College

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

CSCI 2132 Software Development. Lecture 2: Introduction to UNIX and Unix-like Operating Systems

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Bash Shell Programming Helps

Processes and Shells

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

CSC 2500: Unix Lab Fall 2016

CSC209. Software Tools and Systems Programming.

Essential Unix and Linux! Perl for Bioinformatics, ! F. Pineda

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Review of Fundamentals

Lec 1 add-on: Linux Intro

BGGN 213 Working with UNIX Barry Grant

Introduction To. Barry Grant

CSE 391 Lecture 1. introduction to Linux/Unix environment

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

Chapter 4. Unix Tutorial. Unix Shell

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

(a) About Unix. History

Transcription:

EECS 470 Lab 5 Linux Shell Scripting Department of Electrical Engineering and Computer Science College of Engineering University of Michigan Friday, 1 st February, 2018 (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 1 / 40

Overview Administrivia UNIX Files Utilities Connecting Utilities Bourne Again Shell Variables Flow Control Functions Globbing Scripting Assignment (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 2 / 40

Administrivia Administrivia Homework Homework 3 is due Tuesday, 6 th February at the beginning of lecture Projects Project 3 is due Sunday, 4 th February at 9:00PM We are available to answer questions on anything here. Office hours can be found in the course google calendar. (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 3 / 40

UNIX What is UNIX? Mainframe operating system Written by Dennis Ritchie, Ken Thompson and Rob Pike (among others) at Bell Labs The basis for many modern operating systems, e.g. Linux, BSD, Mac OSX History of UNIX Written at Bell Labs in 1969 First version of BSD is installed in 1974 Last Bell Labs UNIX (Version 7) is published in 1979 The GNU Project is started by Richard Stallman Linux Torvalds writes a monolithic kernel operating system in 1991 (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 4 / 40

UNIX Philosophy This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface. Douglas McIlroy Everything is a file descriptor or a process. Linus Torvalds (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 5 / 40

Files Files What is a file? Anything referenced through a filesystem Anything with a file descriptor (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 6 / 40

Files Types of Files Regular Anything not in one of the following categories Directory Can contain other files and directories (read up on inodes sometime) Symbolic Link A pointer to another file Pipe Covered in a few slides Socket Covered in EECS 482/489 (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 7 / 40

Files Permissions r w x - - - - - - User Group Other Table: UNIX permissions are represented with one bit for each permission in each category, e.g. 700. Three Permissions Read Write Execute Three Categories User Group Other (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 8 / 40

Utilities Utilities What is a utility? A program used to process text streams/files Called from some command line/shell Why do I care? Utilities form the basis of Linux skills Useful for automation Necessary for today s lab (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 9 / 40

Utilities Navigation Utilities pwd Description: print working directory where you are Synopsis: pwd [OPTIONS] ls Description: list directory contents Synopsis: ls [OPTIONS]... [FILE]... cd Description: change directory Synopsis: cd [OPTIONS] [PATH] (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 10 / 40

Utilities File Utilities cp Description: copy files Synopsis: cp [OPTONS]... SOURCE DEST mv Description: move files Synopsis: mv [OPTONS]... SOURCE DEST rm Description: remove files Synopsis: rm [OPTONS]... FILE... (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 11 / 40

Utilities diff Description Shows the line-by-line differences between files Good for checking if your output is correct Synopsys diff [OPTIONS] FILES Examples diff -uy../project3_correct/writeback.out writeback.out vimdiff../project3_correct/writeback.out writeback.out (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 12 / 40

Utilities Graphical Diff Alternatives to diff Parsing output of diff is hard, so it might be useful to use some kind of graphical diff tool, like vimdiff, which opens up two files side-by-side in Vim showing their differences. Try it and you ll see how much easier to parse this. (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 13 / 40

Utilities grep Description Print lines matching a pattern Synopsys grep [OPTIONS] PATTERN [FILE] Examples grep @@@ program.out ps -axfuw grep "$USER" grep "vcs" (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 14 / 40

Utilities Regular Expressions Really powerful/useful Complicated, and beyond the scope of this presentation Read up on them at Wikipedia in the grep manual (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 15 / 40

Utilities man Description An interface to the on-line reference manuals (pager) Synopsys man PAGE Examples man grep man diff (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 16 / 40

Utilities Pager Definition A program which allows browsing of large text files by breaking them into screen-sized chunks. Examples less man (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 17 / 40

Utilities Other Utilities These will be useful... cut touch tee xargs tail column find less These are harder, but even more useful... sed awk patch vi(m) fmt tmux (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 18 / 40

Connecting Utilities Program Features Methods of Communication Standard Text Streams stdin stdout stderr Return value/code (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 19 / 40

Connecting Utilities Return Codes What is a return code? The integer value a program returns (e.g. return(0);) Conventionally, returning zero indicates success, non-zero failure Specific values other than zero mean different things for different programs (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 20 / 40

Connecting Utilities Standard Text Streams stdin The default input to a program From a keyboard by default stdout The default output of a program To a display by default stderr The default error output of a program To a display by default, requires additional effort to save (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 21 / 40

Connecting Utilities Connecting Utilities Why do we want to connect utilities? Combination jobs without intermediate files e.g. take the diff of two different grep operations (what you need to do for today s lab) How can we connect utilities? Pipes Redirection (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 22 / 40

Connecting Utilities Pipe What is a pipe? Connects the stdout of one program to the stdin of another Does not connect stderr How do I use one? Call a program on one side of the and then call another on the other side e.g. dmesg less (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 23 / 40

Connecting Utilities Pipe: xargs Problem What if we want to pipe to utility that uses arguments instead of input? Solution: xargs xargs splits input into individual items and calls the program that is its argument once for each input. (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 24 / 40

Connecting Utilities Redirection What is redirection? Allows for modification of the standard text streams stdin is 0 stdout is 1 stderr is 2 Several types: 0< Use a file instead of the keyboard for stdin i> Use a file instead of the terminal for the stream i i>> Like i>, but append to a file instead of overwriting i>&j Put stream i into the same place as stream j Advanced Bash Scripting Guide: I/O Redirection (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 25 / 40

Connecting Utilities Redirection by Example Example./vs-asm < test_progs/evens.s > program.mem Example make tee 2>&1 build.out Example./test 1>&2 2>&3 (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 26 / 40

Bourne Again Shell Shell What is a shell? Before we had graphical environments, we had text shells Basically, an interpreter for commands, executing programs and saving information Possibly a Read-Execute-Print-Loop (REPL) (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 27 / 40

Bourne Again Shell Bourne Again Shell What is BASH? Stands for the Bourne Again Shell Created in 1989 by Brian Fox Default shell in most Linux distributions and Mac OSX Why BASH? What I learned first Default in CAEN Redhat, finally (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 28 / 40

Bourne Again Shell Warning Warning Everything after this slide will be specific to BASH. Other shells behave similarly, but not identically. If you want to use something else (e.g. ZSH, TCSH, etc.), please find other resources. (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 29 / 40

Bourne Again Shell Variables Variables BASH Variables Store data Contain text, for the most part No type system Syntax Assignment/Declaration variable=value Referencing $variable ${variable} (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 30 / 40

Bourne Again Shell Variables Variable Scope Scope Variables exist inside the shell they re in Unless exported e.g. export EDITOR=vim This is very important in scripts, particularly the shell startup scripts (.bashrc,.bash_profile) (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 31 / 40

Bourne Again Shell Variables Special Variables $# The number of command line arguments $0 The name of the script/function called $1 The first argument to the script/function $? The return code of the last program run in this shell $USER The current user $HOME The user s home directory (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 32 / 40

Bourne Again Shell Flow Control Flow Control if/else if [ var -eq "string" ] then command elif [ var -eq "string2" ] then command2 else command3 fi (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 33 / 40

Bourne Again Shell Flow Control Flow Control case case "$var" in val) esac ;; val2) ;; ( * ) ;; command command default (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 34 / 40

Bourne Again Shell Flow Control Flow Control for for file in./*; do command $file command2 done for (( a=1; a <= LIMIT; a++ )) do command command2 done (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 35 / 40

Bourne Again Shell Flow Control Conditionals Testing Testing happens in [ ] String tests are different than arithmetic tests Generally use -lt, -gt, -le, -ge, -eq, -ne Tests for files are special, e.g. [ -x simv ] makes sure that the binary simv has execution permissions (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 36 / 40

Bourne Again Shell Functions Functions Functions Packages of commands Arguments are referenced by position Useful for packaging up commonly reused bits Syntax function () { commands } (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 37 / 40

Bourne Again Shell Globbing File Globbing What is globbing? File name wildcarding in the shell Expansion is done, and then passed to the command to be executed e.g. test_progs/*.s What should I know globbing? Superior to parsing ls output in everyway Can get more complicated, see this page of the BASH manual. (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 38 / 40

Scripting Scripting 1. Write a series of shell commands into a text file 2. On the first line of the file, specify an interpreter e.g. #!/bin/bash 3. Name it something appropriate e.g. test.sh 4. Add execute permissions e.g. chmod +x test.sh 5. Run the script e.g. $./test.sh (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 39 / 40

Assignment Lab Assignment Assignment is posted to the course website as Lab 5 Assignment. If you get stuck... Ask a neighbor, quietly Put yourself in the help queue When you finish the assignment, sign up in the help queue and mark that you would like to be checked off. If you are unable to finish today, the assignment needs to be checked off by a GSI in office hours by next Thurday. (University of Michigan) Lab 5: Scripting Friday, 1 st February, 2018 40 / 40