Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

Similar documents
Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

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

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

COMP 4/6262: Programming UNIX

Conditional Control Structures. Dr.T.Logeswari

9.2 Linux Essentials Exam Objectives

Control Structures. CIS 118 Intro to LINUX

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Lecture 5. Essential skills for bioinformatics: Unix/Linux

CS Unix Tools & Scripting

A shell can be used in one of two ways:

Title:[ Variables Comparison Operators If Else Statements ]

Bash scripting basics

There are some string operators that can be used in the test statement to perform string comparison.

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Assignment 3, Due October 4

Shell Programming (bash)

Linux shell programming for Raspberry Pi Users - 2

Lab 4: Shell scripting

Grep and Shell Programming

bash Execution Control COMP2101 Winter 2019

Shells & Shell Programming (Part B)

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Shell Start-up and Configuration Files

Shell programming. Introduction to Operating Systems

Useful Unix Commands Cheat Sheet

Computer Systems and Architecture

Vi & Shell Scripting

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Open up a terminal, make sure you are in your home directory, and run the command.

CSC 2500: Unix Lab Fall 2016

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Computer Systems and Architecture

Essential Skills for Bioinformatics: Unix/Linux

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

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Assignment 2. Summary. Some Important bash Instructions. CSci132 Practical UNIX and Programming Assignment 2, Fall Prof.

Csci 132 Spring 13. Assignment 2 Due: Tuesday, March 5 (by 11:59PM)

Bash shell programming Part II Control statements

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Then, we modify the program list.1 to list two files.

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

Chapter 4. Unix Tutorial. Unix Shell

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

What is Bash Shell Scripting?

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

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Програмиранев UNIX среда

A Brief Introduction to the Linux Shell for Data Science

Shells and Shell Programming

Basic Linux (Bash) Commands

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Linux Bash Shell Scripting

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shells and Shell Programming

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

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

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

CSE 15L Winter Midterm :) Review

Introduction: What is Unix?

More Scripting Techniques Scripting Process Example Script

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

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

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

LOG ON TO LINUX AND LOG OFF

Assignment clarifications

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

BASH Programming Introduction

Getting Started. Logging In and Out. Adapted from Practical Unix and Programming Hunter College

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shell Programming Overview

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

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Linux 系统介绍 (III) 袁华

CENG 334 Computer Networks. Laboratory I Linux Tutorial

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

COMS 6100 Class Notes 3

UNIX shell scripting

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

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

Bourne Shell Reference

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

CS214 Advanced UNIX Lecture 4

Practice 5 Batch & Shell Programming

INTRODUCTION TO SHELL SCRIPTING Dr. Jeffrey Frey University of Delaware, IT

Exploring UNIX: Session 3

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

Shell Programming (ch 10)

Practical 04: UNIX / LINUX Shell Programming 1

Shell Programming (Part 2)

Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

Transcription:

Scripting More Shell Scripts Adapted from Practical Unix and Programming Hunter College Copyright 2006 2009 Stewart Weiss

Back to shell scripts Now that you've learned a few commands and can edit files, you can start to write shell scripts. You need a few more tools to make this possible. For starters, you need to know how a shell script can access the words you type on its command line. For example, suppose I want to write a script named swap that when called like this: swap word1 word2 would output this: word2 word1 41 Comp 190 Scripting Languages

Command line arguments Bash can access the words on the command line using the variables $1, $2, $3, and so on. The first word is stored in $1, the second, in $2, etc. The number of words on the line, excluding the command itself, is stored in a variable named $#. Therefore, our swap script would be as simple as #!/bin/bash echo $2 $1 This displays the second word, then the first word 42 Comp 190 Scripting Languages

Adding error-checking: test Scripts should always check that they have the correct number of arguments and that they are all valid. swap just has to check that there are exactly 2 words, so it needs to test whether $# is equal to 2. The test command evaluates an expression. For example test 1 -ne 2 is true because 1 is not equal to 2. test 2 -eq 2 is true because 2 equals 2. The other operators are -le, -lt, -gt, and -ge. Can you guess what these mean? 43 Comp 190 Scripting Languages

Other forms of test If you read the man page for test, you will see that there are other ways to use it. For example [ 1 -ne 2 ] is equivalent to test 1 -ne 2 You can put square brackets around an expression but there must be spaces on either side of them: [1 -ne 2] would be an error. 44 Comp 190 Scripting Languages

Using the if command Every shell has a command named if. In bash, you have to follow a very specific set of rules to use it. It look like if test-command then any sequence of commands fi where the words if, then, and fi are on separate lines. 45 Comp 190 Scripting Languages

Example if command if test $# -ne 2 then echo usage: swap word1 word2 exit fi This piece of shell script will print a usage message if the number of words on the command line is not equal to 2. It will also quit immediately after printing the message, no matter what commands follow the word fi. 46 Comp 190 Scripting Languages

Putting it all together We can put the testing bit of stuff ahead of our echo command to do our input-checking, and we now have a safe script: #!/bin/bash if test $# -ne 2 then echo usage: swap word1 word2 exit fi echo $2 $1 47 Comp 190 Scripting Languages

Another Type of Test The test command has many different types of tests. Many are called file tests and they can be used to test whether a file exists, or is of a given type, or size, or has some other property. For example: #!/bin/bash if test -e $1 then echo $1 exists fi 48 Comp 190 Scripting Languages

Negating Tests There is no test that is true if a file does not exist. If you want to print an error message if the user did not supply a filename, you need to negate the test. The exclamation mark negates expressions: if test! -e myfile is true if myfile does not exist, and is false if it does. If $1 is a command line argument then if test! -e $1 is true if it is not the name of a file that exists. 49 Comp 190 Scripting Languages

Shell comments Add your authorship and other information in a comment (a line that starts with #: #!/bin/bash # Written by Stewart Weiss, 09/24/2009 if test $# -ne 2 then echo usage: swap word1 word2 exit fi echo $2 $1 50 Comp 190 Scripting Languages

Shell comments Add your authorship and other information in a comment (a line that starts with #) #!/bin/bash # Written by John Barr, 09/20/2018 if test $# -ne 2 then echo usage: swap word1 word2 exit fi cat $2 $1 > newfile.txt # Any shell command works! 50 Comp 190 Scripting Languages

Adding comments to the script A line that starts with # and no! after it is called a comment. The shell ignores everything to the right of the #. In fact, the # can be written to the right of a command and the shell will ignore the rest of the line after the #: # Written by Stewart Weiss, 09/24/2009 # This script swaps words. echo $2 $1 # swap first and second words

Arithmetic $((expression)) Must enclose the expression in double quotes #!/bin/bash echo $(( 8#100 )) echo $(( 2#1010 )) i=10 echo $(( i+5 )) # can use spaces in expression but not in = i=$(( i *=5 )) echo $i

More Arithmetic $((expression)) Must enclose the expression in double quotes #!/bin/bash echo $a # spaces optional within paren a=$(( a + 2 )) echo $a b=$(( a < 100 )) # 0 = false, 1 = true echo $b

Loops while command do done command command... # Written by John Barr, 09/20/2018 i=1 # no spaces allowed! No $ necessary while [ $i -le 5 ] do echo $i # this is a command; need $ i=$((i+1)) #aritmetic expression! done

Example: a phonebook The phonebook: Alice Chebba 971-555-2015 Barbara Swingle 201-555-9257 Liz Stachiw 212-555-2398 Susan Goldberg Susan Topple 212-555-4932 201-555-7776 Tony Iannino 973-555-1295 Create this in your account.

Example: a phonebook The lu script: 1 #! /bin/bash 2 grep $1 phonebook Try these: $ lu Alice $ lu Susan $ lu Susan T $ lu "Susan T" Why does the last command still give 2 results?

Example: a phonebook extended The lu script version 2: 1 #! /bin/bash 2 grep "$1" phonebook Try these: $ lu Tony $ lu Susan $ lu Susan T $ lu "Susan T" grep saw "Susan T" as 2 different arguments! What difference do the quotes around $1 make? What do you get now?

Example: a phonebook adding Write a script named "an" to add an entry to the phonebook. Remember that there is a tab between the name and the number! Try these comands: $ an 'Stromboli Pizza' 973-555-9478 $ more phonebook

Example: a phonebook adding The an script version 1: 1 #! /bin/bash 2 echo "$1 $2" >> phonebook Note that there is a tab between the arguments. Try: $ an 'Stromboli Pizza' 973-555-9478 Is the phonebook still sorted?

Example: a phonebook adding The an script version 2: 1 #! /bin/bash 2 echo "$1 $2" >> phonebook 3 sort -o phonebook phonebook what does the -o flag do?

Example: a phonebook removing Write a script named "rem" to add an entry to the phonebook. Remember that there is a tab between the name and the number! Note: do not name your script rm! Why? Try these comands: $ rem 'Stromboli Pizza' $ more phonebook

Example: a phonebook remove The rem script version 1: 1 #! /bin/bash 2 grep -v "$1" phonebook > /tmp/phonebook 3 mv /tmp/phonebook phonebook Try: $ rem "Stromboli Pizza" $ rem Susan What happens with the last command? How would we fix this?

Example: a phonebook remove The rem script version 1: 1 #! /bin/bash 2 grep -v "$1" phonebook > /tmp/phonebook 3 mv /tmp/phonebook phonebook How do we fix this? Must recognize: 1. If there are no matches 2. If there is more than 1 match To find the number of matches: grep -o "$1" phonebook wc -l

Example: a phonebook remove The rem script version 1 How do we fix this? Must recognize: 1. If there are no matches 2. If there is more than 1 match To do find the number of matches: grep -o "$1" phonebook wc -l now must save the number of matches in a variable: lines=$(grep -o "$1" phonebook wc -l)

Example: a phonebook remove The rem script version 1 How do we fix this? Must recognize: 1. If there are no matches 2. If there is more than 1 match Now we just need to test the value in $line See the next 2 slides.

Example: a phonebook remove The rem script version 2: 1 #!/bin/bash Check for the 2 if [ "$#" -ne 1 ] correct number 3 then of arguments 4 echo "Incorrect number of arguments." 5 echo "Usage: rem name" 6 exit 1 7 fi 8 9 lines=$(grep -o "$1" phonebook wc -l) 10 Line numbers are part of the editor, not the code!

Example: a phonebook remove The rem script version 2 (cont): 11 if [ $lines -eq 0 ] 12 then 13 echo "no match for " $1 14 exit 1 15 elif [ $lines -gt 1 ] 16 then 17 echo "Too many matches" 18 grep "$1" phonebook 19 exit 1 20 else 21 grep -v "$1" phonebook > \tmp\phonebook 22 mv \tmp\phonebook phonebook 23 fi

Things to try Try creating a few simple scripts of your own. It will give you practice using gedit if you are at a UNIX console, or vi or nano if you are not. Read about the test command and learn its tricky syntax. Play around with > to store the output of various commands. 52 Comp 190 Scripting Languages