Shell Programming Systems Skills in C and Unix

Similar documents
Script Programming Systems Skills in C and Unix

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

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

Introduction to Supercomputing

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

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

The Unix Shell. Pipes and Filters

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

5/20/2007. Touring Essential Programs

UNIX Basics. UNIX Basics CIS 218 Oakton Community College

Essentials for Scientific Computing: Bash Shell Scripting Day 3

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

5/8/2012. Exploring Utilities Chapter 5

Command-line interpreters

Linux Shell Script. J. K. Mandal

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

List of Linux Commands in an IPm

Useful Unix Commands Cheat Sheet

Introduction To Linux. Rob Thomas - ACRC

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

Unix Tools / Command Line

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

Shells and Shell Programming

Perl and R Scripting for Biologists

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

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

Introduction To. Barry Grant

Linux Refresher (1) 310/ Fourth Workshop on Distributed Laboratory Instrumentation Systems (30 October - 24 November 2006)

Part 1: Basic Commands/U3li3es

UNIX System Programming Lecture 3: BASH Programming

Introduction: What is Unix?

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

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Shells and Shell Programming

Chapter 9. Shell and Kernel

EECS2301. Lab 1 Winter 2016

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

UNIX Essentials Featuring Solaris 10 Op System

for more :-

Assignment clarifications

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Chapter-3. Introduction to Unix: Fundamental Commands

Introduction to Unix: Fundamental Commands

Utilities. September 8, 2015

Introduction to UNIX Part II

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

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

Introduction to Linux. Roman Cheplyaka

Scripting Languages Course 1. Diana Trandabăț

Shells & Shell Programming (Part B)

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

SANJAY GHODAWAT POLYTECHNIC

Vi & Shell Scripting

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

Basic Linux (Bash) Commands

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

Automating Tasks Using bash

The Unix Shell & Shell Scripts

Unix Basics. Benjamin S. Skrainka University College London. July 17, 2010

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

Operating Systems. Copyleft 2005, Binnur Kurt

Introduction To. Barry Grant

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

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

Unix basics exercise MBV-INFX410

Recap From Last Time:

BGGN 213 Working with UNIX Barry Grant

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

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

On successful completion of the course, the students will be able to attain CO: Experiment linked. 2 to 4. 5 to 8. 9 to 12.

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

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

Advanced Linux Commands & Shell Scripting

Computer Systems and Architecture

Recap From Last Time: Setup Checklist BGGN 213. Todays Menu. Introduction to UNIX.

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

Introduction. Let s start with the first set of slides

Computer Systems and Architecture

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

Linux Essentials Objectives Topics:

Introduction to UNIX command-line

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

System Administration

Files and Directories

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

The Shell. EOAS Software Carpentry Workshop. September 20th, 2016

Lecture 02 The Shell and Shell Scripting

Unix Shells and Other Basic Concepts

Introduction to Shell Scripting

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

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

Practical 02. Bash & shell scripting

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

C Shell Tutorial. Section 1

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

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

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

TestOut Linux Pro - English 4.0.x OBJECTIVE MAPPING: CompTIA Linux+ LX0-103

Transcription:

Shell Programming 15-123 Systems Skills in C and Unix

The Shell A command line interpreter that provides the interface to Unix OS.

What Shell are we on? echo $SHELL Most unix systems have Bourne shell (sh) No command history Kornshell (ksh) Shell functions C shell (csh) History, no shell functions More details at unix.com

What s Shell good for? Starting and stopping processes Controlling the terminal Interacting with unix system Solving complex problems with simple scripts Life saver for system administrators Life saver for system administrators What is a shell script? A collection of shell commands supported by control statements Shell scripts are interpreted and instructions executed

Quick review of basics

A Shell Script #!/bin/sh --above line should always be the first line in your script # A simple script who am I Date Execute with: sh first.sh

Another shell script

Command Line Arguments $# -represents the total number of arguments (much like argv) except command $0 -represents the name of the script, as invoked $1, $2, $3,.., $8, $9 -The first 9 command line arguments Use shift command to handle more than 9 args $* -all command line arguments OR $@ -all command line arguments

What are the three kinds of quotes in Shell expressions?

Capturing output from a shell operation A major bug: Did not catch if the program segfaulted

Control Statements Loops and conditionals

Useful shell commands Shell already has a collection of rich commands Some Useful commands uptime, cut, date, cat, finger, hexdump, man, md5sum, quota, mkdir, rmdir, rm, mv, du, df, find, cp, chmod, cd uname, zip, unzip, gzip, tar tr, sed, sort, uniq, ascii Type man command to read about shell commands

What do these shell commands do? cat dups.txt sort uniq cat somefile.txt sed's/ /,/g' > outfile cat somefile.txt sed's# #,#g' > outfile cat somefile.txt sed'1,10 s/ /,/g' > outfile cat somefile.txt sed'1,$ s/ /,/g' > outfile cat somefile.txt sed'/^[0-9]+/ s/ /,/g' > outfile cat file cut -d: -f3,5 cat file.txt tr"abcd" "ABCD" > outfile.txt

More of those cat file.txt tr"a-z" "A-Z" > outfile.txt cat file.txt tr-d "\015" > outfile.txt cat somefile.txt tr"\015" "\012" > somefile.txt

File descriptors I/O Stdin(0), stdout(1), stderror(2) Input/output from/to stdin/stdout read data echo $data redirecting rmfilename 1>&2

Unix tools in shell scripts Shell scripts can include utilities such as grep sed awk Pattern matching Stream editor Pattern scanning and processing Read more in notes and man pages

Interprocesscommunication

Inter Process Communication (IPC) Communication between processes Using Pipes Pipes is the mechanism for IPC ls sort echo 4 processes in play Each call spans a new process Using folk More later about folk

Editing in Place cat somefile.txt tr-d "\015" "\012" fold > somefile.txt What does it do? What are some of the problems? Problems are caused by the way pipes work

How does pipes work A finite buffer to allow communication between processes Typically size 8K If input file is less than the buffer We may be ok What if input file is more than the buffer Redirecting output to the same file is a bad idea

How to deal with this? Use a temp file cat file tr-d "\015""\012" fold > file.tmp mvfile.tmp file Better process cat file tr-d "\015" "\012" fold > "/usr/tmp/file.$$" mv"/usr/tmp/file.$$" file /usr/tmp is cleared upon reboot

#!/bin/sh FILE=$1 Pipes, Loops and Sub shells cat $FILE while read value do echo ${value} done while loop is executed in a sub shell

What is the problem? #!/bin/sh FILE=${1} max=0 cat ${FILE} while read value do if [ ${value} -gt${max} ]; then max=${value} fi done echo ${max}

The fix #!/bin/sh FILE=${1} max=0 values=`cat ${FILE}` for value in ${values} do if [ ${value} -gt${max} ]; then max=${value} fi done echo ${max}

Arrays in bash array[2]=23 array[3]=45 array[1]=4 To dereference an array variable, we can use, for example echo ${array[1]} Array elements need not be consecutive and some members of the array can be left uninitialized. Here is an example of printing an array in bash. Note the C style loop. Also note the spaces between tokens. for (( i=1 ; i<=3 ; i++ )) do echo ${array[$i]} done

Coding Examples