CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Introduction to Shell Script Programming
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux Operations and Administration
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
CS465 - UNIX The Bourne Shell.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Intro Python: Variables, Indexing, Numbers, Strings.
Writing Scripts Hadi Otrok COEN 346.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
CIRC Winter Boot Camp 2017 Baowei Liu
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
CSc 352 An Introduction to the C Preprocessor
Essential Shell Programming
Shell Control Structures
Shell Control Structures
CSc 352 An Introduction to make
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu

What is a shell script? A shell script is a list of commands to be run by a shell basically a program, but using shell commands instead of C or Java statements Why? automate repetitious tasks e.g.: testing a program on a large set of test inputs package up commonly executed command sequences create our own commands

Creating and executing a shell script Create a file, say “foo”, containing the commands to be executed by the script Execute the script: invoke the appropriate shell with foo as argument, e.g.: bash foo csh foo chmod a+x foo ./foo

A simple example a shell script to print “hello world!” the “echo” command, as executed from the command line contents of the shell script file “hello_world.script” this script behaves the same for a variety of different shells with appropriate permissions, the script can be executed directly

Syntax issues across shells Different shells have slightly different syntax rules the same script may not work on all shells: script contents different behaviors

Which shell runs my script? my current shell is csh the script executes as expected when csh is invoked explicitly if not invoked explicitly, the default shell /bin/sh is used

Solution: specify shell in the script #!pathToShell on first line specifies which shell to use executes as expected when invoked in csh executes as expected when invoked in bash

Specifying the shell interpreter #!/bin/csh ‘#!’ at the beginning of a file indicates that it is a script The rest of the first line specifies (absolute) path to the interpreter What if the interpreter is at a different location on a different system? use /usr/bin/env to improve portability: #!/usr/bin/env csh

Variables Variables different than in C or Java: don’t have to be declared in advance they are untyped: the same variable can hold an integer value or a string Syntax for using variables (bash): Defining the value of a variable X: X=value Using the variable X: $X or ${X}

Example variable definitions variable uses undefined variable evaluates to empty string

Guarding against uninitialized variables -u : “exit if using an unitialized variable” -u : works if we specify /bin/bash but not with /usr/bin/env

Guarding against uninitialized variables

Bash scripts: whitespace without whitespace  the command works as expected with whitespace  weird error message variable value not set as expected

Different kinds of quotes back quotes: evaluates to the output of the enclosed commands single quotes: variables not expanded double quotes: variables inside the quotes are expanded

A simple scripting problem Iterate over the files in a given directory: for each file, express its size in human-readable form (i.e., using KB and MB where appropriate): give 2 decimal places for MB sizes; 1 decimal place for KB sizes functionality similar to using “ls –lh” Requires: list files in a directory [valid directory?  if-then-else] iterate over these files obtain the size of each file perform arithmetic [KB/MB decision  if-then-else]

The test command Used to guide control flow in conditionals and loops Syntax: test operand1 operator operand2 test operand often abbreviated to: [ operand1 operator operand2 ] [ operand ] see “man test” for a list of operators Returns a nonzero status if the test is true; zero o/w

Example: test

Example: if-then-else if-then-else command

Example: if-then-else need a ‘;’ between the test expression and ‘then’

Example: if-then-else

Alternative formulation of test

For Loops Syntax 1: for X in word ; do cmds done Convenient for iterating over lists Syntax 2: for (( e1; e2; e3 )) ; do cmds done Convenient for iteration involving arithmetic expressions

For Loops: Example 1

For Loops: Example 2

Iterating over lists: pitfalls ! print out the first couple of lines of a file listing

Iterating over lists use quotes to prevent splitting at whitespace Note: the loop iterates only once – not once per file!

Arithmetic arithmetic evaluation doesn’t happen automatically; must be specified explicitly get file size (see “man stat” for other file properties)

Arithmetic integer arithmetic only! arithmetic evaluation: $(( expr ))

get bc to do the floating point math for us Arithmetic get bc to do the floating point math for us

Command-line arguments Arguments at known positions, e.g., 1st, 2nd, ..., can be accessed using $1, $2, … $0 = the name of the script file Total no. of arguments: $# However, we can’t get to the ith argument this way $i  value of variable i

Example

Iterating over the argument list

Getting at the entire list of arguments

Iterating over the argument list Placement of quotes matters: all of the arguments are seen as a single word

Iterating over the argument list Placement of quotes matters:

Arguments with whitespace $@ is similar to $*, but each argument is a quoted string no interpretation or expansion allows us to properly access arguments that may contain whitespace needs to be quoted “…”

Arguments with whitespace

Debugging

prints shell input lines as they are read Debugging: -v prints shell input lines as they are read

Debugging: -v these are the lines from the script that are being executed Note that variable values etc. are not being shown

Debugging: -x prints out command traces before they are executed

Debugging: -x variable values, command arguments are printed out

Debugging: summary Debugging can be done by observing what commends get executed “set –v” prints out shell commands as they are read in does not print out runtime variable values “set –x” prints out a trace of the command before it is actually executed prints out variable and argument values Debugging can be turned off using “set +v”, “set +x” useful for localizing which parts of the script are observed

String manipulation shell scripts often involve string manipulation e.g., to pattern match on file names, create new files based on existing files, etc. bash gives extensive facilities for string matching and manipulation see “man expr”

A sample problem Write a bash script to convert Unix make files to “mymake” make files unix make mymake a : b.o c.o d.o gcc *.o –o a b.o : b.c b.h gcc –Wall –c b.c @target a : b.o c.o d.o @cmd gcc *.o –o a @target b.o : b.c b.h @cmd gcc –Wall –c b.c 

Attempt 1 Ignore definitions Assume Issues: assume make file does not have stuff like “CC = gcc” Assume user has added whitespace where necessary e.g.: on either side of ‘ : ’ in rules all dependencies have been made explicit Issues: need to process a line at a time lines may contain whitespace need to check whether a line starts with a ‘\t’

Attempt 1

Attempt 1 IFS = “Internal Field Separator” used by bash to split the input into words the new definition of IFS means that the input is now split at newlines identify lines that begin with a tab

Attempt 1

Attempt 2 relax assumption about whitespace around : Issue: want: input: “a.o: a.c b.h c.h” output: “@target a.o : a.c b.h c.h” Issue: replace ‘:’ by ‘ : ’ bash offers two replacement operators: ${str/substr/replacement} replaces the first occurrence of substr in str with replacement ${str//substr/replacement} replaces all occurrences of substr in str with replacement

Attempt 2

Attempt 2

Only the first ‘:’ had whitespace inserted Attempt 2 Only the first ‘:’ had whitespace inserted

“replace all occurrences” Attempt 2 “replace all occurrences”

Attempt 2

Attempt 3 Relax assumption about definitions Issues: allow “CC=gcc”, “CFLAGS= –Wall –g –c” assume no whitespace around ‘=‘ Issues: identify definitions, i.e., lines that contain a ‘=‘ extract LHS, RHS of the definition replace every occurrence of LHS by RHS

Attempt 3 if the line contains a ‘=‘ … figure out where it occurs LHS = substring from position 0 to position of ‘=‘ RHS = substring after position of ‘=‘ to end substitute RHS for every occurrence of LHS

Attempt 3

Attempt 3 Oops!

Attempt 3

Attempt 3