LING 408/508: Computational Techniques for Linguists

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
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
CIS 191: Linux and Unix Class 3 February 11 th, 2015.
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.
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.
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 ?
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.
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.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
©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.
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.
LING 408/508: Programming for Linguists Lecture 5 September 9 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.
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.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
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
Linux Shell Programming
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CIRC Summer School 2017 Baowei Liu
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
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
LING 408/508: Computational Techniques for Linguists
Copyright © – Curt Hill Bash Scripting Fundamentals Copyright © – Curt Hill.
CSC 352– Unix Programming, Spring 2016
LING 408/508: Computational Techniques for Linguists
CSE 303 Concepts and Tools for Software Development
Essential Shell Programming
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
Chapter 5 The Bourne Shell
Module 6 Working with Files and Directories
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

LING 408/508: Computational Techniques for Linguists Lecture 7

Today's Topics Homework 3 review More on bash programming

Homework 3 review Modify the BMI calculator to: accept either command line arguments or read from the terminal if they’re missing print the weight status message according to the following table: modify the calculator to accept input in both metric and traditional units make sure you supply examples of your program working!

Homework 3 review

Homework 3 review

Homework 3 review

loops For-loop: Example: for VAR [in LIST]; do …; done 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: until COND; do …; done #!/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 while-loop: while COND; do …; done break (out of while-loop) ${#var} length of string stored in var until COND; do …; done

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 shift removes 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 1 2 3 4 5 15 bash sum.sh 1 2 3 bash sum.sh 1 1 bash sum.sh

Expansion Arithmetic expansion: Pathname expansion (aka globbing): $(( … 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: Substring: Delete prefix: ${#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 Levels of quoting: Example: Substring substitution: ${string/substring/replacement} 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 http://linuxcommand.org