Shells & Shell Programming (Part B)

Similar documents
Shells and Shell Programming

Shells and Shell Programming

Shell Programming (bash)

Assignment clarifications

Useful Unix Commands Cheat Sheet

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

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

Vi & Shell Scripting

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

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

Conditional Control Structures. Dr.T.Logeswari

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

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

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

CS Unix Tools & Scripting

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Shell programming. Introduction to Operating Systems

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

UNIX shell scripting

9.2 Linux Essentials Exam Objectives

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

CSC209H1S Day Midterm Solutions Winter 2010

A shell can be used in one of two ways:

Last Time. on the website

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

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

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

COMP 4/6262: Programming UNIX

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

bash Execution Control COMP2101 Winter 2019

CSC 2500: Unix Lab Fall 2016

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

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

30 Bash Script Examples

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

Basic Linux (Bash) Commands

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Bash scripting basics

Shell Control Structures

Linux shell programming for Raspberry Pi Users - 2

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

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

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

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

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

Grep and Shell Programming

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

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

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

bash, part 3 Chris GauthierDickey

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

Chapter 5 The Bourne Shell

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

UNIX System Programming Lecture 3: BASH Programming

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

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

Computer Systems and Architecture

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

Bourne Shell Reference

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

Shell Control Structures

CSE 15L Winter Midterm :) Review

Basic Shell Scripting

Computer Systems and Architecture

Bourne Shell Programming Topics Covered

More Scripting Techniques Scripting Process Example Script

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

Part 1: Basic Commands/U3li3es

Lecture 02 The Shell and Shell Scripting

Introduction to Shell Scripting

Introduction to Linux. Roman Cheplyaka

Linux Bash Shell Scripting

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

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

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

COMP2100/2500 Lecture 17: Shell Programming II

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

EECS2301. Lab 1 Winter 2016

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

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

Lab 4: Shell scripting

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Consider the following program.

Essential Skills for Bioinformatics: Unix/Linux

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Title:[ Variables Comparison Operators If Else Statements ]

More Linux Shell Scripts

Shell Programming Overview

Chapter 9. Shell and Kernel

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

Practical 02. Bash & shell scripting

Chapter 4. Unix Tutorial. Unix Shell

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

A Brief Introduction to the Linux Shell for Data Science

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

What is Bash Shell Scripting?

Transcription:

Shells & Shell Programming (Part B) Software Tools EECS2031 Winter 2018 Manos Papagelis Thanks to Karen Reid and Alan J Rosenthal for material in these slides

CONTROL STATEMENTS 2

Control Statements Conditional statements if/then/else test case Loops - Repetitive task statements for while 3

if/then/else if TEST-COMMANDS; then CONSEQUENT-COMMANDS; else ALTERNATE-CONSEQUENT-COMMANDS fi 4

Test The built-in command test is used to construct conditional statements in Bourne shell (sh) Equivalent to [ ] in bash test arguments -d filename Exists as a directory -f filename Exists as a regular file -r filename Exists as a readable file -w filename Exists as a writable file -x filename Exists as an executable file -z string True if empty string str1 = str2 True if str1 equals str2 str1!= str2 True if str1 not equal to str2 int1 -eq int2 True if int1 equals int2 -ne, -gt, -lt, -le -a, -o And, or 5

test: example #!/bin/bash if test -z "$1"; then echo "No command-line arguments." else echo "First command-line argument is $1." fi 6

[ ]: example #!/bin/bash directory="./bashscripting" # bash check if directory exists if [ -d $directory ]; then echo "Directory exists" else echo "Directory does not exists" fi 7

if/test relationship test is a command that returns a value If statements check the return value of the command test equivalent (brackets): [ ] if test! d notes then else fi echo not found echo found = if [! d notes ] then else fi echo not found echo found 8

case case EXPRESSION in PATTERN1) COMMAND-LIST;; PATTERN2) COMMAND-LIST;;... PATTERNN) COMMAND-LIST;; esac 9

case: example #!/bin/bash echo "What is your preferred programming / scripting language" echo "1) bash" echo "2) perl" echo 3) I do not know!" read choice; case $choice in esac 1) echo "You selected bash";; 2) echo "You selected perl";; 3) exit 10

for for NAME [ in LIST ]; do COMMANDS; done 11

for: example #!/bin/bash for f in $( ls /var/ ); do echo $f done 12

for: example 2 Assume that: % ls *.xml % file1.xml file2.xml file3.xml Then: % ls *.xml > list % for i in `cat list`; do cp "$i" "$i".bak ; done % ls *.xml* What would the output be? OUTPUT: % file1.xml file1.xml.bak file2.xml file2.xml.bak file3.xml file3.xml.bak 13

while while CONTROL-COMMAND; do CONSEQUENT-COMMANDS; done 14

while: example #!/bin/bash COUNT=6 while [ $COUNT -gt 0 ]; do echo Value of count is: $COUNT let COUNT=COUNT-1 done Note: let is used (as one way) to evoke arithmetic evaluation of an expression. 15

while: example 2 #!/bin/bash # This script opens 4 terminal windows. i="0" while [ $i -lt 4 ]; do xterm & i=$[$i+1] done 16

COMMAND LINE ARGUMENTS 17

Command line arguments positional parameters: variables that are assigned according to position in a string Command line arguments are placed in positional parameters: %chmod u+x myscript $0 $1 $2 $* 18

Positional Parameters Example: giant #!/bin/sh echo arg1: $1 echo arg2: $2 echo name: $0 echo all: $* $ giant fee fie fo fum arg1: fee arg2: fie name: giant all: fee fie fo fum (Remember to run chmod u+x giant or chmod 711 giant) 19

Positional Parameters Variable What it references $0 Name of the script $# Number of positional parameters $* Lists all positional parameters $@ Same as $* except when in quotes $* Expands to a single argument ( $1 $2 $3 ) $@ Expands to separate arguments ( $1 $2 $3 ) $1.. $9 First 9 positional parameters ${10} 10th positional parameter (need to use braces) 20

set and shift set assigns positional parameters to its arguments. $ set `date` $ echo "The date today is $2 $3, $6" The date today is May 25, 2006 shift change the meaning of the positional parameters giant2 #!/bin/sh while test "$1" do echo $1 shift done $ giant2 fee fie fo fum fee fie fo fum 21

Iterating over arguments Don t use this one unless you know that the argument list will always be short sh allows only 9 positional parameters #!/bin/sh while test "$1" do echo $1 shift done The method below is more portable. Use this one. #!/bin/sh for arg in "$@" do echo $arg done 22

Even more on quotes Getting the quotes right on a loop or similar commands can be a bit tricky. The following 4 loops do different things: for arg in "$*" do echo $arg done for arg in $* do echo $arg done Quotes mean arguments are all in one string. One element for each argument. for arg in "$@" do echo $arg done for arg in $@ do echo $arg done Blanks in the arg list are preserved Does not preserve blanks in arg list. 23

EXPRESSIONS 24

expr Since shell scripts work by text replacement, we need a special function for arithmetic. Strings may be evaluated as numbers using expr x=1 expr $x #evaluates to 1 expr $x + 3 #evaluates to 4 x=`expr $x + 3`#evaluates to x=4 y=`expr 3 * 5` #doesn t work 25

expr: more examples % a=3 % b=$a" + 9" # string concatenation % echo $b 3 + 9 % expr $b 12 % expr $a * $a # wrong! - need to escape * expr: syntax error % expr $a \* $a 9 26

String matching using expr expr $string : $substring Returns the length of matching substring at the beginning of string. Example: % string="hello" % substring="hel" % expr $string : $substring 3 It returns 0 if the substring is not found at the beginning of string. Useful in some simple cases. If you need anything more complicated use Python, Perl, sed or awk. 27

READING USER INPUT 28

read reads one line from standard input and assigns successive words to the specified variables. Leftover words are assigned to the last variable. name #!/bin/sh echo "Enter your name:" read fname lname echo "First: $fname" echo "Last: $lname" $ name Enter your name: Alexander Graham Bell First: Alexander Last: Graham Bell 29

Reading User Input: example #!/bin/bash echo "Hi, please type a word:" read word echo "The word you entered is: $word" echo "Can you please enter two words? " read word1 word2 echo "Here is your input: \"$word1\" \"$word2\" echo "What are your three favorite colours? " # -a makes read command to read into an array read -a colours echo "My favorite colours are ${colours[0]}, ${colours[1]} and ${colours[2]}:-)" 30

Reading from a file while read line do echo $line done < $file Reads one line at a time from a file. $file contains the name of the file that will be read from. 31

FUNCTIONS 32

functions function FUNCTION { } COMMANDS; or FUNCTION () { } COMMANDS; 33

functions (more) You can create your own functions or subroutines: % myfunc() { } arg1=$1 arg2=$2 echo $arg1 $arg2 $globalvar return 0 % globalvar="i am global" % myfunc num1 num2 % num1 num2 I am global Notes: Arguments are passed through positional parameters. Variables defined outside the function are visible within. Return value is the value of the last executed command in the function. 34

functions: example #!/bin/bash # BASH FUNCTIONS DECLARATION function function_b { COMMANDS; } function function_a { echo $1 } # FUNCTION CALLS % function_a "Function A." % function_a. 36

find [path ] [expression] Expression Options: -maxdepth level Tests: -name pattern Base of file name matches shell pattern pattern -newer file File was modified more recently the file. Actions -print -exec 37

find: example Displays the names of all the Java files in directories in and below the current working directory. find. -name "*.java" print 38

USING PIPES 39

The power of pipelines Question 1: How many people with EECS accounts are using the bash shell as their default shell? (We need to know that the default shell is stored in /etc/passwd) papaggel:x:18084:2000:manos Papagelis:/cs/home/papaggel:/cs/local/bin/bash paras273:x:15708:10000:parastoo Baghaei Ravari:/cs/home/paras273:/bin/false pareto:x:9733:7000:park Search Engine:/cs/home/pareto:/buonly parham71:x:17252:10000:parham Amani:/cs/home/parham71:/bin/false paria:x:12757:3000:paria Mehrani:/cs/home/paria:/cs/local/bin/tcsh... 40

The power of pipelines Solution: grep bash /etc/passwd wc -l Answer: 10 41

The power of pipelines Question 2: How many EECS accounts are there? 42

The power of pipelines Solution: wc -l /etc/passwd Answer: 2924 43

Another problem Question 3: How many people are running bash or tcsh right now? 44

More on grep and pipes Solution Steps: Step 1: Display active processes using ps man ps ps normally shows processes associated with your terminal use the options aux to display all processes 45

More on grep and pipes Step 2: Extract the processes running bash. ps aux grep bash root 917 0.0 0.0 115640 1116? S Jan05 0:56 /bin/bash papaggel 1623 0.0 0.0 116964 3756 pts/0 Ss+ 09:10 0:00 -bash papaggel 23113 0.1 0.0 116836 3516 pts/14 Ss 10:37 0:00 -bash papaggel 23309 0.0 0.0 112664 976 pts/14 S+ 10:38 0:00 grep Step 3: Weed out the grep process itself (man grep) ps aux grep bash grep -v grep 46

More on grep and pipes Step 4: Keep only info about user names Strip out only the name Use cut to break each line into fields. Two ways to do it: cut -d " " -f 1» Set the delimiter to be a space and select the first field. cut -c -8» Select characters from beginning to the 8th one ps aux grep bash grep -v grep cut -d " " -f 1 47

man cut NAME cut - remove sections from each line of files SYNOPSIS cut [OPTION]... [FILE]... DESCRIPTION Print selected parts of lines from each FILE to standard output. -c, --characters=list -d, --delimiter=delim -f, --fields=list output only these characters use DELIM instead of TAB for field delimiter output only these fields Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Each range is one of: N N'th byte, character or field, counted from 1 N- from N'th byte, character or field, to end of line N-M from N'th to M'th (included) byte, character or field The order of bytes, characters or fields in the output will be identical to those in the input. With no FILE, or when FILE is -, read standard input. 48

More on grep and pipes Step 5: Sort them ps aux grep bash grep -v grep cut -d " " -f 1 sort Step 6: Get rid of duplicates (if any) ps aux grep bash grep -v grep cut -d " " -f 1 sort uniq Step 7: And finally, count them ps aux grep bash grep -v grep cut -d " " -f 1 sort uniq wc -l

EXAMPLE SHELL SCRIPTS 50