Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Similar documents
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/20/2007. Touring Essential Programs

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

Assignment clarifications

Useful Unix Commands Cheat Sheet

Shells and Shell Programming

Chapter 9. Shell and Kernel

Introduction: What is Unix?

Basic Linux (Bash) Commands

Linux shell programming for Raspberry Pi Users - 2

Shells and Shell Programming

UNIX System Programming Lecture 3: BASH Programming

EECS2301. Lab 1 Winter 2016

Quick Start Guide. by Burak Himmetoglu. Supercomputing Consultant. Enterprise Technology Services & Center for Scientific Computing

Lab 2: Linux/Unix shell

Last Time. on the website

UNIX Shell Programming

Processes and Shells

Mills HPC Tutorial Series. Linux Basics II

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Quick Start Guide. by Burak Himmetoglu. Supercomputing Consultant. Enterprise Technology Services & Center for Scientific Computing

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

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

COMP 4/6262: Programming UNIX

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

Shells & Shell Programming (Part B)

Vi & Shell Scripting

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

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Introduction to UNIX Part II

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

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

Shell Programming Systems Skills in C and Unix

Introduction to the Shell

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

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

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Bourne Shell Programming Topics Covered

UNIX Essentials Featuring Solaris 10 Op System

CSE 15L Winter Midterm :) Review

Installing and running COMSOL 4.3a on a Linux cluster COMSOL. All rights reserved.

Common File System Commands

A shell can be used in one of two ways:

New User Tutorial. OSU High Performance Computing Center

Review of Fundamentals

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

85321, Systems Administration Chapter 6: The shell

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

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

Bash Programming. Student Workbook

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

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Introduction To. Barry Grant

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Implementation of a simple shell, xssh

Basics. I think that the later is better.

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

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

COMP2100/2500 Lecture 16: Shell Programming I

Essential Linux Shell Commands

CST Algonquin College 2

System Programming. Unix Shells

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

Introduction to Linux/Unix. Xiaoge Wang

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Linux Command Line Interface. December 27, 2017

Bash Reference Manual

Perl and R Scripting for Biologists

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

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

Advanced Linux Commands & Shell Scripting

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

Backgrounding and Task Distribution In Batch Jobs

UNIX. The Very 10 Short Howto for beginners. Soon-Hyung Yook. March 27, Soon-Hyung Yook UNIX March 27, / 29

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

Automating Tasks Using bash

Running LAMMPS on CC servers at IITM

Linux shell scripting Getting started *

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

NBIC TechTrack PBS Tutorial. by Marcel Kempenaar, NBIC Bioinformatics Research Support group, University Medical Center Groningen

Implementation of a simple shell, xssh

PROGRAMMING PROJECT ONE DEVELOPING A SHELL

Part 1: Basic Commands/U3li3es

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

Essentials for Scientific Computing: Bash Shell Scripting Day 3

SANJAY GHODAWAT POLYTECHNIC

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Topic 2: More Shell Skills

bash, part 3 Chris GauthierDickey

Unix basics exercise MBV-INFX410

LOG ON TO LINUX AND LOG OFF

Command-line interpreters

A Hands-On Tutorial: RNA Sequencing Using High-Performance Computing

The Online Unix Manual

Transcription:

AICT High Performance Computing Workshop With Applications to HPC Edmund Sumbar research.support@ualberta.ca Copyright 2007 University of Alberta. All rights reserved High performance computing environment (1) batch job head node execution nodes May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 2 create delete Copyright 2007 University of Alberta. All rights reserved. 1

High performance computing environment (2) Portable Batch System (PBS) Batch job described by a PBS script PBS script is a shell script Therefore, a batch job is a shell script Also, shell scripts for utility purposes May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 3 create delete What is a shell (1) ` Internet desktop head node May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 4 create delete Copyright 2007 University of Alberta. All rights reserved. 2

What is a shell (2) ssh client (on desktop) connects to ssh server (on head node) User authentication (login) A shell program is launched on head node (login shell) Local ssh client displays the results from the shell Allows you to execute commands interactively on the head node May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 5 create delete Two major shell program variants Bourne-like shells sh ksh bash C-like shells csh tcsh Login shell specified for each user account Look for your CCID in /etc/passwd echo $SHELL May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 6 create delete Copyright 2007 University of Alberta. All rights reserved. 3

Very little difference for basic interactive use Non-interactive use Execute a sequence of commands contained in a file (script) More advanced usage Differences in shell syntax become important Focus on the bash shell Reference documentation man bash info bash May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 7 create delete Example commands (* internal command) cd* read* cp chmod mv awk rm ps mkdir kill* ls sleep cat echo* diff exit* find qsub grep qstat May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 8 create delete Copyright 2007 University of Alberta. All rights reserved. 4

Types of commands Simple ls ~/src > filelist test -e mhd.c Pipeline (sequence of one or more simple commands) cat mhd.c grep MPI grep -v Bcast List (sequence of one or more pipelines) cd ~/src ; test -e mhd.c && cp mhd.c mhd2.c & Compound commands (act on lists) if! grep MPI mhd.c ; then echo serial code ; fi for f in * ; do test L $f && rm $f ; done May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 9 create delete Three ways to execute a script If script syntax is appropriate for current shell, set executable bit on the file (chmod), and run script file directly Run shell program with script file as argument (bash scriptfile) Specify the path to the shell program on the first line, set the executable bit, and execute the script directly (#!/bin/bash same effect as /bin/bash scriptfile) May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 10 create delete Copyright 2007 University of Alberta. All rights reserved. 5

Workshop exercises (1) Execute the following sequence of simple commands in a shell script using each of the three techniques. date uname n id May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 11 create delete Exit status Return value of simple command last command in pipeline last command executed in list last command executed by compound command last command executed by a script echo $? 0 success 1..128 failure 128+n termination due to signal May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 12 create delete Copyright 2007 University of Alberta. All rights reserved. 6

Workshop exercises (2) What s the difference between a command that crashes and a command that fails? Compare the exit status of these commands ls l. ls l zzz /home/esumbar/crasher May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 13 create delete Execution modes Synchronous Command terminated by semicolon (;) or newline Shell waits for command to terminate Shell returns with command s exit status Asynchronous Command terminated by ampersand (&) Shell does not wait for command to finish Command runs in the background Shell returns with an exit status of zero (not the exit status of background command) May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 14 create delete Copyright 2007 University of Alberta. All rights reserved. 7

Special characters and words Metacharacters (delimit words) & ; ( ) < > space tab File name pattern matching characters *? [ Parameter expansion character $ Quoting characters \ Tilde character ~ Reserved words that have a special meaning! case do done elif else esac fi for function if in select then until while { } time [[ ]] May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 15 create delete Parameters and parameter expansion Named parameters (variables) remotehost=num.srv.ualberta.ca ping c 1 ${remotehost} > /dev/null Positional parameters (arguments passed to script) Assigned automatically echo ${1} ${15} Special parameters and shell variables Assigned automatically echo ${?} cd ${HOME} May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 16 create delete Copyright 2007 University of Alberta. All rights reserved. 8

Shell variables (environment variables) (1) All uppercase names (by convention) Used to define attributes PATH search path for commands HOME home directory PWD current working directory Inherited by subcommands Add variable to environment EMAIL_ADDRESS=research.support@ualberta.ca export EMAIL_ADDRESS Display with printenv or env May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 17 create delete Shell variables (environment variables) (2) PBS augments a script s environment PBS_O_WORKDIR original working directory cd $PBS_O_WORKDIR PBS_NODEFILE name of file containing the list of exec nodes assigned to the job mpirun -machinefile $PBS_NODEFILE -np $NP./a.out PBS_JOBID useful for uniquely naming output files./a.out > output.$pbs_jobid Pass additional environment variables using -v or -V options qsub v BASE=linear,TARGET=3600 scriptfile May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 18 create delete Copyright 2007 University of Alberta. All rights reserved. 9

Command substitution Standard output of command used as a string Old style (still valid) echo current date and time is `date` New style for file in $(ls $HOME/core*) do rm f $file done May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 19 create delete Workshop exercises (3) Assign the output from a command to a parameter nfiles=$(ls a wc -l) Recall the value of the parameter later echo number of files is $nfiles Try to recall the value of nfiles from a script May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 20 create delete Copyright 2007 University of Alberta. All rights reserved. 10

Shell arithmetic x=45 Internal command let x = x * 5 Compound command (( x = x * 5 )) Expansion x=$(( x * 5 )) echo $x 225 May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 21 create delete Comments used in scripts Introduced by # Everything to the end of the line is ignored PBS directives (#PBS nodes=2) are shell script comments qsub scans the script for PBS directives up to the first shell command that it encounters to determine job specs Any command line args to qsub override PBS script directives May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 22 create delete Copyright 2007 University of Alberta. All rights reserved. 11

Quoting Full (using single quotes) all special characters and words lose their special meaning prompt=greetings echo $prompt: $(date) Partial (using double quotes) only $ and \ retain their special meaning args= file1 file2 test $args = file1 file2 && rm -f $args Escaping (using backslash) remove special meaning of single character echo value of \$prompt: $prompt May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 23 create delete I/O redirection (1) Three open files/file descriptors Standard input (default stdin from keyboard) 0 Stardard output (default stdout to terminal) 1 Stardard error output (default stderr to terminal) 2 Examples 0 < inputfile 1 > outputfile >> outputfile 2 > /dev/null &> outputfile >outputfile 2>&1 May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 24 create delete Copyright 2007 University of Alberta. All rights reserved. 12

I/O redirection (2) PBS redirects stdout and stderr from the job script to files located on the execution node At the end of the job, by default, PBS copies these files from the execution node to the original working directory as <job name>.o<job id> and <job name>.e<job id> Use the -o and/or -e qsub options to specify alternate files May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 25 create delete Workshop exercises (4) Explain the output from this shell command echo 2 * 3 > 5 is a valid inequality May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 26 create delete Copyright 2007 University of Alberta. All rights reserved. 13

Functions Group command (a type of compound command) test -e $resultsfile && { mkdir $resultsdir mv $resultsfile $resultsdir echo moved $resultsfile to $resultsdir } A function is a named group command function store { mkdir $resultsdir mv $resultsfile $resultsdir echo moved $resultsfile to $resultsdir } test e $resultsfile && store May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 27 create delete Branching (1) if-command branches on the basis of exit status if [ $denominator -ne 0 ] then value=$(( $numerator/$denominator )) else echo divide-by-zero condition fi [ $denominator ne 0 ] test $denominator -ne 0 May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 28 create delete Copyright 2007 University of Alberta. All rights reserved. 14

Branching (2) case-command branches on the basis of pattern matches case $(uname -p) in mips ) echo SGI machine ;; powerpc ) echo IBM machine ;; *86 ) echo Intel machine ;; * ) echo unknown processor ;; esac May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 29 create delete Looping (1) for-command loops over items in a word or token (a sequence of characters considered as a single unit) Environment variable IFS defines the characters used to split the word into items (by default <space> <tab> <newline>) for file in $(ls $PBS_O_WORKDIR/temp) do rm -f $file done May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 30 create delete Copyright 2007 University of Alberta. All rights reserved. 15

Looping (2) while-command loops on the basis of exit status while : do./a.out test -e output && break done May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 31 create delete Workshop exercises (5) Say that you ve parameterized your PBS script to accept three environment variables. You plan to use the -v option to specify the values of these variables when you submit your jobs, and you d like to take the parameter values from a file. Create a utility bash-shell script that formats an appropriate qsub command line for each line of the parameter file and saves all the command lines in a file that can be later executed as a script to submit the jobs. May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 32 create delete Copyright 2007 University of Alberta. All rights reserved. 16

Workshop exercises (5) PBS script #!/bin/bash #PBS l walltime=02:00:00 cd $PBS_O_WORKDIR./a.out $varx $vary $varz > output.$pbs_jobname Parameter file blue A 45 blue A 89 green K 35 yellow Q 51 Result file qsub v varx=blue,vary=a,varz=45 script.pbs qsub v varx=blue,vary=a,varz=89 script.pbs... May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 33 create delete Workshop exercises (5) #!/bin/bash src=$1 #---parameter file pbs=$2 #---pbs script file res=$3 #---result file exec 0< $src while read x y z; do echo qsub v varx=$x,vary=$y,varz=$z $pbs >> $res done./format.sh param.file pbs.script result.file May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 34 create delete Copyright 2007 University of Alberta. All rights reserved. 17

Workshop exercises (5) #!/bin/bash src=$1 #---parameter file pbs=$2 #---pbs script file res=$3 #---result file test -e x$src exit test -e x$res && rm f $res exec 0< $src while read x y z; do test -n $x continue test -n ${x##\#*} continue echo qsub N $x$y$z v varx=$x,vary=$y,varz=$z $pbs >> $res done May 4, 2007 Copyright 2007 University of Alberta. All rights reserved 35 create delete Copyright 2007 University of Alberta. All rights reserved. 18