LING 408/508: Programming for Linguists Lecture 8 September 23 rd.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Linux+ Guide to Linux Certification, Second Edition
Linux+ Guide to Linux Certification, Second Edition
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
Using Linux Commands 2 Lab#5
Using Linux Commands 2 Lab#5. Sort command Sort the lines of text files. $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
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.
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.
Linux+ Guide to Linux Certification, Second Edition
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
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.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
LING 408/508: Programming for Linguists Lecture 4 September 2 nd.
1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp slides created by Marty Stepp
Writing Scripts Hadi Otrok COEN 346.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
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.
LING 408/508: Programming for Linguists Online Lecture 6 September 14 th.
Sed. Class Issues vSphere Issues – root only until lab 3.
Linux+ Guide to Linux Certification, Second Edition
LING 408/508: Programming for Linguists Online Lecture 7 September 16 th.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2014 Lecture 3 – I/O Redirection, Shell Scripts.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CIRC Winter Boot Camp 2017 Baowei Liu
SUSE Linux Enterprise Desktop Administration
Prepared by: Eng. Maryam Adel Abdel-Hady
CSC 352– Unix Programming, Fall 2012
Part 1: Basic Commands/Utilities
CSE 374 Programming Concepts & Tools
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
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

LING 408/508: Programming for Linguists Lecture 8 September 23 rd

Adminstrivia Homework 4 graded

Today's Topics Homework 4 review Ungraded Exercise review There's so much more… – but this is probably the 2nd last lecture about the bash shell

Homework 4 Review Exercise 1: – Write a bash shell script that simple accepts command line arguments and prints out the number of arguments, and each argument numbered on a separate line Examples: –bash args.sh a b c Args: 3 #1: a #2: b #3: c bash args.sh Args: 0

Homework 4 Review Many different ways to write the solution…

Homework 4 Review Exercise 2 Consider: –x=$(echo "scale=4;4*a(1)" | bc -l) –echo $x – (computes pi to 4 decimal places) Explain why –x=$((echo "scale=4;4*a(1)" | bc -l)) is wrong (( … arithmetic expansion … )) $( … shell command substitution … ) also backquote: ` … command … `

Homework 4 Review Exercise 3: Remove File/Directory – rm removes a file (dangerous!) – rm –d removes a directory (empty) – rm –rrecursive remove (extreme danger!) – rm –rfforced recursive remove (!!!) Examples: –touch file.txt –rm file.txt (you have default write permission) –touch protected.txt –chmod u-w protected.txt (u = user, -w = remove write permission) –rm protected.txt override r--r--r-- sandiway/staff for protected.txt? –rm –f protected.txt (no interaction: forced removal) –rm –i file.txt (ask it to ask you for confirmation) remove file.txt?

Homework 4 Review best used in interactive shell can put alias shortcut in terminal startup ~/.bash_profile (OSX) or ~/.bashrc alias rm="rm -i" not recursively expanded (considered dangerous: why?) alias (list defined aliases) unalias rm (remove alias) Aliases don't work in shell scripts: #!/bin/bash if [ $# -ne 1 ]; then echo "usage: filename" exit 1 fi touch $1 rm $1 rm –i won't be called! define a function in ~/.bash_profile (absolute path: otherwise recursive) rm () { /bin/rm -i } export –f rm At least two reasons: 1.another computer 2.shell scripts

Other commands with -i -i (interactive confirm option) before overwriting a file –mv-i rename file –cp -i copy file

awk Example: – Top 30 surnames and percentages in the Canary Islands according to wikipedia – pe pe – Filename: surnames.txt (3 fields: rank, name, and percentage of population) Run the following awk code to figure out what the code does: 1.awk '{ print $2; }' surnames.txt 2.awk '{ if ($3>=1) {print $2;} }' surnames.txt

Homework 4 Review Exercise 4 Write awk code to: 1.print a table of and calculate the total percentage of population for the top 10, 20 and 30 surnames 2.read and print out the table with table headings aligned with the field values

Homework 4 Review Exercise 4: Let's develop the answer step-by-step –surnames.txt: $1 $2 $3 – ranknamepercentage –awk '{ sum+=$3 } END {print sum}' surnames.txt –42.42 –awk '{ if ($1 <= 10) sum+=$3 } END {print sum}' surnames.txt –28.96 –…–… –awk 'BEGIN {format="%-5s %-10s %s\n"; printf format, "Rank", "Name", "%"} { if ($1 <= 10) {sum+=$3; printf format, $1, $2, $3} } END {print "Total:", sum, "%"}' surnames.txt

Homework 4 Review Throwaway programming: –awk 'BEGIN {format="%-5s %-10s %s\n"; printf format, "Rank", "Name", "%"} { if ($1 <= 10) {sum+=$3; printf format, $1, $2, $3} } END {print "Total:", sum, "%"}' surnames.txt Rank Name % 1 González Rodríguez Hernández Pérez García Martín Santana Díaz Suárez Sánchez 1.29 Total: %

awk and UTF-8 By default, awk's formatted printing (printf) counts bytes not # of characters. – (This can be considered to be a BUG!) – with UTF-8 character encoding (up to 4 bytes used per character) this can create incorrect formatting  Example: –awk '{ printf "|%-13s|" $2 }' surnames.txt Sometimes using gawk (GNU awk) does the right thing – sudo apt-get install gawk

awk and UTF-8 set the language environment –export LANG=en_US.UTF-8 Re-run command: –awk '{ printf "|%-13s|" $2 }' surnames.txt

awk and UTF-8 May also need:

awk and UTF-8 To make default on login:

Ungraded Exercise Review Changing the line spacing of a text file 1.Write a script that reads each line of a file, then writes the line back out, but with an extra blank line following. This has the effect of double-spacing the file. Note: include if possible a check to see whether the script gets the necessary command-line argument (a filename), and whether the specified file exists. (Exercise adapted from

Ungraded Exercise Review double-spacing the file: -n = non-zero also works read –r If this option is given, backslash does not act as an escape character.

Ungraded Exercise Review double-spacing the file: whitespace trim problem workaround:

Ungraded Exercise Review Changing the line spacing of a text file 2.Next, write a script to echo all lines of a file except for blank lines.

Useful tidbits Pre-programmed interaction: – (here document: inline file) rm () { /bin/rm -i } export -f rm #!/bin/bash if [ $# -ne 1 ]; then echo "usage: filename" exit 1 fi touch $1 rm $1 #!/bin/bash bash confirm.sh $1 <<EOF y EOF #!/bin/bash bash confirm.sh $1 <<<y

Interaction using expect expect is a program that executes preprogrammed interaction using commands including: – expect stringlook for string in input – send stringrespond with string (/r for return) – spawn stringexecute a program under expect – interactgives control to user output will appear on terminal #!/usr/bin/expect -f expect "hello" send "world"

Interaction using expect Knock knock joke expect script: 1.#!/usr/bin/expect -f 2.expect "knock knock\n" 3.send "who's there?\n" 4.expect -re "(.*)\n" 5.send "$expect_out(1,string) who?\n" 6.expect "\n" 7.send "Very funny\n" Kangaroo knock knock joke… regular expression substring (…) stored in variable 1

Interaction using expect Let's interact with the BMI homework solution …