CIRC Summer School 2017 Baowei Liu

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
Linux+ Guide to Linux Certification, Second Edition
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Linux+ Guide to Linux Certification, Third Edition
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
LING 408/508: Programming for Linguists Lecture 5 September 9 th.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Copyright 2009 der.hans intro over a decade as sysadmin, programmer, analyst, data janitor currently director of engineering for a startup adjunct instructor.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CIRC Summer School 2016 Baowei Liu
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
CIRC Winter Boot Camp 2017 Baowei Liu
Department of Computer Engineering
CIRC Summer School 2017 Baowei Liu
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CIRC Winter Boot Camp 2017 Baowei Liu
Shell Scripting March 1st, 2004 Class Meeting 7.
Strings in BASH.
Agenda Control Flow Statements Purpose test statement
Pepper (Help from Dr. Robert Siegfried)
CHAPTER 8 (skip , ) CHAPTER 10
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
The Linux Command Line Chapter 7
CSC 352– Unix Programming, Spring 2016
LING 408/508: Computational Techniques for Linguists
UNIX Reference Sheets CSE 2031 Fall 2010.
Chapter 5 The Bourne Shell
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

CIRC Summer School 2017 Baowei Liu 7.25.2017 Bash Scripting CIRC Summer School 2017 Baowei Liu 7.25.2017

source, . VS execute 1. builtin command in Linux. source filename [argument] 1. builtin command in Linux. 2. read and execute commands from filename in the current shell environment 3. usually used for setting environment variables . Filename [argument] synonymous to source in Bash Make it executable and execute script start a new process when running and close when completed

Conditional Expression and if if conditional expression then …. else fi

Conditional Expression and Operators [[ conditional expression ]] : new, supported by Bash [ … ] : traditional/standard, portable (( … )) : arithmetic operations. (( $a > $b )) ==, !=, <, <=, >, >= (white space are not critical) [[ ”$a” –gt “$b” ]] –gt –le, =, !=, >, <, (white space are critical [ “$a” \> “$b” ] –gt, –le \> \<

Integer Comparison Suggestion: Always use (( )) : for integer comparison > < in [[ ]] is for string comparison!! Example: compareIntegers.sh

Strings Comparison Compare strings: [[ “$a” = “$b” ]] White spaces around [[ ]], [] and operators are necessary!! Double-quoting variables! Examples: compareStrings.sh; compareStrings2.sh

Compound Operators &&, || if [[ … ]] && [[ … ]] then …. fi

Loop Constructs –while loop Conditional Expression while [[ conditional expression ]] do …. done Arithmetic Expansion while (( arithmetic expression )) …

Loop Constructs –until loop Conditional Expression until [[ conditional expression ]] do …. done Arithmetic Expansion until (( arithmetic expression )) …

Flow Control Constructs: Case case expression in pattern1) statement;; pattern2) *) …. esac ) ;; and *

Strings and Manipulation Create a string: string variable Display a string: $stringName Length of a string: ${#stringName} Substring: a Bash string just holds one element Compare strings Concatenate of string: = or += Substring extraction Substring replacement

Substring Extraction ${string:position:length} ${string:position} Example:substring_extraction.sh

Substring Replacement ${string/substring/replacement} ${string//substring/replacement}: replace all matches ${string/#substring/replacement}: replace one match from the front ${string/%substring/replacement}: replace one match from the end String match/replacement can be parameterized

Substring Removal ${string#substring}: Remove the shortest match from the front ${string##substring}: Remove the longest match from the front ${string%substring}: remove the shortest match from the back ${string%%substring}: remove the longest match from the back Example: rm_substr.sh

Common Use Get extension: ${filename##*.} Get filename without extension ${filename%.*} Get extension: ${filename##*.} Get directory name: ${pathname%/*} Get filename: ${pathname##*/} Example: FileName/fileName.sh

Filename Expansion / Globbing Expanding filenames containing special characters Wild cards * ? Square brackets [set]: “-” Special characters: ^ (exclude/ other than) Escaping backslash: protect a subsequent special character

Project Write a Bash script using if/case to count how many files there are in files/ for each of these types: txt, html, xml, data Key tech: editor, chmod, command substitution, for loop, arithmetic expansion, echo, case or if, string compare, substring removal