Computer Systems and Architecture

Similar documents
Computer Systems and Architecture

Shell programming. Introduction to Operating Systems

Bash scripting basics

Bourne Shell Reference

COMP 4/6262: Programming UNIX

Lab 4: Shell scripting

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

Conditional Control Structures. Dr.T.Logeswari

Control Structures. CIS 118 Intro to LINUX

CS Unix Tools & Scripting

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Title:[ Variables Comparison Operators If Else Statements ]

UNIX shell scripting

bash Execution Control COMP2101 Winter 2019

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

A shell can be used in one of two ways:

CSC 2500: Unix Lab Fall 2016

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

Shell Programming (Part 2)

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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.

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

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

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

9.2 Linux Essentials Exam Objectives

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

Linux shell programming for Raspberry Pi Users - 2

Practical 04: UNIX / LINUX Shell Programming 1

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

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

Shell Programming (bash)

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shells & Shell Programming (Part B)

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

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

UNIX System Programming Lecture 3: BASH Programming

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

Bash shell programming Part II Control statements

Basic Linux (Bash) Commands

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

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

Vi & Shell Scripting

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

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

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

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

Practice 5 Batch & Shell Programming

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Shell Start-up and Configuration Files

Useful Unix Commands Cheat Sheet

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

Scripting versus Programming

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

30 Bash Script Examples

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

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

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

Mills HPC Tutorial Series. Linux Basics II

Essential Skills for Bioinformatics: Unix/Linux

Lecture 5. Essential skills for bioinformatics: Unix/Linux

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

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

Lecture 02 The Shell and Shell Scripting

Linux Bash Shell Scripting

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

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

Bourne Shell Programming Topics Covered

Shell Control Structures

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

DAVE LIDDAMENT INTRODUCTION TO BASH

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

Processes and Shells

More Linux Shell Scripts

Shell Control Structures

RHCE BOOT CAMP. System Administration

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

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

BASH Programming Introduction

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

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?

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Chapter 5 The Bourne Shell

Part 1: Basic Commands/U3li3es

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

Shell Programming (ch 10)

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

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

Shells and Shell Programming

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

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

Basic Shell Scripting

Shells and Shell Programming

What is Bash Shell Scripting?

CSE 15L Winter Midterm :) Review

Transcription:

Computer Systems and Architecture Stephen Pauwels UNIX Scripting Academic Year 2018-2019

Outline Basics Conditionals Loops Advanced Exercises

Shell Scripts Grouping commands into a single file Reusability Possible to use programming constructs Variables Conditionals Loops No compilation required

How to create a Shell Script 1. Save script as a (.sh) file 2. Add the line #!/bin/bash to the beginning of the scipt #! indicates that the file is a script /bin/bash is the shell that is used to execute the script When the scipt is executed, the program after the #! is executed and the name of the script is passed to it Since the line starts with a # it is ignored by the shell 3. Make the script executable using chmod +x 4. Execute the script by calling it Put./ in front of the name in order to avoid confusion with commands

Comments Comments are placed behind a # and last until the end of the line There are no multiline comments The #! line is a comment

Variables - Basic Assigning a variable VARIABLE=value VARIABLE=$(command options arguments) No spaces before and after the =! Using the value of variables Place a $ before the name If the variable name is followed by text -> place the name between braces E.g.: echo Today is the ${DAY}th day of the week Getting keyboard input read VARIABLE Exporting variables To make them accessible from other programs Place export before the name of the variable E.g.: export PATH= /bin:/usr/bin

Variables - Specials $@ Expands to the list of positional parameters separated by commas $# The number of positional parameters $0 The name of the script $1,, $9 The nine first positional parameters $? The exit status of the last executed command $! The PID of the last process that was started in the script $RANDOM A positive random integer

Example script.sh #! /bin/bash name=$(whoami) echo Hello $name! Terminal chmod +x script.sh./script.sh

Example script.sh #! /bin/bash name=$(whoami) echo Hello $name! Location of shell Store output of whoami into variable name Beware: - put commands between ( and ) - use $ to point to value of cmd Call variable name using $ Make script executable Terminal chmod +x script.sh./script.sh Run script.sh

Conditions - Basic Between [ ] Spaces before and after [ ] Examples [ -d dir ] returns true if dir is a directory [ $var eq 2 ] returns true if $var equals 2 [ $var eq 1 ] [ $var eq 2 ] returns true if $var equals 1 or 2

Conditions Files -e File exists -d Is a directory -f Is a regular file -r Is readible -w Is Writeable Strings -n Length of string is nonzero -z Length of string is zero s1 = s2 s1 and s2 are identical s1!= s2 s1 and s2 are not identical

Conditions Numbers i1 eq i2 i1 ne i2 i1 gt i2 i1 ge i2 i1 lt i2 i1 le i2 i1 and i2 are equal i1 and i2 are not equal i1 is greater than i2 i1 is greater than or equal to i2 i1 is less than i2 i1 is less than or equal to i2 And, or, not! NOT operator && AND operator OR operator

If statements Zero or more elif clauses are possible The else clause is optional The if body is executed if the exit status of the condition is 0 if [ ] then commands1 elif command then commands2 else commands3 fi

If statements Zero or more elif clauses are possible The else clause is optional The if body is executed if the exit status of the condition is 0 if [ ] then commands1 elif command then commands2 else commands3 fi If condition is true execute commands1 If exit status of command is 0 execute commands2 If all other clauses are false exectue commands3

Case statements Executes code based on which pattern matches a word Multiple cases can be specified per block by separating them using Each block has to be terminated by a ;; Use * to match the rest If multiple cases match, the first one is executed case $NUMBER in 11 12 13) echo ${NUMBER}th ;; *1) echo ${NUMBER}st ;; *) echo ${NUMBER}th ;; esac

For loops The list can be A literal list: a b c A glob pattern: *.jpeg The output of a command: `ls a` The body is executed for each element in the list The loop variable is set to the value of the current word for VARIABLE in list do echo $VARIABLE done

While and Until loops The condition is evaluated on each iteration While loops are executed as long as the exit status of the condition is zero Until loops are executed as long as the exit status of the condition is not zero while condition do commands done until condition do commands done

Break and continue Break casues the loop to be exited immediately Continue causes a loop to continue with the next iteration An integer parameter can be specified to continue or break from the nth enclosing loop break 2 will break from the second enclosing loop continue 1 is the same as continue

Arithmetic Arithmetic can be performed between (( and )) Only operations on integers are possible The exit status is 0 when the result of the expression is not zero and 1 if the result of the expression is zero An expression between $(( and )) expands to the result of the expression For more advanced calculations bc can be used

Arithmetic - Example #! /bin/bash A=$RANDOM B=$RANDOM C=$A D=$B while ((D!= 0)) do TEMP=$D D=$((C % D)) C=$TEMP done echo The GCD of $A and $B is $C

Functions Functions behave the same as commands The exit status of the function is the exit status of the last executed process Parameters are placed in variables $1,,$9 Use return to exit from the function early Use the local keyword to make local variables Hello() { echo Hello World $1 $2 } Hello Zara Ali

Functions Functions behave the same as commands The exit status of the function is the exit status of the last executed process Parameters are placed in variables $1,,$9 Use return to exit from the function early Use the local keyword to make local variables Hello() { echo Hello World $1 $2 } Hello Zara Ali Function definition Call function with parameters

Further reading The Bash Manual www.gnu.org/software/bash/manual/bashref.html Advanced Bash-Scripting Guide www.tldp.org/ldp/abs/html/ UNIX tutorials www.tutorialspoint.com/unix/

Exercises Blackboard Course webpage http://msdl.cs.mcgill.ca/people/hv/teaching/computersystemsarchitecture/#cs3