Introduction to 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

Systems Programming & Scripting

Unix Scripts and Job Scheduling

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Lecture 4: Writing shell scripts

HP-UX Essentials and Shell Programming Course Summary

Hands-On UNIX Exercise:

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

Introduction to Shell Programming

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

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

Bash shell programming Part II Control statements

Answers to Even-numbered Exercises

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

SFTP SHELL SCRIPT USER GUIDE

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

Thirty Useful Unix Commands

Beginners Shell Scripting for Batch Jobs

Command Line - Part 1

Linux Shell Script To Monitor Ftp Server Connection

PHP Debugging. Draft: March 19, Christopher Vickery

An Introduction to the Linux Command Shell For Beginners

UNIX, Shell Scripting and Perl Introduction

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

Advanced Bash Scripting. Joshua Malone

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

Tor Exit Node Block Scripts

Hadoop Hands-On Exercises

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

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

LSN 10 Linux Overview

Linux Syslog Messages in IBM Director

A Tiny Queuing System for Blast Servers

Hadoop Hands-On Exercises

USEFUL UNIX COMMANDS

Secure Shell Demon setup under Windows XP / Windows Server 2003

Monitoring a Linux Mail Server

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

Running your first Linux Program

Answers to Even- Numbered Exercises

A UNIX/Linux in a nutshell

Unix Sampler. PEOPLE whoami id who

CLC Server Command Line Tools USER MANUAL

Lecture 22 The Shell and Shell Scripting

CPSC2800: Linux Hands-on Lab #3 Explore Linux file system and file security. Project 3-1

Introduction to Grid Engine

SendMIME Pro Installation & Users Guide

Command Line Crash Course For Unix

Computational Mathematics with Python

Unix the Bare Minimum

This presentation explains how to monitor memory consumption of DataStage processes during run time.

MAX_RMAN_08137_IGNORE=5 DISK_RETENTION_POLICY='RECOVERY WINDOW OF 7 DAYS'

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

The Linux Operating System and Linux-Related Issues

Kleine Linux-Tricks - schneller ohne Aufwand

Open Source Computational Fluid Dynamics

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

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

PMOD Installation on Linux Systems

Fred Hantelmann LINUX. Start-up Guide. A self-contained introduction. With 57 Figures. Springer

Linux System Administration on Red Hat

Computational Mathematics with Python

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

AN INTRODUCTION TO UNIX

Unix Shell Scripting Tutorial Ashley J.S Mills

Training Day : Linux

Automatic Script Generation Based on User-System Interactions

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

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


The Einstein Depot server

SWsoft Plesk 8.3 for Linux/Unix Backup and Restore Utilities

Lecture 4. Regular Expressions grep and sed intro

Computational Mathematics with Python

Creating a Simple Macro

Higher National Unit Specification. General information for centres. Multi User Operating Systems. Unit code: DH3A 34

Introduction to Matlab

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

1 Basic commands. 2 Terminology. CS61B, Fall 2009 Simple UNIX Commands P. N. Hilfinger

Quick Tutorial for Portable Batch System (PBS)

To reduce or not to reduce, that is the question

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

CS10110 Introduction to personal computer equipment

INASP: Effective Network Management Workshops

Setting Up the Site Licenses

Decision Support System to MODEM communications

Learn Perl by Example - Perl Handbook for Beginners - Basics of Perl Scripting Language

IBM Redistribute Big SQL v4.x Storage Paths IBM. Redistribute Big SQL v4.x Storage Paths

Computer Programming In QBasic

Exercise 4 Learning Python language fundamentals

CTIS486 Midterm I 20/11/ Akgül

Programming Exercises

Tutorial 0A Programming on the command line

Basics of STATA. 1 Data les. 2 Loading data into STATA

Mass Deploying Bomgar Software to Macs

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 \ grep "^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 `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. 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. 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. 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. 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. Note the use of the backwards quotes. i. Try typing echo $(( 5 + 9 )) at the command line. ii. What happens if one of the arguments is not a number? iii. You can use parentheses in the usual manner within bash math expressions. 1. 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=`grep -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 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/sh gcounter=0 ccounter=0 tcounter=0 acounter=0 ocounter=0 while read line ; do isfirstline=`echo "$line" grep -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? 6