Shell Scripting. 2014/10/09 Victor Eijkhout

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

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

Hands-On UNIX Exercise:

Unix Scripts and Job Scheduling

Advanced Bash Scripting. Joshua Malone

A Crash Course on UNIX

HP-UX Essentials and Shell Programming Course Summary

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.

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

Command Line Crash Course For Unix

Bash shell programming Part II Control statements

CS2720 Practical Software Development

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

Introduction to Shell Scripting

CS Unix Tools & Scripting Lecture 9 Shell Scripting

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

Linux Shell Script To Monitor Ftp Server Connection

Beginners Shell Scripting for Batch Jobs

Answers to Even- Numbered Exercises

Unix the Bare Minimum

Introduction to Shell Programming

AN INTRODUCTION TO UNIX

UNIX, Shell Scripting and Perl Introduction

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

Lecture 4: Writing shell scripts

Answers to Even-numbered Exercises

Systems Programming & Scripting

Command Line - Part 1

Unix Sampler. PEOPLE whoami id who

Using SVN to Manage Source RTL

SFTP SHELL SCRIPT USER GUIDE

An Introduction to the Linux Command Shell For Beginners

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

Unix Shell Scripting Tutorial Ashley J.S Mills

Secure Shell Demon setup under Windows XP / Windows Server 2003

Thirty Useful Unix Commands

A UNIX/Linux in a nutshell

A Brief Introduction to the Use of Shell Variables

Installing and running COMSOL on a Linux cluster

PYTHON Basics

Hadoop Hands-On Exercises

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

Linux Syslog Messages in IBM Director

Hadoop Hands-On Exercises

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

New Lab Intro to KDE Terminal Konsole

Microsoft Windows PowerShell v2 For Administrators

Useless Use of * Slide 1. Useless Use of * Jan Schaumann jschauma@netmeister.org PGP: 136D 027F DC B42 47D6 7C5B 64AF AF22 6A4C

Setting up PostgreSQL

Bash Guide for Beginners

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

PHP Debugging. Draft: March 19, Christopher Vickery

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

AmbrosiaMQ-MuleSource ESB Integration

Setting Up Database Security with Access 97

6. Control Structures

Oracle Insurance Policy Administration

The latest update can be found on the Bruker FTP servers:

Windows PowerShell Essentials

CS10110 Introduction to personal computer equipment

Lecture 22 The Shell and Shell Scripting

ICS 351: Today's plan

High Availability for Informatica Data Replication in a Cluster Environment

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

Introduction to the Oracle DBMS

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

L01 Introduction to the Unix OS

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

Command-Line Operations : The Shell. Don't fear the command line...

LSN 10 Linux Overview

Introduction to Operating Systems

VDCF - Virtual Datacenter Control Framework for the Solaris TM Operating System

PL / SQL Basics. Chapter 3

Networks. Inter-process Communication. Pipes. Inter-process Communication

Using Parallel Computing to Run Multiple Jobs

L04 C Shell Scripting - Part 2

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

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Moving from CS 61A Scheme to CS 61B Java

Grid 101. Grid 101. Josh Hegie.

Introduction to Python

C++ Input/Output: Streams

CPSC 226 Lab Nine Fall 2015

A Tiny Queuing System for Blast Servers

XSLT File Types and Their Advantages

The Linux Operating System and Linux-Related Issues

Tutorial 0A Programming on the command line

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

BASH scripting. Sebastian von Alfthan Scripting techniques CSC Tieteen tietotekniikan keskus Oy CSC IT Center for Science Ltd.

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

INASP: Effective Network Management Workshops

PHP Tutorial From beginner to master

PIC 10A. Lecture 7: Graphics II and intro to the if statement

PMOD Installation on Linux Systems

Basics of I/O Streams and File I/O

Kleine Linux-Tricks - schneller ohne Aufwand

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

Writing Shell Scripts in UNIX. A simple Bash-shell script. Comments and Commands

Exercise 1: Python Language Basics

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Transcription:

Shell Scripting 2014/10/09 Victor Eijkhout

What is a shell? Command interpreter: listens for your commands, executes, shows output Toolbox: chain together commands with pipes Programming language: Variables Control structures (not so much data structures) Interactive use or through programs

The bash shell sh: one of the original shells (the other is csh ), written by Stephen Bourne in 1977 bash: Bourne Again Shell, reimplementation and extension We prefer bash: better for shell programming

Variables Setting a variable: title= My System Information! Use: echo $title!

Exercise 1 $ a = "the title" $ a= "the title" $ a="the title" $ echo a $ echo $a Explore the various ways to go wrong when you declare and use variables

Variables Naming rules: Must start with a letter Must not contain embedded spaces Use underscores Must not be a punctuation mark Must not be an existing bash reserved word To see a list of reserved words, use the help command

Variables Some variables are predefined $HOME,$PATH! Typical use: my_project_dir=$home/projects/main! (It would be nice if you didn t have to do this every time you logged in)

Exercise 2 $ thevar="the value $ bash bash-3.2$ echo $thevar # what is the output? bash-3.2$ exit $ export thevar="the value" $ bash bash-3.2$ echo $thevar # what is the output Two ways of setting a variable

Exercise 3 $ a="foo.bar" $ echo ${a%%.bar} $ echo ${a##foo.} Funky stuff to do with variables $ a=$( whoami ) $ echo $a $ a=1 $ b=$(( a+2 )) $ echo $b

Exercise 4: Startup files Create a file.bashrc in your home directory, and add some lines export my_project_dir=$home/projects/main! Also create.profile with this content: if [ -f ~/.bashrc ]; then source ~/.bashrc fi! Log out, log back in (or just open a new terminal window)

Shell scripts In exercise 20 you made a shell script Call a script: explicit path: ~/bin/work.cmd in your current directory:./work.cmd! Better: echo $PATH and put a line in your.profile: export PATH=$PATH:$HOME/bin!

Shell script programming In a shell script, some variables are predefined: $# : the number of arguments $1, $2, : the arguments

Exercise 5 $ cat > countum #!/bin/bash Finish the script and execute it echo "There are arguments"

Flow Control Bash provides several commands to control the flow of execution if exit for while until case break continue

if # First form! if condition ; then! fi!!!! commands! # Second form! if condition ; then! commands! else! fi!! commands! # Third form! if condition ; then! commands! elif condition ; then! fi! commands!

if: usual form Square brackets are shorthand for test (important: spaces inside the brackets!) if [ sometest]; then! Numerical tests: if [ $a gt 2 ]! File tests: if [ -f.profile ]! Man page: man test!

Exercise 6 $ cat > namum #!/bin/bash echo "There are arguments" Write a script that echos the first argument, if there is at least one argument if [... ] ; then echo "The first argument is " fi

Exercise 7 $ cat > testum #!/bin/bash Write a script that tests if a file exists if [ ] ; then echo "You need to provide an arg" fi if [... ] ; then echo "Your file exists!" else echo "Your file does not exist!" fi

Loops - for!! for variable in words; do! commands! done!

Exercise 8 Let s do it all on one line: substitute the name of an existing file in this command for f in * ; do if [ $f = name" ] ; then echo "found name" ; fi ; done What happens if you forget the do or the then or fi?

Reading Input #!/bin/bash!! echo -n "Enter some text > "! read text! echo "You entered: $text"!

More control structures: case #!/bin/bash!! echo -n "Enter a number between 1 and 3 inclusive > "! read character! case $character in! 1 ) echo "You entered one."! ;;! 2 ) echo "You entered two."! ;;! 3 ) echo "You entered three."! ;;! * ) echo "You did not enter a number between 1 and 3."! esac!

Loops - while #!/bin/bash!! number=0! while [ "$number" -lt 10 ]; do! echo "Number = $number"! number=$((number + 1))! done!

Nested commands! Use backticks ` to execute a command inside another! for i in `ls`; do! echo $i! done for i in `ls grep foo tr a-z A-Z`; do! echo $i! done!!

Here Scripts The greatest programmers are also the laziest Really they write programs to save them work When clever programmers write programs, they try to save themselves typing

Here Scripts Here scripts are a form of I/O redirection A here script is constructed like this: command << token! content to be used as command s standard input! token!! token can be any string Often: EOF is short for End Of File

Here Scripts Changing << to <<- causes bash to ignore the leading tabs (but not spaces). Using token prevents expansion

Exercise 9 $ cat > makum #!/bin/bash Write a script that generates a (fairly simple) new script cat > report.sh <<EOF This program says $1 EOF chmod +x report.sh

Exercise 10 $ touch prog.c prog.o none.o $ for ofile in *.o # report if you find a.o file # without corresponding.c file This is difficult! See exercise 3: you can manipulate the $ofile variable

Shell Functions Aliases are good for simple commands Use shell functions if you want something more complex Add the following function to your.bash_profile function today {!!echo Today s date is:!!date + %A, %B %-d, %Y! }!

Shell Functions Function is a shell builtin too! As with alias, you can enter this directly on the command prompt! $ function day {! > echo Today s date is:! > date + %A, %B %-d, %Y! > }!

type There are many types of commands alias, shell function, executable file To determine what a command is, use the type command $ type command!

type $ type l! l is aliased to `ls -l!! $ type cd! cd is a shell builtin!! $ type function! function is a shell keyword!