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

Similar documents
9.2 Linux Essentials Exam Objectives

Shell Start-up and Configuration Files

Bash scripting basics

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Chapter 4. Unix Tutorial. Unix Shell

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

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

A shell can be used in one of two ways:

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

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

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

Linux Shell Script. J. K. Mandal

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

Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

UNIX System Programming Lecture 3: BASH Programming

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.

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

Exploring UNIX: Session 3

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

COMP 4/6262: Programming UNIX

Lecture 5. Essential skills for bioinformatics: Unix/Linux

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

Useful Unix Commands Cheat Sheet

UNIX shell scripting

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

Unix Introduction to UNIX

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

Title:[ Variables Comparison Operators If Else Statements ]

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

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

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

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

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

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

Linux shell programming for Raspberry Pi Users - 2

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

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

Introduction to the UNIX command line

Shell programming. Introduction to Operating Systems

Computer Systems and Architecture

CS Unix Tools & Scripting

Essential Skills for Bioinformatics: Unix/Linux

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Welcome to the Bash Workshop!

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

Shell Programming (bash)

Linux Bash Shell Scripting

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

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

The Unix Shell & Shell Scripts

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

Introduction to Shell Scripting

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

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

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Week 6 Lesson 1: Control-Flow Statements (Continued)

Introduction to Linux

Shell Scripting. Jeremy Sanders. October 2011

DAVE LIDDAMENT INTRODUCTION TO BASH

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

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

Introduction to Linux

Bash Programming. Student Workbook

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

Basic Linux (Bash) Commands

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

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Control Structures. CIS 118 Intro to LINUX

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

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

Introduction to the Linux Command Line

Shell Programming (ch 10)

Computer Systems and Architecture

Manual Shell Script Linux If Not Equal String Comparison

S E C T I O N O V E R V I E W

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

The Online Unix Manual

Essential Linux Shell Commands

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

Introduction: What is Unix?

Vi & Shell Scripting

Output with printf Input. from a file from a command arguments from the command read

commandname flags arguments

Bash command shell language interpreter

A Brief Introduction to Unix

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Flow Control: Branches and loops

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

Simple Shell Scripting for Scientists

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Part 1: Basic Commands/U3li3es

CS/CIS 249 SP18 - Intro to Information Security

COMS 6100 Class Notes 3

Introduction to Supercomputing

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Introduction. SSH Secure Shell Client 1

Python for Astronomers. Week 1- Basic Python

Transcription:

Unix Shell scripting Dr Alun Moon 7th October 2017 Introduction Shell scripts in Unix are a very powerfull tool, they form much of the standard system as installed. What are these good for? So many file and system administration tasks are easier" using nautilus or the winws explorer or other tools accessible from the graphical desktops. Notation In the notes here and in the man pages help, a particular notation is used. Examples here are shown without the shell prompt ($). Mandatory parameters are parameters that must be given to the command. In the man pages these are often shown in CAPITALS, in these notes I ll put them in angle brackets required, Optional parameters that may be used or left out are shown in square brackets [optional] CP(1) User Commands CP(1) NAME cp - copy files and directories SYNOPSIS cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE... Figure 1: output from man cp Spaces In some places spaces in the bash script are important. Where necessary these will be shown as a symbol, for example if [ -f name

unix shell scripting 2 To try Fragments of scripts to illustrate a point at marked with a blue line ones to try yourself are marked with a green line. Contents of scripts are shown in amber boxes Editing A Script is just a text file containing a sequence of commands. These are performed as if typed from the command line. In Unix systems scripts start with the shebang line code # Comments start witth a # character The script is just a text file and can be edited using any text editor. There are a number of good text-editors about, both GUI based and terminal based. For example: the following script is put in a file ascript; I would use vi vi ascript you can use nano nano ascript then edit the file to contain the following. ascript Figure 2: ascript date whoami pwd Permissions The file has to have its execute bit set in order for it to be used as a command chmod +x ascript It can now be run like any other unix command

unix shell scripting 3./ascript The./ is needed to explicitly give the current directory as the place to look for the script. Bypassing the search of the directories in PATH is needed as it esn t have the current directory in it. Directories and the Working Directory The concept of the working directory is important when working with files and scripts. The shell can be thought of as being in the current working directory commands that operate on files use this as the default directory. For example ls with no parameters lists the files in the working directory. To change the working directory. use the cd directory name command To find the current working directory use the pwd command. Usually the modern convention is to have the prompt show the working directory 1. The pwd command is useful to use in scripts. When referring to files it is in relation to the working directory 2. A plain file-name refers to a file in the working directory. A name prefixed with a directory name (directory/file) it refers to a file in that sub-directory. 1 or sometimes the last folder in the working directory 2 Some editors default to the Documents directory. If your script is saved there you will need to cd Documents to make this the working directory Aside on missing files Try the following in the command shell mkdir folder cp *.h folder You should get an error as follows shell$ mkdir folder shell$ cp *.h folder cp: cannot stat '*.h': No such file or directory The problem is that there are no files ending in a.h, the exact reason for the error is dependent on the way the shell handles the *.h Command-line parameters We want to be able to make scripts that can be given parameters, such as a directory name. We can call these like any other command

unix shell scripting 4./scriptname foldername filename These are accessed using special variables $0... $9 Exercise: 1 Try the following script, take care with the quotes name echo "the script name is '$0'" echo "the first parameter is '$1'" echo "the second parameter is '$2'" Then use the following parameters running the script from the shell./names./names fred./names fred flintstone Note how variables that have not been set are expanded Parameters can be used in any way Exercise: 2 Create a script initf initf mkdir $1/src mkdir $1/bin touch $1/makefile Then try it with./initf../mkdir folder./initf folder./initf Question: 1 What is the meaning of the t. as a directory name? Question: 2 Without parameters, why es the script fail? Variables Shell variables are assigned by simply using their name with an equals sign and a value

unix shell scripting 5 bash $ foo=something bash $ echo $foo Something bash $ bar="some more text (note quotes)" bash $ echo $bar Some more text (note quotes) bash $ Note the lack of spaces round the equals sign. Try foo=this bar=that echo $foo and $bar User input The read command can be used to set variables from used input readex read -p "what is your name?: " first last echo "Hello $first" echo "in the phone book you would be \"$last, $first\"" Variable substitution To use a variable prefix it by a llar sign $. The variable name and its llar are substituted for by the text stored in the variable. If the variable is unset, a blank is substituted. echo :$fred: fred=some-value echo :$fred: unset fred echo :$fred: In the example the variable expansion is surrounded by colons as delimeters to show that when there is no value, nothing is produced Arithmetic Variable substitution is by the text the variable contains. If we want to perform arithmetic we need a different syntax a=1 b=2 echo $((a+b))

unix shell scripting 6 By putting the expression inside the $((... )) several things happen 1. the value of variables is treated as a number 2. arithmetic operations can take place 3. numeric variables not use the llar prefix inside the brackets Conditional Execution The Unix shell gives us the full range of conditional operations. Logic values in unix are based on the exit status of commands 3 3 technically an exit status of 0 is true, non-0 is false tests In order to logic we need to be able to tests. For this we need a function test (more commonly seen as [ ]) some common tests are 4 4 see man test [ -e name ] the file called name exists [ -f name ] the file name exists and is an ordinary name [ -d name ] the file name exists and is a directory [ "string1" = "string2" ] a string comparison for equality [ N -eq M ] an integer numeric comparison for equality Note the need for spaces in the expressions if... fi Try the following life read -p "Type your name, please: " nm echo Hello $nm. echo The ultimate question of life, the universe, and everything... read -p "Please Type the answer: " ch if [ "$ch" == "42" ] then echo That\'s right else echo No, the answer is 42 fi

unix shell scripting 7 Note the spaces in the test and the if..then..else..fi pattern. The test is ne as a string comparison, note how the variable expansion is in a string quote. This avoids the problem where nothing is entered and the variable expansion is a blank. We can use these to check that a directory exists before creating it. dirchk if [! -d $1 ] then mkdir $1 fi Multiple choises For a multi-way decision the if... then... elif exists ifelif if [ ] then elif [ ] then else fi Repetition loops With loops we can perform operations repetitively. while We can keep asking a question until a condition is met.

unix shell scripting 8 liferepr read -p "Type your name, please: " nm echo Hello $nm. echo The ultimate question of life, the universe, and everything... read -p "Please Type the answer: " ch while [ "$ch"!= "42" ] read -p "No, try again! " ch ne echo That\'s right Note that the... ne marks the ends of the section of code repeated for each iteration of the loop shift The shift operation modifies the parameters supplied to the script in $1, $2, etc. Try the following shiftex n=1 while [ "$1"!= "" ] echo "parameter $((n++)) is $1" shift ne./shiftex these are some command line parameters Shift is useful for iterating over a list of parameters shiftsum total=0 while [ "$1"!= "" ] total=$((total+$1)) shift ne echo $total./shiftsum 1 2 27 13 59 3 1 0 10

unix shell scripting 9 for The other operation useful for loops is the for. This takes a list of values and sets a variable to each one in turn. forabc for n in a b c d e f echo $n ne Backquotes and commands The list of values may not be know ahead of time. What we want is a way of generating a list of value to put in the code. We can this by using file globbing forglob for n in * echo file is $n echo file size is ls -hs $n ne We can use the backquote notation to include the output of a command Put the following in a text file, call the file sample.text sample.text this is some standard text split across two lines Then try fortext for n in `cat sample.text` echo word is $n ne There is an alternative notation (that I prefer) that uses the characters $( )

unix shell scripting 10 fortextalt for n in $(cat sample.text) echo word is $n ne Exercises Exercise: 3 Write a script to take a list of filenames and write out some information about each 5 Exercise: 4 Write a script to count-wn from 10 then write a message blast-off 5 hint: take a look at the file command to identify the file Exercise: 5 Write a script to accept two numbers and an arithmetic operator and the calculation, displaying the result. The operators could be + - * / % Exercise: 6 Extend these scripts so that they run repeatedly, until the user opts to exit. Exercise: 7 Write a script supporting a menu system of your own design