LING 408/508: Programming for Linguists Lecture 5 September 9 th.

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
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
CIS 191: Linux and Unix Class 3 February 11 th, 2015.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
1.1 Command Substitution The backquote “`” is different from the single quote “´”. It is used for command substitution: `command`  $ LIST=`ls` $ echo.
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.
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.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
LING 408/508: Programming for Linguists Lecture 4 September 2 nd.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
Shell / command interpreter ● interactive user interface with an operating system ● command line interface (CLI) ● understands and executes the commands.
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: whereis, which, whoami, finger, passwd, cal, date Working with Files: cat, more, less.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
LING 408/508: Programming for Linguists Lecture 8 September 23 rd.
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.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
LING 408/508: Programming for Linguists Lecture 18 November 2 nd.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
LING 408/508: Programming for Linguists Online Lecture 6 September 14 th.
Sed. Class Issues vSphere Issues – root only until lab 3.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
LING 408/508: Programming for Linguists Online Lecture 7 September 16 th.
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.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
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.
Bash Scripting 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
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
Strings in BASH.
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
CSE 303 Concepts and Tools for Software Development
What is Bash Shell Scripting?
LING 408/508: Computational Techniques for Linguists
CHAPTER 8 (skip , ) CHAPTER 10
LING 408/508: Computational Techniques for Linguists
CSC 352– Unix Programming, Spring 2016
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
Module 6 Working with Files and Directories
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

LING 408/508: Programming for Linguists Lecture 5 September 9 th

Adminstrivia Homework 3 graded Remember: – Submit PDF file 1.Always submit the program 2.Always submit sample runs 3.Explanation appreciated!

Adminstrivia I'm away next week and the following Monday. Procedure: – Next week there will be lecture slides posted online – And one homework – Following Monday class canceled

Today's Topic Homework 3 review Another lecture on the bash shell …

Homework 3 Review Shell-script BMI calculator – it can solicit input from the terminal or take command line arguments

Homework 3 Review

loops For-loop: – for VAR [in LIST]; do …; done Example: – create backup copies of files – (let's create a few empty.jpg files first using touch ) –for i in *.jpg; do echo $i ; done –for i in *.jpg; do cp $i $i.orig ; done touch f1.jpg f2.jpg f3.jpg Brace expansion: touch f{1,2,3}.jpg

loops while-loop: – while COND; do …; done – break (out of while-loop) #!/bin/bash line="" while true do read -p "Next word: " word line="$line $word" if [ ${#line} -gt 30 ] then break fi done echo ${#line}:$line exit 0  until COND; do …; done ${#var} length of string stored in var ${#var} length of string stored in var

Positional Parameters command line to script (or function): – $0, $1, etc. – $# number of parameters passed – $*all parameters as a single word – each parameter is a quoted string – shiftremoves one parameter (use with $#) for arg in "$*" do echo $arg done for arg in do echo $arg done

Example Sum all numbers supplied on command line #!/bin/bash sum=0 while [ $# -ne 0 ] do ((sum=sum+$1)) shift done echo $sum exit 0 bash sum.sh bash sum.sh bash sum.sh 1 1 bash sum.sh 0

Expansion Arithmetic expansion: – $(( … expression..)) x=$(($x+1)) Pathname expansion (aka globbing): – similar (but not the same) as regular expressions * (wild card string) ? (wild card character) [ab] (a or b) [^ab] (not (a or b)) (curly) Brace expansion: mkdir ~/class/examples/{ex1,ex2} echo x{1,2,3,4} Use command ls in conjunction with globbing

String manipulation String length: – ${#var} Substring: – ${string:position}starting at position (0,1,2…), (-N) from the end – ${string:position:length} Delete prefix: – ${string#substring}shortest match – ${string##substring} longest match Delete suffix: – ${string%substring}shortest match – ${string%substring} longest match Substring substitution: – ${string/substring/replacement}replace first match – ${string//substring/replacement}replace all matches Prefix substitution: ${string/#substring/replacement} Suffix substitution: ${string/%substring/replacement} units=${3:0:1} cp $file ${file%.*}.bak

File extension renaming Shell script: #!/bin/bash if [ $# -ne 2 ]; then echo “usage: ext1 ext2” exit 1 fi for filename in *.$1 # Traverse list of files ending with 1st argument. do mv "$filename" "${filename%$1}$2" done exit 0 create a subdirectory with some.JPG files sh../rename_ext.sh JPG jpg Delete suffix: ${string%substring}

File renaming Example: – append a suffix -1 to all jpg files –for f in *.jpg; do mv $f ${f/./-1.}; done Levels of quoting: $ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER text /home/me/ls-output.txt a b foo 4 me $ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER" text ~/*.txt {a,b} foo 4 me $ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER' text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER Substring substitution: ${string/substring/replacement} Substring substitution: ${string/substring/replacement}