Introduction to Shell Scripting

Similar documents
Shells & Shell Programming (Part B)

Shell programming. Introduction to Operating Systems

9.2 Linux Essentials Exam Objectives

Basic Linux (Bash) Commands

COMP 4/6262: Programming UNIX

A shell can be used in one of two ways:

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shell Programming (bash)

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

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

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Useful Unix Commands Cheat Sheet

Bash scripting basics

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

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

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

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Linux Bash Shell Scripting

Computer Systems and Architecture

CS Unix Tools & Scripting

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

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shells and Shell Programming

Chapter 4. Unix Tutorial. Unix Shell

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

UNIX shell scripting

Shells and Shell Programming

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

5/8/2012. Exploring Utilities Chapter 5

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

RHCE BOOT CAMP. System Administration

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Computer Systems and Architecture

CSC 2500: Unix Lab Fall 2016

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Bourne Shell Reference

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

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Essential Skills for Bioinformatics: Unix/Linux

Last Time. on the website

Assignment clarifications

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Vi & Shell Scripting

A Brief Introduction to the Linux Shell for Data Science

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

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

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

Програмиранев UNIX среда

DAVE LIDDAMENT INTRODUCTION TO BASH

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

Linux shell programming for Raspberry Pi Users - 2

Exploring UNIX: Session 3

More Linux Shell Scripts

Shell Control Structures

LING 408/508: Computational Techniques for Linguists. Lecture 5

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

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

Lab #12: Shell Scripting

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

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

Conditional Control Structures. Dr.T.Logeswari

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

Shell Control Structures

Lab 4: Shell scripting

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

Practical 02. Bash & shell scripting

UNIX files searching, and other interrogation techniques

Shell Programming an Introduction

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Simple Shell Scripting for Scientists

There are some string operators that can be used in the test statement to perform string comparison.

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

Introduction To. Barry Grant

PROGRAMMAZIONE I A.A. 2015/2016

Shell Scripting. Jeremy Sanders. October 2011

UNIX System Programming Lecture 3: BASH Programming

Shell Start-up and Configuration Files

bash Execution Control COMP2101 Winter 2019

Lecture 02 The Shell and Shell Scripting

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

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

LOG ON TO LINUX AND LOG OFF

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Processes and Shells

BGGN 213 Working with UNIX Barry Grant

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

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

Lab 4: Bash Scripting

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

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

CENG 334 Computer Networks. Laboratory I Linux Tutorial

COMS 6100 Class Notes 3

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

Linux Shell Script. J. K. Mandal

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

CS Unix Tools & Scripting

Transcription:

Introduction to Shell Scripting Lecture 1. Shell scripts are small programs. They let you automate multi-step processes, and give you the capability to use decision-making logic and repetitive loops. 2. Most UNIX scripts are written in some variant of the Bourne shell. Older scripts may use /bin/sh (the classic Bourne shell). We use bash here. 3. Consider this sample script (you ve seen it before in the PATH example). It can be found at ~unixinst/bin/progress.! /bin/bash Sample shell script for Introduction to UNIX class. Jason R. Banfelder. Displays basic system information and UNIX students' disk usage. Show basic system information echo `hostname`: `w head -1` echo `who cut -d" " -f1 sort uniq \ egrep "^unixst" wc -l` students are logged in. Generate a disk usage report. echo "-----------------" echo "Disk Usage Report" echo "-----------------" cd /home Loop over each student's home directory... for STUDENT_ID in `/bin/ls -1d unixst*` do...and show how much disk space is used. du -skl /home/$student_id done a. All scripts should begin with a shebang (!) to give the name of the shell. b. Comments begin with a hash. c. You can use all of the UNIX commands you know in your scripts. d. The results of commands in backwards quotes (upper-left of your keyboard) are substituted into commands. e. Use a $ before variable names to use the value of that variable. f. Note the for loop construction. See the Introduction to UNIX manual for details on the syntax. 4. Scripts have to be executed, so you need to chmod the script file. Use ls l to see the file modes. J. Banfelder, L. Skrabanek, Weill Cornell Medical College, 2017. 1

Exercise 1. Write a script to print out the quotations from each directory. a. Did you write the script from scratch, or copy and modify the example above? 2. Create a subdirectory called bin in your home directory, if you don t already have one. Move your script there. 3. Permanently add your bin directory to your PATH. a. It is a UNIX tradition to put your useful scripts and programs into a directory named bin. 4. Save your script s output to a file, and e-mail the file to yourself. a. mail myself@my.college.edu < AllMyQuotations.txt b. We hope you enjoy this list of quotations as a souvenir of this class. J. Banfelder, L. Skrabanek, Weill Cornell Medical College, 2017. 2

More Scripting Techniques Lecture 1. As you write scripts, you will find you want to check for certain conditions before you do things. For example, in the script from the previous exercise, you don t want to print out the contents of a file unless you have permission to read it. Checking this will prevent warning messages from being generated by your scripts. a. The following script fragment checks the readability of a file. Note that this is a script fragment, not a complete script. It won t work by itself (why not?), but you should be able to incorporate the idea into your own scripts. if [ -r $STUDENT_ID/quotation ]; then echo cat $STUDENT_ID/quotation fi b. Note the use of the if fi construct. See the Introduction to UNIX manual that accompanies this class for more details, including using else blocks. i. In particular, note that you must have spaces next to the brackets in the test expression. c. Note how the then command is combined on the same line as the if statement by using the ; operator. d. You can learn about many other testing options (like r) by reading the results of the man test command. 2. The read command is useful for reading input (either from a file or from an interactive user at the terminal) and assigning the results to variables.! /bin/bash A simple start at psychiatry. (author to remain nameless) echo "Hello there." echo "What is your name?" read PATIENT_NAME echo "Please have a seat, ${PATIENT_NAME}." echo "What is troubling you?" read PATIENT_PROBLEM echo -n "Hmmmmmm... '" echo -n $PATIENT_PROBLEM echo "' That is interesting... Tell me more..." a. Note how the variable name is in braces. Use braces when the end of the variable name may be ambiguous. J. Banfelder, L. Skrabanek, Weill Cornell Medical College, 2017. 3

3. The read command can also be used in a loop to read one line at a time from a file. while read line; do echo $line <your script code here> done < input.txt a. You can also use [] tests as the condition for the loop to continue or terminate in while commands. b. Also see the until command for a similar loop construct, 4. You can also use arguments from the command line as variables. while read line; do echo $line <more script code here> done < $1 a. $1 is the first argument after the command, $2 is the second, etc. Exercise 1. Modify your quotation printing script to test the readability of files before trying to print them. J. Banfelder, L. Skrabanek, Weill Cornell Medical College, 2017. 4

Scripting Expressions Lecture 1. You can do basic integer arithmetic in your scripts. a. total=$(( $1 + $2 + $3 + $4 + $5 )) will add the first five numerical arguments to the script you are running, and assign them to the variable named total. b. Try typing echo $(( 5 + 9 )) at the command line. c. What happens if one of the arguments is not a number? d. You can use parentheses in the usual manner for grouping within bash math expressions. i. gccontentmin= $(( ( $gcmin * 100 ) / $total )) Exercise 1. When we learned about csplit, we saw that we had to know how many sequences were in a.fasta file to properly construct the command. a. Write a script to do this work for you.! /bin/bash Intelligently split a fasta file containing multiple sequences into multiple files each containing one sequence. seqcount=`egrep -c '^>' $1` echo "$seqcount sequences found." if [ $seqcount -le 1 ]; then echo "No split needed." exit elif [ $seqcount -eq 2 ]; then csplit -skf seq $1 '%^>%' '/^>/' else repcount=$(( $seqcount - 2 )) csplit -skf seq $1 '%^>%' '/^>/' \{${repcount}\} fi J. Banfelder, L. Skrabanek, Weill Cornell Medical College, 2017. 5

b. Expand this script to rename each of the resultant files to reflect the sequence s GenBank ID. >gi 37811772 gb AAQ93082.1 taste receptor T2R5 [Mus musculus] This is shown underlined and in italics in the example above. i. How would you handle fasta headers without a GenBank ID? c. Expand your script to sort the sequence files into two directories, one for nucleotide sequences (which contain primarily A, T, C, G), and one for amino acid sequences. i. How would you handle situations where the directories do/don t already exist? ii. How would you handle situations where the directory name already exists as a file? iii. When does all this checking end??? 2. What does this script do? (hint: man expr)! /bin/bash gcounter=0 ccounter=0 tcounter=0 acounter=0 ocounter=0 while read line ; do isfirstline=`echo "$line" egrep -c '^>'` if [ $isfirstline -ne 1 ]; then linelength=`echo "$line" wc -c` until [ $linelength -eq 1 ]; do base=`expr substr "$line" 1 1` case $base in "a" "A") acounter=`expr $acounter + 1` "c" "C") ccounter=`expr $ccounter + 1` "g" "G") gcounter=`expr $gcounter + 1` "t" "T") tcounter=`expr $tcounter + 1` *) ocounter=`expr $ocounter + 1` esac line=`echo "$line" sed 's/^.//'` linelength=`echo "$line" wc -c ` done fi done < $1 echo $gcounter $ccounter $tcounter $acounter $ocounter 3. Write a script to report the fraction of GC content in a given sequence. a. How can you use the output of the above script to help you in this? J. Banfelder, L. Skrabanek, Weill Cornell Medical College, 2017. 6