Beginners Shell Scripting for Batch Jobs

Similar documents
UNIX, Shell Scripting and Perl Introduction

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

Command Line Crash Course For Unix

Unix Sampler. PEOPLE whoami id who

Unix the Bare Minimum

Introduction to CloudScript

HP-UX Essentials and Shell Programming Course Summary

Unix Scripts and Job Scheduling

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

An Introduction to the Linux Command Shell For Beginners

The Linux Operating System

LSN 10 Linux Overview

Command Line - Part 1

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

A Crash Course on UNIX

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

CS Unix Tools & Scripting Lecture 9 Shell Scripting

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

Lab 2 : Basic File Server. Introduction

Setting up PostgreSQL

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.

Installing IBM Websphere Application Server 7 and 8 on OS4 Enterprise Linux

Creating a Java application using Perfect Developer and the Java Develo...

Introduction to Running Hadoop on the High Performance Clusters at the Center for Computational Research

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

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

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

MFCF Grad Session 2015

SFTP SHELL SCRIPT USER GUIDE

Bash Guide for Beginners

Introduction. Created by Richard Bell 10/29/2014

Running your first Linux Program

Using Parallel Computing to Run Multiple Jobs

Introduction to Shell Scripting

LAE Enterprise Server Installation Guide

Hands-On UNIX Exercise:

Thirty Useful Unix Commands

Introduction to the UNIX Operating System and Open Windows Desktop Environment

RHadoop and MapR. Accessing Enterprise- Grade Hadoop from R. Version 2.0 (14.March.2014)

Introduction to Linux and Cluster Basics for the CCR General Computing Cluster

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

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

CS 103 Lab Linux and Virtual Machines

The Linux Operating System and Linux-Related Issues

Notes on the SNOW/Rmpi R packages with OpenMPI and Sun Grid Engine

Tutorial 0A Programming on the command line

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

A UNIX/Linux in a nutshell

Automatic Script Generation Based on User-System Interactions

L01 Introduction to the Unix OS

HPC Wales Skills Academy Course Catalogue 2015

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

Installing and running COMSOL on a Linux cluster

An Introduction to High Performance Computing in the Department

SendMIME Pro Installation & Users Guide

KretchmarBook 2003/8/29 19:05 page 39 #51

PKZIP 6.0 Command Line Getting Started Manual

The Build Process. of (GNU Tools for ARM Embedded Processors)

CS10110 Introduction to personal computer equipment

Job Scheduler Daemon Configuration Guide

Lecture 4: Writing shell scripts

Setting Up a CLucene and PostgreSQL Federation

Stata R Release 13 Installation Guide

Introduction to Operating Systems

CS 2112 Lab: Version Control

A DirectX 9 capable graphics card is recommended. A DirectX 10 capable graphics card from ATI or nvidia is required for Volume Rendering.

Linux für bwgrid. Sabine Richling, Heinz Kredel. Universitätsrechenzentrum Heidelberg Rechenzentrum Universität Mannheim. 27.

Lab 1: Introduction to the network lab

Introduction to Scientific Computing

24/08/2004. Introductory User Guide

Programming for GCSE Topic H: Operating Systems

PHP Debugging. Draft: March 19, Christopher Vickery

Automated Offsite Backup with rdiff-backup

Linux Overview. The Senator Patrick Leahy Center for Digital Investigation. Champlain College. Written by: Josh Lowery

Linux System Administration on Red Hat

Introduction to Running Computations on the High Performance Clusters at the Center for Computational Research

A Brief Introduction to the Use of Shell Variables

Basic Linux & Package Management. Original slides from GTFO Security

Installing HSPICE on UNIX, Linux or Windows Platforms

TEL2821/IS2150: INTRODUCTION TO SECURITY Lab: Operating Systems and Access Control

Unit 10 : An Introduction to Linux OS

Lecture 22 The Shell and Shell Scripting

Koha 3 Library Management System

Hadoop Hands-On Exercises

Installing Java. Table of contents

Apache 2.0 Installation Guide

Distributed Version Control

USEFUL UNIX COMMANDS

Systems Programming & Scripting

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

INF-110. GPFS Installation

SAS 9.4 In-Database Products

Linux Overview. Local facilities. Linux commands. The vi (gvim) editor

Getting Started with Amazon EC2 Management in Eclipse

Hodor and Bran - Job Scheduling and PBS Scripts

GRID Computing: CAS Style

Fundamentals of UNIX Lab Networking Commands (Estimated time: 45 min.)

Transcription:

Beginners Shell Scripting for Batch Jobs Evan Bollig and Geoffrey Womeldorff

Before we begin... Everyone please visit this page for example scripts and grab a crib sheet from the front http://www.scs.fsu.edu/~bollig/techseries Also please sign in on the clipboard being passed around

Introduction to Shells History What is the *nix ENV? What is Shell Scripting? Novice Scripting Outline HelloWorld (HelloWorld.sh) Variables (HelloWorld2.sh) `...`, '...' and... (Example3.sh)

Outline (2) Intermediate Scripting File and Output Control (Example4.sh) Loops, Conditionals and Utilizing Available Programs (Example5.sh) Advanced Scripting Substrings and Case Statements for Machine and Architecture Specific Tasks (Example6.sh) Restart Files, Integers and Avoiding Redundant Tasks (Example7.sh) Calling Matlab for Batch Processing (Example8.sh)

Introduction to Shells

What are *nix Shells? Unix was the first OS to separate the command interpreter (shell) from the operating system (kernel) Shells abstract the complexities of systems and provide standard commands for all machines A shell has built in commands, plus allows us access to other Unix commands history, export, cd > built in cp, ls, grep > Unix commands (/bin, /usr/bin, etc.) http://www.phys.ualberta.ca/~gingrich/research/shells/shells.html

A General History Bourne Shell (/bin/sh): Bell Laboratories, by Stephen Bourne Basis for all shells, but provided limited functionality C Shell (/bin/csh): UC Berkley, by Bill Joy (creator of vi ) Expanded Bourne Shell to mimic C language and provide interactivity (in reality, it was too buggy to use for scripts) TENEX csh (/bin/tcsh) was later developed to get rid of bugs but Unix manufacturers stuck with csh http://www.softlab.ntua.gr/facilities/documentation/unix/shelldiff.html#3

Korn Shell (/bin/ksh): History (2) AT&T, by David Korn Hybrid of sh and csh using sh language with csh interactivity Although it was the best of both worlds, it was still from AT&T => ksh was not free. Bourne Again Shell (/bin/bash): GNU's POSIX compatible Bourne Shell Equivalent to Korn but free implementation Perfect fit in Linux which resulted in mass popularity rc, zsh and others are still emerging...

Understanding the ENV The ENV is the Unix environment Key=value pairs describe current configuration Every running application, user, etc that interacts with the operating system has an environment A process inherits its parent's environment unless the parent overrides/specifies other properties. Users' initial ENV is provided by the shell and based on /etc/profile.d/*, /etc/*shrc and ~/.*shrc files. Changes to child's environment only affect child's children and are lost when the subtree exits.

Controlling the Env Shells allow us to control the environment with built in commands; Unix commands can also be used: %> env Lists your current environment %> export <key>= <value> Set a new key or update an existing one NOTE: this is equivalent to setenv <key> <value> in the C shell family %> echo $<key> Print current value of variable

Understanding Shell Scripts Shell scripts are files that contain commands as you would run them on the command prompt. Scripts commonly use variables, conditionals and loops. These are all available on the command prompt What makes a script so special? It has the executable bit set (chmod +x <script>) It executes until it encounters an error (interpreted) Scripts can use any available executables; no need to worry about libraries, headers or APIs

Quick and dirty solutions Why Script? Mostly you use pre existing binaries Changes to script have immediate effects no need to write/(re )compile code Inter program communication is performed through standard file I/O pipes No worries about communication standards Shell Scripts use shells that are standard on all *nix platforms NOTE: only the shells are standard. This is not always true for the commands a script contains.

Novice Shell Scripting

HelloWorld Example 1: HelloWorld.sh Key features: Sh bang (#!/bin/sh) Every shell script must start with a sh bang on the first line. This tells the executing shell what type of shell should interpret it (i.e. sh, bash, csh, perl, python, tcl, etc.) Comments Comments begin with the hash (#) and proceed to the end of the line

Variables Example2: HelloWorld2.sh; Hello.sh Key Features: Local and Global Variables <key>=<value> is a local variable that is not passed to children export <key>=<value> is a global variable that is passed to children but is not visible to parent of this script

`...`, '...' and... Example3: Example3.csh Key Features: Use of different shell (see csh in sh bang) `...`: execute program performing substitution on variables '...': strings without variable substitution... : strings with variable substitution

Intermediate Shell Scripting

File and Output Control Example4: Example4.sh; HandsFreeCharmm.sh Key Features: cat << EOF... Print everything between << EOF and the next occurrence of EOF to stdout. NOTE: the > example4.out to the right of << EOF redirects stdout to a file Redirecting both stdout and stderr Use ( [command] > [stdoutfile] ) > [stderrfile] to store output separately Use [command] &> [outfile] to store together

Loops, Conditionals and Utilizing Available Programs Example5: Example5.sh Key Features: For loop executes on list of filenames Conditional (if then else) tests for a lock file to avoid extra work We take advantage of standard Unix commands like mkdir, pwd, grep and echo but also use non standard Imagemagick to perform batch image conversion

Advanced Shell Scripting

Substrings and Case Statements for Machine and Architecture Specific Tasks Example6: Example6.sh Key Features: Case statement to compare machine's name (hostname) Case statement to compare architecture of machine Substrings from variables using ${var_name#*/} and ${var_name%/*}

Restart Files, Integers and Avoiding Redundant Tasks Example7: Example7.sh Key Features: Restart file informs script what tasks it has already completed Integers require unique handling (we cannot compare like strings) Locates ALL jpegs in ALL subdirectories and rebuilds subdirs in output folder (important to keep batch jobs organized)

Calling Matlab as for Batch Processing Example8: Example8.sh; testmatlab.m; mandrill_cvt.m Key Features: Per machine tasks different compression for each cluster: Class*, Prism*, Hallway*, and Vislab machines Executes Matlab behind the scenes with a complex clustering algorithm for data compression (easily coded in Matlab and easy to visualize results)

Conclusion You now have example shell scripts to get started. Use the resources on the crib sheet and on the SCS TechSupport Twiki to help you write your own scripts. Use logging, locks and restart files to help accelerate batch jobs. Remember: don't reinvent the wheel! If a command exists and it functions correctly, don't waste your time re writing it from scratch. If it has bugs then its another story...

Special Thanks... to TSG for the excuse to play with shell scripts to Gordon Erlebacher for the extra time allotment away from WATT to SCS Custodial for making life interesting during the wee hours of the morning And of course! Thank you all for attending...