CS2043 - Unix Tools & Scripting Lecture 9 Shell Scripting



Similar documents
Lecture 4: Writing shell scripts

Bash shell programming Part II Control statements

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Unix Scripts and Job Scheduling

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

CS2720 Practical Software Development

Introduction to Shell Programming

HP-UX Essentials and Shell Programming Course Summary

Introduction to Shell Scripting

Systems Programming & Scripting

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

BASH scripting. Sebastian von Alfthan Scripting techniques CSC Tieteen tietotekniikan keskus Oy CSC IT Center for Science Ltd.

Automating admin tasks using shell scripts and cron Vijay Kumar Adhikari.

Hands-On UNIX Exercise:

Answers to Even-numbered Exercises

Advanced Bash Scripting. Joshua Malone

A Crash Course on UNIX

Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References

Command Line Crash Course For Unix

Beginners Shell Scripting for Batch Jobs

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

AN INTRODUCTION TO UNIX

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

Answers to Even- Numbered Exercises

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

IBM Redistribute Big SQL v4.x Storage Paths IBM. Redistribute Big SQL v4.x Storage Paths

SFTP SHELL SCRIPT USER GUIDE

Thirty Useful Unix Commands

An Introduction to the Linux Command Shell For Beginners

Chapter 1. Backup service

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Linux Shell Script To Monitor Ftp Server Connection

Lecture 22 The Shell and Shell Scripting

Unix Shell Scripting Tutorial Ashley J.S Mills

1001ICT Introduction To Programming Lecture Notes

Command Line - Part 1

Setting up PostgreSQL

Setting Up the Site Licenses

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CTIS486 Midterm I 20/11/ Akgül

Regular Expressions. Abstract

USEFUL UNIX COMMANDS

Bash Tutorial. Eigil Obrestad and Erik Hjelmås. August 18, 2015

Automated Offsite Backup with rdiff-backup

Tutorial 0A Programming on the command line

Using SVN to Manage Source RTL

Syntax: cd <Path> Or cd $<Custom/Standard Top Name>_TOP (In CAPS)

UNIX, Shell Scripting and Perl Introduction

Monitoring a Linux Mail Server

ICS 351: Today's plan

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

Setting up Auto Import/Export for Version 7

Lecture 4. Regular Expressions grep and sed intro

Useless Use of * Slide 1. Useless Use of * Jan Schaumann jschauma@netmeister.org PGP: 136D 027F DC B42 47D6 7C5B 64AF AF22 6A4C

Forms, CGI Objectives. HTML forms. Form example. Form example...

A UNIX/Linux in a nutshell

Introduction to analyzing big data using Amazon Web Services

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

3.GETTING STARTED WITH ORACLE8i

Lecture 18 Regular Expressions

List of FTP commands for the Microsoft command-line FTP client

Lecture 25 Systems Programming Process Control

CS 1133, LAB 2: FUNCTIONS AND TESTING

Unix Sampler. PEOPLE whoami id who

Informatica e Sistemi in Tempo Reale

CPSC2800: Linux Hands-on Lab #3 Explore Linux file system and file security. Project 3-1

LECTURE-7. Introduction to DOS. Introduction to UNIX/LINUX OS. Introduction to Windows. Topics:

Object Oriented Software Design

1 Basic commands. 2 Terminology. CS61B, Fall 2009 Simple UNIX Commands P. N. Hilfinger

Secure Shell Demon setup under Windows XP / Windows Server 2003

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Cygwin command line windows. Get that Linux feeling - on Windows

L01 Introduction to the Unix OS

Unix the Bare Minimum

Learn Perl by Example - Perl Handbook for Beginners - Basics of Perl Scripting Language

MAX_RMAN_08137_IGNORE=5 DISK_RETENTION_POLICY='RECOVERY WINDOW OF 7 DAYS'

Birmingham Environment for Academic Research. Introduction to Linux Quick Reference Guide. Research Computing Team V1.0

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CS 103 Lab Linux and Virtual Machines

Automating FTP with the CP IT

Slides from INF3331 lectures - Bash programming

Advanced PBS Workflow Example Bill Brouwer 05/01/12 Research Computing and Cyberinfrastructure Unit, PSU

Linux Syslog Messages in IBM Director

How to Write a Simple Makefile

Running your first Linux Program

PHP Tutorial From beginner to master

Tour of the Terminal: Using Unix or Mac OS X Command-Line

A Swift Tutorial i. A Swift Tutorial

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Introduction to Programming and Computing for Scientists

Example of a Java program

Installing and Running MOVES on Linux

An Introduction to Using the Command Line Interface (CLI) to Work with Files and Directories

CS 170, Section 000, Fall 2009

Sources: On the Web: Slides will be available on:

UNIX Intro and Basic C shell Scripting

UNIX / Linux commands Basic level. Magali COTTEVIEILLE - September 2009

Transcription:

CS2043 - Unix Tools & Scripting Lecture 9 Shell Scripting Spring 2015 1 February 9, 2015 1 based on slides by Hussam Abu-Libdeh, Bruno Abrahao and David Slater over the years

Announcements Coursework adjustments (now 4 assignments plus a final project) A3 is out (due Friday 02 / 20)

Scripting This week we will discuss bash scripting. Before we begin, we will discuss a few preliminaries. (and quickly review a few things for the sake of completeness) Today s agenda: Shell variables Shell expansion Quotes in bash Running commands sequentially & exit codes Your first script Passing arguments to scripts If conditionals

Variables Bash scripting is very powerful! To get anything done we need variables. To read the values in variables, precede their names by a dollar sign ($). The contents of any variable can be listed using the echo command Example: Two types of variables: Local and Environment. echo $SHELL /bin/bash

Local Variables Local variables exist only in the current shell: Example: nsavva@maxwell: $ x=3 nsavva@maxwell: $ echo $x 3 Note: There cannot be a space after the x nor before the 3!

Environment Variables Environment Variables are used by the system to define aspects of operation. The Shell passes environment variables to its child processes Examples: $SHELL - which shell will be used by default $PATH - a list of directories to search for binaries $HOSTNAME - the hostname of the machine $HOME - current user s home directory To get a list of all current environment variables type env New Environment Variable: To set a new environment variable use export nsavva@maxwell: $ export X=3 nsavva@maxwell: $ echo $X 3 Note: NO Spaces around the = sign.

A Word About the Difference The main difference between environment variables and local variables is environment variables are passed to child processes while local variables are not: Local Variable: nsavva@maxwell: $ x=3 nsavva@maxwell: $ echo $x 3 nsavva@maxwell: $ bash nsavva@maxwell: $ echo $x nsavva@maxwell: $ Environment Variable: nsavva@maxwell: $ export x=myvalue nsavva@maxwell: $ echo $x myvalue nsavva@maxwell: $ bash nsavva@maxwell: $ echo $x myvalue nsavva@maxwell: $

Environment Variables Again... When we say the Shell passes environment variables to its child processes, we mean a copy is passed. If the variable is changed in the child process it is not changed for the parent Example: nsavva@maxwell: $ export x=value1 nsavva@maxwell: $ bash nsavva@maxwell: $ echo $x value1 nsavva@maxwell: $ export x=value2 nsavva@maxwell: $ exit nsavva@maxwell: $ echo $x value1

Listing and Removing Variables env - displays all environment variables set - displays all shell/local variables unset name - remove a shell variable unsetenv name - remove an environment variable

Shell Expansions The shell interprets $ in a special way. Example If var is a variable, then $var is the value stored in the variable var. If cmd is a command, then $(cmd) is translated to the result of the command cmd. nsavva@maxwell: $ echo $USER nsavva nsavva@maxwell: $ echo $(pwd) /home/nsavva

Arithmetic Expansion The shell will expand arithmetic expressions that are encased in $(( expression )) Examples nsavva@maxwell: $ echo $((2+3)) 5 nsavva@maxwell: $ echo $((2 < 3)) 1 nsavva@maxwell: $ echo $((x++)) 3 And many more. Note: the post-increment by 1 operation (++) only works on variables

Quotes 3 different types of quotes, and they have different meanings: Single quotes ( ): Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. Double quotes ( ): Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $ \! Back quotes (`): Executes the command within the quotes. Like $().

Quotes Example nsavva@maxwell: $ echo "$USER owes me $ 1.00" nsavva owes me $ 1.00 nsavva@maxwell: $ echo $USER owes me $ 1.00 $USER owes me $ 1.00 nsavva@maxwell: $ echo "I am $USER and today is `date`" I am nsavva and today is Mon Feb 09 11:30:42 EST 2015

Running Commands Sequentially The ; Operator <command1> ; <command2> Immediately after command1 completes, execute command2 The && Operator <command1> && <command2> Example: command2 executes only if command1 executes successfully mkdir photos && mv *.jpg photos/ Creates a directory and moves all jpegs into it

Exit Codes The command after a && only executes if the first command is successful, so how does the Shell know? When a command exits it always sends the shell an exit code (number between 0 and 255) The exit code is stored in the variable $? An exit code of 0 means the command succeeded The man page for each command tells you precisely what exit codes can be returned Example: nsavva@maxwell: $ ls /Documents/cs2043 2012 2013 2014 2015 nsavva@maxwell: $ echo $? 0

You have the power! We now have a variety of UNIX utilities at our disposal and it is time to learn about scripting!

Scripting 101 Definition: A script is very similar to a program, although it is usually much simpler to write and it is executed from source code (or byte code) via an interpreter. Shell scripts are scripts designed to run within a command shell like bash. Scripts are written in a scripting language, like perl, ruby, python, sed or awk. They are then run using an interpreter. In our case, the scripting language and the interpreter are both bash.

The Shebang All the shell scripts we ll see in this course begin the same way: with a shebang (#!). This is followed by the full path of the shell we d like to use as an interpreter: /bin/bash Example: #! /bin/bash # This is the beginning of a shell script. Any line that begins with # (except the shebang) is a comment Comments are ignored during execution - they serve only to make your code more readable.

Simple Examples: Bash scripts can be as simple as writing commands in a file. Example: hello.sh #! /bin/bash echo "Hello World" Now set your file permissions to allow execution: Example: chmod u+x hello.sh And finally you can run your first shell script!./hello.sh Hello World!

Hello World - String Version Lets modify this slightly and use a variable: Example: hello2.sh #! /bin/bash STRING="Hello again, world!" echo $STRING Set your permissions and run: chmod u+x hello2.sh &&./hello2.sh Hello again, world!

A Backup Script Here is something a little more practical - a simple script to back up all the files in your documents directory: Example: backup.sh #! /bin/bash tar -czf /backups/cs2043backup.tar.gz /cs2043/

Backup Script With Date Lets add the current date to the name of our backup file. Example: backupwithdate.sh #! /bin/bash tar -czf /backups/cs2043 $(date +%d %m %y).tar.gz /cs2043/ Today, this will write to a file named cs2043_09_02_2015.tar.gz

Passing arguments to scripts When we pass arguments to a bash script, we can access them in a very simple way: $1, $2,... $10, $11 : are the values of the first, second etc arguments $0 : The name of the script $# : The number of arguments $* : All the arguments, $* expands to $1 $2... $n, $@ : All the arguments, $@ expands to $1 $2... $n You almost always want to use $@ $? : Exit code of the last program executed $$ : current process id.

Simple Examples multi.sh #! /bin/bash echo $(( $1 * $2 )) Usage:./multi.sh 5 10 Returns first argument multiplied by second argument To do arithmetic in bash use $(( math )) uptolow.sh #! /bin/bash tr [A-Z] [a-z] < $1 > $2 Usage:./uptolow.sh file1 file1low translates all upper case letters to lowercase and writes to file1low

If conditionals If statements are structured just as you would expect: if cmd1 then cmd2 cmd3 elif cmd4 then else cmd5 cmd6 fi Each conditional statement evaluates as true if the cmd executes successfully (returns an exit code of 0)

A simple script textsearch.sh #! /bin/bash # This script searches a file for some text then # tells the user if it is found or not. # If it is not found, the text is appended if grep "$1" $2 > /dev/null then else fi echo "$1 found in file $2" echo "$1 not found in file $2, appending." echo $1 >> $2

test expressions We would not get very far if all we could do was test with exit codes. Fortunately bash has a special set of commands of the form [ testexp ] that perform the test testexp. First to compare two numbers: n1 -eq n2 : tests if n1 = n2 n1 -ne n2 : tests if n1 n2 n1 -lt n2 : tests if n1 < n2 n1 -le n2 : tests if n1 n2 n1 -gt n2 : tests if n1 > n2 n1 -ge n2 : tests if n1 n2 If either n1 or n2 is not a number, the test fails.

Test Expressions We can use test expressions in two ways: test EXPRESSION [ EXPRESSION ] Either of these commands returns an exit status of 0 if the condition is true, or 1 if it is false. Use man test to learn more about testing expressions Note: Remember you can check the exit status of the last program using the $? variable.

Example #! /bin/bash # Created on [2/20/2009] by David Slater # Purpose of Script: Searches a file for two strings and prints which #is more frequent # Usage:./ifeq.sh <file> string1 string2 arg= grep $2 $1 wc -l arg2= grep $3 $1 wc -l if [ $arg -lt $arg2 ] then echo "$3 is more frequent" elif [ $arg -eq $arg2 ] then else fi echo "Equally frequent" echo "$2 is more frequent"

string comparison To perform tests on strings use s1 == s2 : s1 and s2 are identical s1!= s2 : s1 and s2 are different s1 : s1 is not the null string Make sure you you leave spaces! s1==s2 will fail!

Expansion When using testexp variable substitution is performed, but no matching is perform. If x is the null string, what will [ $x!= monster ] return?

Expansion When using testexp variable substitution is performed, but no matching is perform. If x is the null string, what will [ $x!= monster ] return? It will return an error, because $x is expanded to the null string and the test becomes [!= monster ]. To make sure there are no errors, place your variables inside double quotes. Then [ $x!= monster ] is expanded to [ ""!= monster ] which returns true.

path testing If path is a string indicating a path, we can test if it is a valid path, the type of file it represents and the type of permissions associated with it: -e path : tests if path exists -f path : tests if path is a file -d path : tests if path is a directory -r path : tests if you have permission to read the file -w path : tests if you have write permission -x path : tests if you have execute permission

More stuff We can now begin to ensure our scripts get the input we want: if [ -f $1 ] then else fi Perform the action you want echo "This script needs a file as its input dummy!"

More on testing You can combine tests: if [ testexp1 -a testexp2 ] then fi cmd -a : and -o : or! testexp1 : not

A note about debugging To debug your code, invoke the script with the -x option. You will then see all the commands successfully executed: $ bash -x ifeq.sh frankenstein.txt monster the ++ grep monster frankenstein.txt ++ wc -l + arg=33 ++ grep the frankenstein.xt ++ wc -l + arg2=3850 + [ 33 -lt 3850 ] + echo the is more frequent

Putting it on one line Sometimes we might want to type a multiline command into the shell, we can do this by hitting enter for each line, or by using semicolons to tell the shell to start new lines: Example: if [ testexpr ] ; then command1 ; command2 ; fi Real Example: if [ $? -eq 0 ] ; then echo "Last Command Successful" ; fi

Putting it on multiple line Sometimes we might want to type a multiline command into the shell, we can do this by hitting enter for each line, or by using semicolons to tell the shell to start new lines: Example: if [ testexpr ] then partofcommand1 \ command1continued command2; fi Real Example: if [ $? -eq 0 ] then ls \ *.txt pwd fi

Next Time