Introduction to Linux Part 2b: basic scripting. Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

Similar documents
Introduction to Linux Part 3: Advanced Scripting and Compiling. Albert Lund CHPC User Services

Introduction to Linux Scripting (Part 2) Brett Milash and Wim Cardoen CHPC User Services

Introduction to Linux Scripting (Part 2) Brett Milash and Wim Cardoen CHPC User Services

Introduction: What is Unix?

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

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

Introduction to Linux Part 1. Anita Orendt and Wim Cardoen Center for High Performance Computing 24 May 2017

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

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

Vi & Shell Scripting

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Chapter 9. Shell and Kernel

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

CENG 334 Computer Networks. Laboratory I Linux Tutorial

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Topic 2: More Shell Skills

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Mills HPC Tutorial Series. Linux Basics II

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

System Programming. Unix Shells

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Introduction to Linux. Roman Cheplyaka

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

EE516: Embedded Software Project 1. Setting Up Environment for Projects

CSE 390a Lecture 5. Intro to shell scripting

Linux Command Line Interface. December 27, 2017

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

Dynamic & Static Libraries in Linux (New Talk!) Wim Cardoen and Brett Milash CHPC User Services

Introduction to Linux for BlueBEAR. January

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

EECS2301. Lab 1 Winter 2016

Linux environment. Graphical interface X-window + window manager. Text interface terminal + shell

Part 1: Basic Commands/U3li3es

Introduction to Linux Workshop 1

Topic 2: More Shell Skills

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

A Brief Introduction to the Linux Shell for Data Science

CSCI2467: Systems Programming Concepts

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

RHCE BOOT CAMP. System Administration

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

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

CSE 391 Lecture 5. Intro to shell scripting

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

Introduction to Shell Scripting

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

B a s h s c r i p t i n g

System Programming. Session 6 Shell Scripting

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

DAVE LIDDAMENT INTRODUCTION TO BASH

Working with Shell Scripting. Daniel Balagué

Linux at the Command Line Don Johnson of BU IS&T

5/20/2007. Touring Essential Programs

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

CSNB113: System Administration - 8 th Topic: Shell Scripts Programming I

Practical 02. Bash & shell scripting

Shells and Shell Programming

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

Shells and Shell Programming

Introduction To. Barry Grant

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

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

Assignment clarifications

Introduction to Linux

Shell Scripting. Jeremy Sanders. October 2011

UNIX shell scripting

bwunicluster Tutorial Access, Data Transfer, Compiling, Modulefiles, Batch Jobs

Introduction of Linux

C Shell Tutorial. Section 1

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Operating Systems. Copyleft 2005, Binnur Kurt

Introduction to Linux Organizing Files

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

Systems Programming/ C and UNIX

The Unix Shell & Shell Scripts

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

Processes and Shells

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

Welcome to Linux. Lecture 1.1

Exercise 1: Basic Tools

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

CS246 Spring14 Programming Paradigm Notes on Linux

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Shells & Shell Programming (Part B)

Introduction to UNIX Part II

More Linux Shell Scripts

Basic Shell Scripting

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

UNIX System Programming Lecture 3: BASH Programming

Unix Introduction to UNIX

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

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

Operating System Interaction via bash

Introduction to Linux

Shell Programming Systems Skills in C and Unix

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

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

Scripting Languages Course 1. Diana Trandabăț

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

Transcription:

Introduction to Linux Part 2b: basic scripting Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

Overview Scripting in Linux What is a script? Why scripting? Scripting languages + syntax Bash/tcsh scripting exercises

What is a script? A script is a collection of linux commands that: are stored in a file the file MUST be executable (with one exception ) commands are separated by: either being a carriage return (new line) or separated by the semi colon ( ; ) executed sequentially until the end of the file has been reached or an error is met

Why scripting? Scripting is a timesaver The real question: When should you script?

Scenarios for scripting Using the batch system at CHPC (discussed in the talk on Slurm Basics) Automating pre- and post- processing of datasets Performing lots of menial, soul draining tasks efficiently and quickly (like building input files)

How long should you script? http://xkcd.com/1205/ Task time saver calculator: http://c.albert-thompson.com/xkcd/

What to script in? Basic scripting needs can be done in the Bash shell or the Tcsh/Csh shell. If you have more complicated tasks to perform, then you should consider something more advanced (like python* or matlab). If your workload is computationally heavy, you should be consider to write your application in a compiled language (e.g. C/C++, Fortran, ). *CHPC also holds a three part workshop focusing on Python

bash vs tcsh/csh A Shell is: a. user interface to the OS s services b. a layer (=> shell) around the kernel c. a programming language CHPC currently supports 2 types of shell-languages /shells: a. B(ourne) Again Shell (bash) b. Csh/Tcsh shell Syntactic differences are significant (and quirky) => NO MIXING ALLOWED Some programs do not support different shells (rather rare) Very easy to switch between shells What shell do I currently use? echo $SHELL WHILE LEARNING TO SCRIPT, PICK ONE AND STICK WITH IT.

Can I change my shell? Yes, you can To change your default shell: go to chpc.utah.edu and login with your U of U credentials. You will be presented with your profile, which will have a link Edit Profile. A new dialogue will show, and you will see an option to change shell. Change it to whatever you want, and save it. Changes will go through in about 15 minutes. (Also can be used to change your email on record, please do this if you change email addresses.)

Getting the exercise files For today s exercises, open a session to one of the cluster interactives (or linuxclass) and run the following commands: # Either: cp ~u0253283/talks/linuxscripting1.tar.gz. # Or: wget https://www.chpc.utah.edu/presentations/linuxscripting1.tar.gz tar -zxvf LinuxScripting1.tar.gz cd LinuxScripting1/ # Pro tip: copy the URL above from our web page!

Write your first script (ex1) Open a file named ex1.sh (Bash) or ex1.csh (Tcsh) using Vi(m) # character: start of a comment Top line always contains the she-bang followed by the lang. interpreter: #!/bin/bash (if you use Bash) or #!/bin/tcsh (if you use Tcsh) Put the following content in a file: echo " My first script:" echo " My userid is:" whoami echo " I am in the directory:" pwd echo "Today's date:" date echo " End of my first script" Make the script executable + execute: chmod u+x./ex1.sh./ex1.sh or chmod u+x./ex1.csh or./ex1.csh

Setting and Using Variables #!/bin/bash #set a variable (no spaces!) VAR="hello bash!" #print the variable echo $VAR #make it permanent export VAR2="string" echo $VAR2 #remove VAR2 unset VAR2 #!/bin/tcsh #set a variable set VAR = "hello tcsh!" #print the variable echo $VAR #make it permanent (no =) setenv VAR2 "string" echo $VAR2 #remove VAR2 unset VAR2 Be careful what you export! Don t overwrite something important!

Script Arguments #!/bin/bash ARG1=$1 ARG2=$2 #ARG3=$3, and so on echo $ARG1 echo $ARG2 #!/bin/tcsh set ARG1 = $1 set ARG2 = $2 #set ARG3 = $3, so on echo $ARG1 echo $ARG2 If the script is named myscript.sh (or myscript.csh ), the script is executed with myscript.sh myarg1 myarg2... myargn $0 : returns the name of the script $#: returns the # arguments

Using grep and wc grep searches files for test strings and outputs lines that contain the string VERY fast, very easy way to parse output can use regex (regular expression) and file patterns use backslash (\) to search for special characters (e.g. to search for "!" use "\!") grep "string" filename wc can count the number of lines in a file wc -l filename

Command line redirection (refresher) You can output to a file using the > operator. cat filename > outputfile You can append to the end of a file using >> cat filename >> outputfile You can redirect to another program with cat filename wc l

Exercise 2 Write a script that takes a file as an argument, searches the file for exclamation points with grep, puts all the lines with exclamation points into a new file, and then counts the number of lines in the file. Use histan-qe.out as your test file. Don t forget #!/bin/bash or #!/bin/tcsh Variables - Bash style: VAR="string" (no spaces!) Tcsh style: set VAR = string Arguments - $1 $2 $3... Grep - grep 'string' filename Counting Lines - wc l filename

Solution to Exercise 2 #!/bin/bash INPUT=$1 grep '\!' $INPUT > outfile wc -l outfile #!/bin/tcsh set INPUT = $1 grep '\!' $INPUT > outfile wc -l outfile The output from your script should have been 34.

Questions? Email issues@chpc.utah.edu