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?

Similar documents
Last Time. on the website

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

Assignment clarifications

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

Shells and Shell Programming

Shells and Shell Programming

Vi & Shell Scripting

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Review of Fundamentals

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

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

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Processes and Shells

Chapter 9. Shell and Kernel

I/O and Shell Scripting

Shell Programming (bash)

Shell Start-up and Configuration Files

Bash Programming. Student Workbook

A shell can be used in one of two ways:

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

UNIX shell scripting

More Scripting Techniques Scripting Process Example Script

Shells & Shell Programming (Part B)

EECS2301. Lab 1 Winter 2016

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

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

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

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

CS Unix Tools & Scripting

Introduction to Linux

9.2 Linux Essentials Exam Objectives

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

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

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction to Linux

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

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

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

PROGRAMMAZIONE I A.A. 2015/2016

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

EE516: Embedded Software Project 1. Setting Up Environment for Projects

Linux Essentials. Smith, Roderick W. Table of Contents ISBN-13: Introduction xvii. Chapter 1 Selecting an Operating System 1

CMPS 12A Introduction to Programming Lab Assignment 7

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

CST8207: GNU/Linux Operating Systems I Lab Ten Boot Process and GRUB. Boot Process and GRUB

UNIX Shell Programming

Lab 3a Using the vi editor

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

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

Introduction to the Shell

Chapter-3. Introduction to Unix: Fundamental Commands

Bash, In A NutShell RUSTY MYERS - SYSTEMS ADMINISTRATOR, PENN STATE CHRIS LAWSON - IT CONSULTANT/SUPPORT SPECIALIST, PENN STATE ALTOONA

Unix Introduction to UNIX

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

5/20/2007. Touring Essential Programs

Bourne Shell Reference

Linux shell programming for Raspberry Pi Users - 2

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

CSE 15L Winter Midterm :) Review

Grep and Shell Programming

CS370 Operating Systems

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

Operating Systems, Unix Files and Commands SEEM

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

Creating a Shell or Command Interperter Program CSCI411 Lab

Introduction: What is Unix?

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

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

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

Unix/Linux: History and Philosophy

First steps on Linux and programming

Pace University. Fundamental Concepts of CS121 1

Linux & Shell Programming 2014

Introduction to UNIX Part II

C Shell Tutorial. Section 1

Implementation of a simple shell, xssh

CHAPTER 2 THE UNIX SHELLS

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

COMP 4/6262: Programming UNIX

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

CISC 220 fall 2011, set 1: Linux basics

CST Algonquin College 2

This is Lab Worksheet 3 - not an Assignment

CSE 391 Lecture 5. Intro to shell scripting

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

Shell Programming Systems Skills in C and Unix

Introduction to Shell Scripting

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Bash command shell language interpreter

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

B a s h s c r i p t i n g

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

Essential Linux Shell Commands

Introduction to Linux Part 2b: basic scripting. Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

Transcription:

Today Unix as an OS case study Intro to Shell Scripting Make sure the computer is in Linux If not, restart, holding down ALT key Login! Posted slides contain material not explicitly covered in class 1 Review What is an Operating System? What are its goals? How do we evaluate it? 2 1

Review: What is an Operating System? A program that acts as an intermediary between a user of a computer and the computer hardware Ø Resource allocator Applications Operating System Ø Control program Tasks: Ø Execute user programs and make solving user problems easier Ø Make the computer system convenient to use Hardware Ø Use the computer hardware in an efficient manner 3 What is an Operating System? Formally: A program that acts as an intermediary between the computer user and the computer hardware Goals: Ø Make the computer system easy to use. Ø Use the computer hardware efficiently. It is an extended machine Ø Hides the messy details which must be performed Ø Presents user with a virtual machine, easier to use It is a resource manager Ø Each program gets time with the resource Ø Each program gets space on the resource 4 2

Review: OS Goals Make computers easier to use Ø Abstraction! Ø Bridge gap between hardware and user experience Use computer hardware efficiently Why are these two separate goals? What is a computer? 5 Review: Evaluating an Operating System Reliability Ø Does exactly what it is designed to do Security Ø Withstands malicious attacks, privacy, Portability Ø Runs on multiple HW specifications Performance Ø Efficiency, fairness, response time, throughput, consistency 6 3

SYSTEMS PROGRAMMING 7 One Course Goal: Develop a Simple OS How are we going to do that? Ø Systems programming! 8 4

What is Systems Programming? Program development with system tools Ø (no fancy pants IDEs here) Uses system calls that hook in to core OS functions Use coding standards to ensure portability Ø Common file locations Ø Common compilation & installation procedures Ø Basic shell functionality We ll be programming in the Unix environment, using C 9 The System Programmer s Toolbox Shell: a program used to run other programs Text editor: where you ll develop your code Ø Your faves? Compiler: transforms source code into an executable file Ø gcc Debugger: a program that allows you to step through an execution & observe how the program state (i.e., variable values) changes Ø gdb Ø Print statements 10 5

The System Programmer s Toolbox Shell: a program used to run other programs Text editor: where you ll develop your code Ø Your faves? Compiler: transforms source code into an executable file Ø gcc Debugger: a program that allows you to step More on Wednesday through an execution & observe how the program state (i.e., variable values) changes Ø gdb Ø Print statements 11 Why Unix? Open source = easier to study Ø Windows is proprietary & closed Ø OSX is proprietary and is built on top of Unix Historic: developed in the 60s & 70s Ø One of the oldest OS s in use today Most serious programmers and hackers know their way around Unix/Linux Linux is a Unix-like OS 12 6

Why C? The high-level language (HLL) that s closest to the hardware If you understand C, you [pretty much] understand how machines store and process data 14 UNIX 15 7

Unix Philosophy Make each program do one thing well Ø More complex functionality by combining programs Ø Make every program a filter Ø More efficient Ø Better for reuse Portability No GUIs Only error feedback 16 What is a Shell? User interface to the operating system Command-line interpreter Functionality: Ø Execute other programs Ø Manage files Ø Manage processes A program, like any other Basic form of shell: Ø while <read command>: parse command execute command hides details of underlying operating system 17 8

The Shell and Terminal When you open the terminal, you can interact with the shell 18 Directory Shortcuts. Ø Current directory.. Ø Parent directory of current directory Ø Every directory except the root directory has a parent directory ~ Ø User s home directory Useful in a variety of Unix commands 19 9

Unix Commands Worksheets Work together on these worksheets Check-in at 2:05 p.m. 20 Handout Discussion What additional Unix commands did you find? What are the tradeoffs to the Unix command design (many small, simple programs; can be combined)? 21 10

Unix Design Small, simple programs Ø Easier to maintain Ø Single-responsibility principle Combine (a few or lots) with pipes Ø Easy to combine with a simple interface Not-so-user-friendly to get started 22 USEFUL SHORTCUTS 48 11

Useful Shortcuts Up arrow!command-prefix Ø! = bang Ø Repeat most recent command that begins with prefix Tab completion Ø Use tab to complete filepaths and commands 49 SHELL SCRIPTING 51 12

Review: What is a Shell? User interface to the operating system Command-line interpreter Functionality: Ø Execute other programs Ø Manage files Ø Manage processes A program like any other Basic form of shell: Ø while <read command>: parse command execute command hides details of underlying operating system 52 What is a shell script? A shell script is a list of commands to be run by a shell Ø basically a program Ø uses shell commands instead of C or Java statements Why? Ø automate repetitious tasks Ex: executing a program on a large set of test inputs Ø package up commonly executed command sequences Ø create our own commands 53 13

Simple Shell Script Example #!/bin/sh echo "Hello World" Which shell to use Command to execute echo like a print statement #! is known as the shebang Look at the available shells by executing ls -l /bin/*sh What do you notice about /bin/sh? 54 Shell Scripts A shell script is a regular text file that contains shell or UNIX commands Kernel uses the first line of script to determine which shell script to use Ø #!pathname-of-shell Kernel invokes pathname and sends the script as an argument to be interpreted Ø If #! is not specified, the current shell assumes it is a script in its own language Can lead to problems 55 14

Invoking a Script A script can be invoked as: Ø sh scr_name [ arg ] Where sh is whatever shell you want Ø sh < scr_name [ args ] Ø path/to/scr_name [ arg ] Before running, script must have execute permission: Ø chmod +x scr_name We ll typically use the 1 st or 3 rd execution option and we ll use the bash shell 56 Example Programs In /csdept/courses/cs330/handouts/ bash_examples/ In a new terminal/tab, go into this directory Look at the permissions on the files 57 15

Writing Your First Bash Script Bash: Bourne-again shell Ø Unix shell and command language Open your favorite text editor Write a simple bash script: Ø Type in the shebang Ø And the command: #!/bin/sh echo "Hello World" Ø and save as hello.sh Type bash hello.sh to run 58 Comments Comments begin with a # Comments end at the end of the line Comments can begin whenever a token begins Many text editors will help you with syntax highlighting Examples: # This is a comment # and so is this grep foo bar # this is a comment grep foo bar# this is not a comment Style requirement: A comment on 2 nd line in your script that lists you as author 59 16

Your Second Script Write a script that Ø Displays the files in the current directory Ø Lists all logged-in users Your script should contain authorship info near the top Build in pieces (Yes, even this short script) Execute and test your script Ø Verify the output 60 Variables Don't have to be declared in advance Untyped: the same variable can hold an integer value or a string Syntax for using variables (bash): Ø Defining the value of a variable name: name=value Ø Using the variable name: $name or ${name} Variables can be local or environment Ø Environment variables are part of UNIX and can be accessed by child processes Notice no spaces around = 61 17

Variable Example #!/bin/sh MESSAGE="Hello World" echo $MESSAGE echo '$MESSAGE' echo "$MESSAGE" Prints variable Prints literally Prints variable variable.sh 62 Environmental Variables Name $HOME $PATH $MAIL $USER $SHELL $TERM $PS1 Meaning Absolute pathname of your home directory A list of directories to search for Absolute pathname to mailbox Your user name Absolute pathname of login shell Type of terminal Prompt 63 18

Using Environment Variables #!/bin/bash echo I am $USER echo "I live at $HOME" Both echo statements work with or without quotes env_var.sh 64 Modify your second script Write a script that Ø Displays the files in YOUR HOME directory Ø Lists all logged-in users Your script should contain authorship info near the top Build in pieces (Yes, even this short script) Execute and test your script Ø Verify the output 65 19

Parameters A parameter is one of the following: Ø A positional parameter, starting from 0 Ø A special parameter To get the value of a parameter: ${param} Ø Can be part of a word (abc${foo}def) Ø Works within double quotes The {} can be omitted for simple variables, special parameters, and single digit positional parameters 66 Positional Parameters The arguments to a shell script Ø $0, $1, $2, $3 Ø Parameter 0 is the name of the shell or the shell script The arguments to a shell function Arguments to the set built-in command Ø set this is a test $1=this, $2=is, $3=a, $4=test Manipulated with shift Ø shift 2 $1=a, $2=test 67 20

Example with Parameters Script #!/bin/sh # Parameter 1: string # Parameter 2: file grep $1 $2 wc l Invocation: $./countlines ing /usr/share/dict/words 30415 countlines 68 Special Parameters Parameter Meaning $# Number of positional parameters $- Options currently in effect $? Exit value of last executed command $$ Process number of current process $! Process number of background process $* All arguments on command line from 1 on "$@" All arguments on command line Individually quoted "$1" "$2" ; useful if parameters contain spaces countlines_params 69 21

Special Characters The shell processes the following characters specially unless quoted: Ø & ( ) < > ; " ' $ ` space tab newline The following are special whenever patterns are processed: Ø *? [ ] The following are special at the beginning of a word: Ø # ~ The following is special when processing assignments: Ø = 70 Command Substitution: `` Used to turn the output of a command into a string Used to create arguments or variables $ date Mon Sep 10 11:46:37 EDT 2018 $ NOW=`date` $ echo $NOW Mon Sep 10 11:46:37 EDT 2018 $ PATH=`myscript`:$PATH 71 22

Compound Commands Multiple commands Ø Separated by semicolon or newline Command groupings Ø pipelines Subshell Ø ( command1; command2 ) > file Boolean operators Control structures 72 Program Development Process Divide & conquer: break the big programming problem into smaller subproblems Ø Recursively repeat as necessary Solve each subproblem & test for correctness In general, test your code after every change to catch bugs quickly & fix them easily Develop incrementally As the programs get bigger, periodically save working versions (script or version control) 74 23

TODO Assign1 due before class Friday Ø Leverage the examples Next time: Reviewing C 78 24