Session 2: Shell Scripting

Similar documents
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

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.

A Crash Course on UNIX

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

Lecture 4: Writing shell scripts

Advanced Bash Scripting. Joshua Malone

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

Unix the Bare Minimum

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Hands-On UNIX Exercise:

Introduction to Shell Scripting

Command Line - Part 1

Unix Scripts and Job Scheduling

UNIX, Shell Scripting and Perl Introduction

AN INTRODUCTION TO UNIX

Bash shell programming Part II Control statements

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

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

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

Beginners Shell Scripting for Batch Jobs

HP-UX Essentials and Shell Programming Course Summary

CSC 120: Computer Science for the Sciences (R section)

CS2720 Practical Software Development

Systems Programming & Scripting

Running your first Linux Program

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

Submitting batch jobs Slurm on ecgate. Xavi Abellan User Support Section

Unix Sampler. PEOPLE whoami id who

Creating a Simple Macro

MATLAB Programming. Problem 1: Sequential

EXTENDED FILE SYSTEM FOR F-SERIES PLC

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

L04 C Shell Scripting - Part 2

Chapter One Introduction to Programming

An Introduction to the Linux Command Shell For Beginners

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

Using Flow Control with the HEAD Recorder

Microsoft Windows PowerShell v2 For Administrators

Thirty Useful Unix Commands

Introduction to Python

Controlling LifeSize Video Systems from the CLI

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

Agenda. Using HPC Wales 2

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.

Beginner s Matlab Tutorial

Answers to Even- Numbered Exercises

PharmaSUG Paper QT26

CLC Server Command Line Tools USER MANUAL

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

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

L01 Introduction to the Unix OS

Welcome to GAMS 1. Jesper Jensen TECA TRAINING ApS This version: September 2006

Automated Offsite Backup with rdiff-backup

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction. Chapter 1

A UNIX/Linux in a nutshell

The Linux Operating System and Linux-Related Issues

Answers to Even-numbered Exercises

Exercise 1: Python Language Basics

PA2: Word Cloud (100 Points)

Introduction to Shell Programming

SharePoint 2007 Get started User Guide. Team Sites

Introduction to Minitab Macros. Types of Minitab Macros. Objectives. Local Macros. Global Macros

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Inteset Secure Lockdown ver. 2.0

Lecture 2 Notes: Flow of Control

Computer Programming In QBasic

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Appendix K Introduction to Microsoft Visual C++ 6.0

A Brief Introduction to the Use of Shell Variables

Using Parametric Equations in SolidWorks, Example 1

Alteryx Predictive Analytics for Oracle R

Kiko> A personal job scheduler

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Linux Shell Script To Monitor Ftp Server Connection

Computational Mathematics with Python

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

How to Write a Simple Makefile

TLMC WORKSHOP: THESIS FORMATTING IN WORD 2010

Introduction to CloudScript

UNIX Intro and Basic C shell Scripting

PHP Debugging. Draft: March 19, Christopher Vickery

Job Streaming User Guide

Using SVN to Manage Source RTL

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

SFTP SHELL SCRIPT USER GUIDE

2Creating Reports: Basic Techniques. Chapter

VHDL Test Bench Tutorial

Subversion workflow guide

UOFL SHAREPOINT ADMINISTRATORS GUIDE

Unix Shell Scripting Tutorial Ashley J.S Mills

CAPIX Job Scheduler User Guide

Business Portal for Microsoft Dynamics GP User s Guide Release 5.1

How to test and debug an ASP.NET application

Transcription:

Session 2: Shell Scripting Craig A. Struble, Ph.D. July 14, 2010 1 Introduction Shell scripts are a way to automate the execution of a series of Linux commands. Essentially, any command you can run from the Linux command line can be run as part of a script. While automation itself is a time saving skill, a more important reason to write shell scripts is that they provide a means for documenting your research steps and workflow. In some cases, scripting can lead to publication as your develop processes, methods, or algorithms that others find useful. Creating a Shell Script Creating a shell script consists of three basic steps: 1. Create a text file with a text editor like pico, vi or emacs containing the sequence of commands to execute. 2. Make the file executable with the command $ chmod a+x scriptname where scriptname is the name of your script. 3. Execute the script from the command line by typing $./scriptname assuming your script is in your working directory. We will explore some variations on this theme as we work through the session, such as adding command line parameters, and storing your scripts in a location so that you can access them from any directory. In general though, the three basic steps are all you need to be more productive. In this session, we focus on using the Bourne Again Shell (bash), which is commonly used on Linux systems. Several other shell scripting languages exist including C-Shell (csh), Bourne Shell (sh), and Z-Shell (zsh). Each shell has variations on syntax and functionality, but basically behave in the same way. As your comfort level grows, you can explore other shells to find the one that suits your needs best. 1.1 Assumptions This session assumes you are familiar with command line basics, are able to navigate the Linux directory structure, and can create and edit files using a Linux text editor (e.g. pico, vi, or emacs). These topics were covered in Session 1, and those notes can be used as a reference. 1

1.2 Exercise Exercise 1. Using a text editor, create a file named hello with the contents!/bin/bash read name echo Hello $name 2. Make it executable with the command $ chmod a+x hello 3. Run the script $./hello which means run the file named hello in my working directory (i.e. the./). The script will wait for you to type in some text and hit the return key. Once you hit return, Hello followed by what you typed should be displayed and the prompt returned to you. 4. Modify hello so that it prompts you to type in your name. Hint: use the echo command to print out the prompt before reading the name. Run your modified script. 2 Variables Variables Variables are named locations storing values. In the first exercise, name is a variable used to store the text you typed. General format for assigning variables variable=value where variable is the name you want to give the location and value is the value you want to store. A simple example year=2010 Retrieving Values Retrieve values preceding name with a dollar sign ($). So if we have the script year=2010 echo The year is $year 2

The output of the script will be The year is 2010 Variables are a good way to make your scripts more readable and more maintainable. following calendars script Consider the Without Variables!/bin/bash echo The calendar for 2010 is: cal 2010 echo Last "year s" calendar was: cal 2009 Using variables, while making calendars slightly longer, is much easier to understand and modify later. With Variables!/bin/bash year=2010 echo The calendar for $year is: cal $year echo Last "year s" calendar was: cal $(( year - 1 )) To change the year, only one modification is needed to the script instead of two in the first version (changing 2010 and 2009). Furthermore, the relationship between the years is much clearer when variables are used. When possible, consider using variables instead of directly including constant values in your scripts. The calendars script introduced a couple other things. First, notice the use of double quotes in the text "year s". This was necessary because a single quote ( ) is a special character used to create strings containing spaces and other characters. Another way to do this is to escape the single quote with a backslash like echo Last year\ s calendar was: The other concept introduced is arithmetic. Arithmetic is done with $(( EXPRESSION )) If the EXPRESSION is not a typical arithmetic expression using operators like +, -, * and /, an error will occur. bash shell scripting only supports integer operations, so it s not suited for complex mathematics. It is useful for many automation tasks, however. Exercises 1. Modify the calendars script so that it has a variable named p year for previous year. Assign it the value of the year previous to the one stored in year. 2. Continue to modify the calendars script so that it prints out 3

The calendar for the previous year 2009 was: instead of Last year s calendar was: using the p year variable. 3 Comments It s good practice to comment your scripts by writing meaningful notes that can be read to easier understand the meaning and intent of the script. In bash comments start with a hash symbol and run until the end of the line. The calendars script we ve been developing can be documented with a simple header that indicates it s purpose. With Comments!/bin/bash Print out this year s and last year s calendars. year=2010 echo The calendar for $year is: cal $year echo Last "year s" calendar was: cal $(( year - 1 )) 4 Command Line Parameters Editing scripts to make small modifications is not the desirable approach for creating general tools. More typically in Linux, command line parameters or arguments are used to pass information from the command line to a script. Consider the command./myscript one two three The command has three command line parameters, which are provided to numbered variables $1, $2, and $3 as illustrated below. Note that the command itself is assigned to $0../myscript one two three $0 $1 $2 $3 With Command Line Parameters!/bin/bash Print out this year s and last year s calendars. 4

year=$1 echo The calendar for $year is: cal $year echo Last "year s" calendar was: cal $(( year - 1 )) We can then execute the script with $./calendars 2010 or $./calendars 2020 The number of command line parameters provided to a script is stored in the special variable $.!/bin/bash Script name: nparams Print the number of command line parameters. echo You passed $ parameters. Exercise 1. Use the nparams script to find out how many command line parameters are passed in the following cases: $./nparams $./nparams one two three $./nparams "one two" three $./nparams "one two three" $./nparams one two three 2. Write a script named params to print out the first 5 command line parameters. Then, use that script to see what the parameters are for the following runs. $./params $./params one two three $./params "one two" three $./params "one two three" $./params one two three To get started, the script should look like!/bin/bash echo The first parameter is $1 echo The second parameter is $2 to print the first two parameters. 5

5 Conditional Execution Making decisions is frequently necessary in scripts. The general form of a conditional statement is if CONDITION then COMMAND1 COMMAND2... fi The commands COMMAND1, COMMAND2 up through the terminating fi keyword will be executed if the CONDITION has an exit status of 0 (which Linux interprets as true). Every program on Linux has an exit status, including scripts that either have the exit status of the last command executed or the status provided to the exit command, which we ll see below. Note that then and fi must be lines on themselves 1 In most situations, the test command, which is frequently abbreviated by [, is used. The test command has too many options to list here, but running $ man test will provide all the different possible tests that can be tried. We ll see one example below. Let s modify the calendars script so that it checks to make sure exactly one command line parameter is sent. With Conditional Statements!/bin/bash Print out this year s and last year s calendars. Check for one parameter. If the number of parameters is not equal (-ne) to 1, print usage and exit. if [ $ -ne 1 ] then echo "Usage: $0 year" exit 1 fi Everything is OK year=$1 echo The calendar for $year is: cal $year echo Last "year s" calendar was: cal $(( year - 1 )) 1 Actually, you can put them on lines with the if or the last command if a semicolon (;) is used, but let s keep things simple. 6

6 Loops Repetition is frequently needed in scripts. bash provides several different kinds of loop constructs, but only two will be described here, which are the most frequently used loops in scripts. 6.1 For Loops A for loop in bash repeats instructions over a sequence of items or WORDS. The term WORDS is used because the items are space separated strings that will be assigned to a variable. The general form of a for loop is for VARIABLE in WORDS do COMMAND1 COMMAND2... done where VARIABLE is assigned each word in WORDS in turn. The commands between do and done will be executed each time in the loop. Like with if the keywords do and done must be on lines of their own. Countdown!/bin/bash Countdown for i in 10 9 8 7 6 5 4 3 2 1 do echo T minus $i done echo "Liftoff!" 6.2 While Loops While loops are used to repeat commands as long as a specific condition is satisfied or holds true. This is similar to repeating a sequence of if statements until the condition fails (has a non-zero exit status). The general form of a while loop is while CONDITION do COMMAND1 COMMAND2... done Like if, CONDITION is true if the exit status of the command is 0. When CONDITION is true, the commands in the loop will be executed. The CONDITION will be executed again, and the loop continues to run until CONDITION has a non-zero exit status. 7

Another Countdown!/bin/bash Another Countdown i=10 while [ $i -gt 0 ] do echo T minus $i i=$(( i - 1 )) done echo "Liftoff!" Beware, it is very easy to create an infinite loop with while loops. Always make sure to do something inside the loop to potentially change the outcome of CONDITION. Sometimes infinite loops are useful, but think carefully about what you really want to do. 7 File Redirection One of the most common uses for shell scripting in research computing is to automate the generation of files. This is particularly handy for generating Condor submission files and DAGMAN input files, which will be covered in later sessions. Redirect Output To make the output of a command go to a file instead of the screen, use the > operator. The following will create a file named hello.out containing the text Hello World. echo Hello World > hello.out If the file already exists, it will be overwritten, so be careful. Redirect Output, Appending To append instead of overwrite the file, use the >> operator. The file will be created if it does not already exist, but will add to the end of an existing file. The following will add the text Goodbye World to the file hello.out. echo Goodbye World >> hello.out Redirect Input Use the < operator to read input from a file instead of typing in on the keyboard. The following will copy the contents of somefile and output them to the file duplicatefile. cat < somefile > duplicatefile A very powerful tool in scripting for creating files is a here document, which is accomplished using the << operator. The contents of the current file become input to the command, up to a line containing the text immediately following the << operator. The real power of this approach is that variable substitution occurs in the text, so it is easy to programmatically create text files in a script. The following will create a file named afile containing the lines Hello World, Goodbye World, and My name is..., where... is the first command line argument of the script. 8

Here Documents!/bin/bash name=$1 cat <<EOF > afile Hello World Goodbye World My name is $name EOF A more powerful example shows how to use this to create a simple Condor submission file. Condor Submission Generation!/bin/bash Create a simple Condor submission file. This doesn t handle complicated quoting or executables outside of the working directory. if [ $ -lt 1 ] then echo "Usage: $0 program argument1 argument2..." exit 1 fi program=$1 shift Remove the first argument Generate the submission file cat <<EOF > $program.submit Universe=vanilla Executable=$program Arguments=$@ Output=$program.out Error=$program.err Log=$program.log Queue EOF echo $program.submit created. 8 Accessing Scripts from Anywhere Accessing Scripts from Anywhere If a script you write is generally useful, you ll want to access it from any directory, not just your working directory. Almost all shells use a special variable $PATH to list the directories where commands our found. On pere.marquette.edu, the bin directory in $HOME (i.e. $HOME/bin) is in the path. So to use your script from anywhere, move it with 9

$ mv scriptname $HOME/bin and then execute with $ scriptname 9 Case Studies Case Study 1: Preparing Data In this case study, we will prepare a script to generate directories and input files for analysis on a Cartesian grid. We specify the maximum x and y coordinates of our study on the command line and generate a simple input file containing x and y. Each input file will be stored in a directory named runn, where N is defined by N(x, y) = maxx y + x The script for this case study will be included after the conclusion of the bootcamp. Case Study 2: Make Project Structures In this case study, we will prepare a script to create a simple project directory structure and a template README.txt file. The directory structure will be $ProjectName bin data doc workspace The script for this case study will be included after the conclusion of the bootcamp. 10