Shell Script Programming 2

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

Linux shell scripting Getting started *

Lecture #13 AWK Part I (Chapter 6)

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

5/8/2012. Exploring Utilities Chapter 5

bash, part 3 Chris GauthierDickey

Shells and Shell Programming

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

Shells and Shell Programming

Review of Fundamentals

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

A shell can be used in one of two ways:

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CS Unix Tools & Scripting

UNIX System Programming Lecture 3: BASH Programming

BASH SHELL SCRIPT 1- Introduction to Shell

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

UNIX II:grep, awk, sed. October 30, 2017

Prefix COMP Course# 421 Title Unix for Programmers Units (3) 3 hours lecture per week 3 hours lecture per week

9.2 Linux Essentials Exam Objectives

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

Implementation of a simple shell, xssh

COMP2100/2500 Lecture 17: Shell Programming II

COMP 4/6262: Programming UNIX

Basic Linux (Bash) Commands

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

CALIFORNIA STATE UNIVERSITY CHANNEL ISLANDS COURSE MODIFICATION PROPOSAL

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Introduction of Linux

Implementation of a simple shell, xssh

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

UNIX Shell Programming

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

Shell Programming Overview

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Shells & Shell Programming (Part B)

Introduction to Supercomputing

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

More Scripting Techniques Scripting Process Example Script

Continue reading command lines even after an execution error has caused the abandonment of aline.

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

The Unix Shell & Shell Scripts

User Commands sed ( 1 )

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

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

What is Bash Shell Scripting?

Systems Programming/ C and UNIX

PROGRAMMING PROJECT ONE DEVELOPING A SHELL

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

Shell Programming Systems Skills in C and Unix

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

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

You must have a basic understanding of GNU/Linux operating system and shell scripting.

4. COMPILING AND RUNNING FORTRAN PROGRAMS

CST Algonquin College 2

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

% echo $SHELL /usr/local/bin/bash. % sh $

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Advanced UNIX Tools. Student Workbook

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Introduction to the Shell

Fall 2006 Shell programming, part 3. touch

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?

Manual Shell Script Linux If Not Equal String Comparison

Useful Unix Commands Cheat Sheet

bash Execution Control COMP2101 Winter 2019

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

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

Part 1: Basic Commands/U3li3es

5/20/2007. Touring Essential Programs

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

Introduction to Scripting using bash

Makefile Brief Reference

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Chapter-3. Introduction to Unix: Fundamental Commands

Practical 02. Bash & shell scripting

Shell Programming (bash)

CMPS 12A Introduction to Programming Lab Assignment 7

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

Essential Skills for Bioinformatics: Unix/Linux

The Shell, System Calls, Processes, and Basic Inter-Process Communication

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

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

The Online Unix Manual

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

Basics. proper programmers write commands

Manual Shell Script Linux If File Exists And

Chapter 4. Unix Tutorial. Unix Shell

Introduction to the shell Part II

Why Bourne Shell? A Bourne Shell Script. The UNIX Shell. Ken Wong Washington University. The Bourne Shell (CSE 422S)

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Unix/Linux: History and Philosophy

Linux shell scripting intro/review

About this course 1 Recommended chapters... 1 A note about solutions... 2

File Commands. Objectives

Manual Shell Script Linux If File Exists Wildcard

Assignment clarifications

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

Transcription:

Shell Script Programming 2 David Morgan Useful capabilities parameter processing validation usage checking user input custom functions filenames decomposition, tempnames, random names action hinged on compound condition evaluation (ifless if) sourcing execute commands from a file in current shell #include functions in such a file, by reference debugging 1

Supplemental supporting utilities find generate qualified file name lists xargs build command lines from stdin sed dynamic, programatic, in-place editing awk text manipulation bc arbitrary precision math capabilities lacking or weak in shell often utilized in scripts Special shell variables $0 script name from command line $1, $2, etc command line parameters $* - command line parameters collectively $$ - process ID (PID) of current process $? exit status of last command 2

shift positional parameter promoter getopts getopts vs getopt a shell built-in a stand-alone binary not identical processes command-line options 3

Command-line format command [ - options ] [ arguments ] ls -l m* tokens Some options take a value, some commands take arguments command [ - options ] [ arguments ] myprog [ -x value ] [-y] [ aaa ] [ bbb ] [ ccc ] an option (-d ) taking a value ( : ) a command ( cut ) taking an argument ( /etc/passwd ) 4

getopts command resides in a script knows the script s allowable options parses the command line that called the script executed successively, finds/returns the options in the line one at a time used in loops to read/detect them one after the other detect phase normally followed by programmed response phase getopts documentation (from man bash ) getopts optstring name [args] getopts is used by shell procedures to parse positional parameters. optstring contains the option characters to be recognized; if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. The colon and question mark characters may not be used as option characters. Each time it is invoked, getopts places the next option in the shell variable name, ini- tializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable OPTARG. The shell does not reset OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used. When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-option argument, and name is set to?. getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead. getopts can report errors in two ways. If the first character of optstring is a colon, silent error reporting is used. In normal operation diagnostic messages are printed when invalid options or missing option arguments are encountered. If the variable OPTERR is set to 0, no error messages will be displayed, even if the first character of optstring is not a colon. If an invalid option is seen, getopts places? into name and, if not silent, prints an error message and unsets OPTARG. If getopts is silent, the option character found is placed in OPTARG and no diagnostic message is printed. If a required argument is not found, and getopts is not silent, a question mark (?) is placed in name, OPTARG is unset, and a diagnostic message is printed. If getopts is silent, then a colon (:) is placed in name and OPTARG is set to the option character found. getopts returns true if an option, specified or unspecified, is found. It returns false if the end of options is encountered or an error occurs. 5

functions install runnable code unit in memory under a callable name A shell function stores a series of commands for later execution. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Functions are executed in the context of the current shell; no new process is created to interpret them (contrast this with the execution of a shell script). bash man page functions 6

filenames decomposition basename dirname temporary names random names Filename decomposition 7

Generating working/temp filenames date based Generating working/temp filenames RANDOM variable, mktemp command in /tmp in some other directory 8

if-less if if [... ]; then xxx fi [... ] && xxx seen often understood seldom a (too?) slick way to do conditionals if-less if Some examples found in rc.sysinit AND-based righthand action only if lefthand condition true: [ -x /usr/bin/plymouth ] && PLYMOUTH=yes [! -f "$file" ] && continue [ "$READONLY" = "yes" ] && return 1 [ -f /var/log/dmesg ] && mv -f /var/log/dmesg /var/log/dmesg.old OR-based righthand action only if lefthand condition not true: [ -f /.autorelabel ] touch /.autorelabel 9

source execute code from a file in current shell often used on a file containing functions effectively similar to c language #include source filename [arguments] Read and execute commands from filename in the current shell environment bash man page find searches for files in a directory tree described by an expression expression consists of elements options tests actions each element returns boolean result find evaluates as many elements of its expression as needed to know expression s outcome 10

Most common use for <all files in a set of files> next if <something about the file> do <something with the file> but the operation details are more complex than that find example expression find. -maxdepth 1 -size +1000000c -print an option a test an action find files 1) in the current directory (no subdirectory search) 2) bigger than a million bytes 3) and print their names 11

Some example elements find. options* tests actions maxdepth name print mount atime +n ls etc size +n exec executable ok type etc empty false etc * find s options, not shell command options Some example elements find. options tests actions what it returns: true always true or false true or false what it does: influence nothing their particular find behavior action 12

Operational logic "[evaluates] the given expression from left to right... until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name. - find man page Operational logic because -name A* is false for B* files because printing happens before -name A* evaluation from the 2 nd -print (2 nd print doesn t happen for B* files) 13

exec action arbitrary response for qualifying files needs to be terminated with ; uses {} as placeholder for current file need to escape these from shell a finder script command: find. -type f exec grep l $1 {} \; print names of all files in current directory containing a given string String manipulation 14

sed stream editor used for search and replace filters standard input to standard output sed stream editor prints 2 lines replace 1 st o with *, each line all o s replace any h or e replace any vowel replace any letter or numeral replace anything neither h nor e replace anything neither letter nor numeral 15

awk (or gawk) a pattern scanning and processing language better text processing facilities than shell s often used in scripts to break text into fields gawk gawk <program> <input files> { <pattern> <action> } gawk processes all lines in the input, comparing each to the pattern and, for those that match, performing the action 16

gawk the program the pattern the action pattern only: default action is to print whole line action only: default pattern selects all lines bc arbitrary precision calculator strong computation features operates internally in decimal interactive offers command prompt programmable weak programming features complements shell (weak comp, strong prog) use with shell pipe commands to bc from shell result returns to shell from bc 17

bc expressions high integer precision high integer precision bc how shell does it (no decimals) high decimal precision how bc does it high decimal precision decimal periodicity = 1 decimal periodicity = 2 decimal periodicity = 6 18

bc 7, 97, 983 and other primes have special properties (see cyclic numbers ) periodicity = 7 1 = 6 high integer precision periodicity = 97 1 = 96 periodicity = 983 1 = 982 bc - functions enter function code at bc prompt utilize it thereafter 19

bc function library files mod( ) no such function but here s a file that defines such a function (plus others) file s contained functions are now available bc code from standard input things you do in bc you can have done by bc in the shell 20

quick RSA tutorial with suitably chosen values for e, n, and d * an integer m encrypts to: and can be recovered by: c= m e mod n c d mod n a set of suitable values (ones that work): e 15941 n 48863 d 17741 * pair {e,n} is called public key and pair {d,n} private key encrypt and recover 1001 in bc 21

encrypt and recover 1001 in shell script use command substitution where the command is bc use here doc to drop in entire bc prog any bc code that returns a desired calculated value 22

Looping helpers debugging scripts sectional debugging turn on with set x, off with set +x within script whole-script debugging set x at command line before running script shabang line in script: #!/bin/bash -x 23

Resources Advanced Bash-Scripting Guide, An in-depth exploration of the art of shell scripting; Mendel Cooper GNU manuals: find: http://www.gnu.org/software/findutils/manual/find.html grep: http://www.gnu.org/software/grep/manual/ sed: http://www.gnu.org/software/sed/manual/sed.html bc: http://www.gnu.org/software/bc/manual/bc.html Classic Shell Scripting Arnold Robbins and Nelson A. F. Beebe, O'Reilly & Associates, 2005 UNIX Shells by Example Ellie Quigley; Prentice Hall, 4th edition, 2005 Learning the bash Shell Cameron Newham and Bill Rosenblatt, O'Reily & Associates, 3rd edition, 2005 Introducing Regular Expressions Michael Fitzgerald, O'Reilly & Associates, 2012 Mastering Regular Expressions Jeffrey E. F. Friedl, O'Reilly & Associates, 3rd edition, 2006 Sed and Awk Dale Dougherty, O'Reilly & Associates, 1997 The AWK Programming Language, Alfred Aho, Brian Kernighan, Peter Weinberger, Addison-Wesly Publishing Company, 1988 24