CSCI2467: Systems Programming Concepts

Similar documents
EECS2301. Lab 1 Winter 2016

Introduction to Shell Scripting

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

CSCI 2132 Software Development. Lecture 3: Unix Shells and Other Basic Concepts

5/20/2007. Touring Essential Programs

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals

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

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

CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux

Unix Shells and Other Basic Concepts

Introduction: What is Unix?

UNIX Kernel. UNIX History

Welcome to Linux. Lecture 1.1

Chapter 9. Shell and Kernel

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

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

Linux Command Line Interface. December 27, 2017

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

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

Perl and R Scripting for Biologists

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

CSC UNIX System, Spring 2015

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

Study Guide Processes & Job Control

Assignment clarifications

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

Unix Processes. What is a Process?

UNIX System Programming Lecture 3: BASH Programming

Shells and Shell Programming

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

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

Unix basics exercise MBV-INFX410

CST Algonquin College 2

Part I. UNIX Workshop Series: Quick-Start

Week 5 Lesson 5 02/28/18

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

CISC 220 fall 2011, set 1: Linux basics

An Introduction to Unix Power Tools

Vi & Shell Scripting

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

Shells and Shell Programming

Unix. Examples: OS X and Ubuntu

Bioinformatics? Reads, assembly, annotation, comparative genomics and a bit of phylogeny.

CS246 Spring14 Programming Paradigm Notes on Linux

find starting-directory -name filename -user username

Unix/Linux: History and Philosophy

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

CSC209. Software Tools and Systems Programming.

Introduction to UNIX Shell Exercises

System Programming. Session 6 Shell Scripting

Operating System Labs. Yuanbin Wu

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Introduction to the Shell

Processes and Shells

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

Basics. I think that the later is better.

Introduction in Unix. Linus Torvalds Ken Thompson & Dennis Ritchie

Linux Fundamentals (L-120)

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

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

COMP 4/6262: Programming UNIX

CSC374, Spring 2010 Lab Assignment 1: Writing Your Own Unix Shell

Part III. Shell Config. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Overview: Concurrent Architectures - Unix: Forks and Pipes

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

85321, Systems Administration Chapter 6: The shell

Introduction to Cygwin Operating Environment

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

Introduction to Linux (Part I) BUPT/QMUL 2018/03/14

Unzip command in unix

ITST Searching, Extracting & Archiving Data

Introduction to Linux

Introduction. Let s start with the first set of slides

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

Unix System Architecture, File System, and Shell Commands

CSC209. Software Tools and Systems Programming.

Implementation of a simple shell, xssh

CS 213, Fall 2001 Lab Assignment L5: Writing Your Own Unix Shell Assigned: Oct. 25, Due: Fri., Nov. 2, 11:59PM

Introduction to UNIX. SURF Research Boot Camp April Jeroen Engelberts Consultant Supercomputing

ACS Unix (Winter Term, ) Page 21

UNIX Concepts COMPSCI 386

Implementation of a simple shell, xssh

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

System Programming. Introduction to Unix

Introduction to Linux. Fundamentals of Computer Science

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

Grep and Shell Programming

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

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

First steps on Linux and programming

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

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

CSE II-Sem)

Introduction to Unix/Linux INX_S17, Day 8,

Shell Programming (bash)

Transcription:

CSCI2467: Systems Programming Concepts Class activity: bash shell literacy Instructor: Matthew Toups Fall 2017

Today 0 Shells History Usage Scripts vs. Programs 1 Bash shell: practical uses for your systems skills Computing your score so far More common tools 2 Activity Make a shell script

Linux process hierarchy Linux Process Hierarchy Carnegie Mellon [0] init [1] Daemon e.g. httpd Login shell Login shell Child Child Child Grandchild Bryant and O Hallaron, Computer Systems: A Programmer s Perspec;ve, Third Edi;on Grandchild Note: you can view the hierarchy using the Linux pstree command 4

History Shell A shell is an application program that runs programs on behalf of the user. - sh Orig. Unix shell (Stephen Bourne, AT&T Bell Labs, 1977) - csh / tcsh BSD Unix C shell (1978 / 1981) 1978: UC Berkeley grad student Bill Joy (vi, Sun Microsystems, Java language) - bash GNU Bourne-Again Shell (1989) default Linux shell (1991), default macos shell (since 10.3, 2003) Windows 10 (Anniversary update, 2016) - tsh tiny shell (you, 2017)

Usage bash is used in many ways Command processor, or programming language?

Usage bash is used in many ways Command processor, or programming language? Both!

Usage Interactive vs. scripting Interactive case: human types commands at a prompt - You ve been using bash this way. - Your tsh works this way.

Usage Interactive vs. scripting Shell script: any commands you could type at the shell prompt could also go into a file - bash then interprets that file as a sequence of commands - thie file is typically called a script (denoted by.sh filename extension)

Usage Interactive vs. scripting bash as Command Processor Interactive case: human types commands at a prompt - You ve been using bash this way. - Your tsh works this way. bash as Programming language Shell script: any commands you could type at the shell prompt could also go into a file - bash then interprets that file as a sequence of commands - thie file is typically called a script (denoted by.sh filename extension)

Scripts vs. Programs Script? Program? What s the difference? scripts are text files which are interpreted - Scripting languages: bash, perl, python (many more) - An interpreter reads script text every time it runs and then performs commands

Scripts vs. Programs Script? Program? What s the difference? scripts are text files which are interpreted - Scripting languages: bash, perl, python (many more) - An interpreter reads script text every time it runs and then performs commands Traditional programming languages are compiled - A compiler converts human-readable source code to machine-level instructions (may be called binary or executable) - These machine-level instructions are the focus of Chapter 3

Scripts vs. Programs Shell scripting

Today 0 Shells History Usage Scripts vs. Programs 1 Bash shell: practical uses for your systems skills Computing your score so far More common tools 2 Activity Make a shell script

Computing your score so far Your point total Scores so far are updated in /home/csci2467/handin/<your-userid> (includes corrections on midterm exam) As usual, log on to terminal in Math 209 lab or via ssh to math209.cs.uno.edu

Computing your score so far Your point total Change to your handin directory: cd /home/csci2467/handin/<your-userid> Can view all with: cat SCORE.*... and add up with: cat SCORE.*../sum.sh

Computing your score so far What did we just do? No felines were harmed cat command concatenates files output contents of each file given in order if only one filename given (singleton), then simply output contents of that file

Computing your score so far What did we just do? wildcards, or globbing Bash looked in the current directory for files that match the pattern: SCORE.* * is a wildcard: matches anything (zero or more characters) Therefore SCORE.* matches any file which begins with SCORE. As in, all of your scores: SCORE.lab0 SCORE.lab1 SCORE.lab2 SCORE.midterm bash does this, so the cat program thinks you typed: cat SCORE.lab0 SCORE.lab1 SCORE.lab2 SCORE.midterm - wildcards won t work in tsh or tshref - so in tsh you would see this: /bin/cat: SCORE.*: No such file or directory

Computing your score so far What did we just do? more on wildcards Could also match a single character: cat SCORE.lab? - Would match SCORE.lab9 but not SCORE.lab10 Or, match only certain characters: cat SCORE.lab[01] - matches only SCORE.lab0 and SCORE.lab1 (not SCORE.lab2) or any others

Computing your score so far What did we just do? more on wildcards Try creating a SCORE.lab10 file with: echo 100 > SCORE.lab10 - remember output redirection? Then compare: cat SCORE.lab? cat SCORE.lab?? cat SCORE.lab* Delete SCORE.lab10 when finished: rm SCORE.lab10 Be very careful using * with rm!

Computing your score so far What did we just do? Pipes is called a pipe - cmd1 output normally goes to the screen... but instead becomes cmd2 input (another type of redirection) - Then cmd2 output goes to screen... but could be written to a file: cmd1 cmd2 > outputfile Many more examples on Bash redirections cheat sheet

Computing your score so far What did we just do? Shell script example #!/ bin / bash TOTAL =0 while read val ; do TOTAL =$(( $TOTAL + $val )) done echo $TOTAL sum.sh is a shell script (text file) commands interpreted by program /bin/bash would also work if typed in at the prompt: TOTAL=0; while read val; do TOTAL=$(($TOTAL+$val)); done; echo $TOTAL

Computing your score so far What did we just do? stdin sum.sh run by itself takes input from keyboard (each number on its own line, end with CTRL-D) file descriptors 0, 1 and 2 commonly referred to as: stdin, stdout, stderr Images from http://www.catonmat.net/blog/bash-one-liners-explained-part-three/

Computing your score so far What did we just do? stdin Thanks to the pipe we used, instead the input to the sum program came from those SCORE.* files (actually sum.sh didn t read files, it read the output of cat) keeps sum.sh program very simple Also works with C programs (file descriptors 0 and 1, open() and dup2()) Images from http://www.catonmat.net/blog/bash-one-liners-explained-part-three/

More common tools grep: print lines matching a pattern grep Part lab0-comments strange name, used as noun or verb simple and very useful example above: find any line containing Part in file lab0-comments (contains several lines) - case sensitive! try with part instead (-i makes it insensitive) could write result to a file with: grep Part lab0-comments > mylab0parts grep photoshoot by Christiaan Colen

Shells Bash shell: practical uses for your systems skills More common tools Pipes make grep even more useful Activity

More common tools Pipes make grep even more useful

Today 0 Shells History Usage Scripts vs. Programs 1 Bash shell: practical uses for your systems skills Computing your score so far More common tools 2 Activity Make a shell script

Make a shell script Extra credit opportunity Add up to 5 points to your point total Create a shell script called secretfinder.sh which: - create a directory called secretfiles and change into it - extract files from /home/csci2467/labs/misc/grep.tar - use grep command to find which file contains the SECRET AUTH CODE - write the name of that file to a new file called: authcode.txt - remove the 1000 files tar created (but leave authcode.txt) 1 - print Found the secret auth code! Saved to authcode.txt to the screen (not to the autocode.txt file) Put your solution in /home/csci2467/handin/<your-userid> 1 Again be careful with rm and see tips on next page

Make a shell script Some tips Begin your file (the very 1st line) with #!/bin/bash - after that you can comment each line using the # symbol - put your name and the date in a comment at the top - comment subsequent commands To extract files use: tar xf /home/csci2467/labs/misc/grep.tar To delete the 1000 files, use rm f*== notice how the above command should only delete files that start with f and end with ==

Make a shell script Scoring Total 5 points - 1 point: file is named properly, begins with interpreter line, valid comments for each line - 1 point: created directory and extracts files - 1 point: finds AUTH CODE - 1 point: writes AUTH CODE to correct file - 1 point: cleans up 1000 files and prints only one line of output.