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.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
UNIX Shell Scripting > Echo “A quick how-to”. What is shell scripting? Interpreted (non-compiled) language Slower compared to compiled languages Much.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
Lab 8 Shell Script Reference:
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
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.
Scripting Languages Course 2 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
LING 408/508: Programming for Linguists Lecture 4 September 2 nd.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 CSE 303 Lecture 5 bash continued: users/groups; permissions; intro to scripting read Linux Pocket Guide pp slides created by Marty Stepp
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.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review.
LING 408/508: Programming for Linguists Lecture 8 September 23 rd.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
LING 408/508: Programming for Linguists Online Lecture 6 September 14 th.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
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.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
Bash Shell Scripting 10 Second Guide.
CIRC Winter Boot Camp 2017 Baowei Liu
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Chapter 9 Shell Programming
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
awk- An Advanced Filter
LING 408/508: Computational Techniques for Linguists
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
Lab 7 Shell Script Reference:
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

LING 408/508: Computational Techniques for Linguists Lecture 6

Shell Arithmetic at the shell prompt: expr 1 + 3 (Need spaces cf. expr 1+3) expr 2 '*' 2 (cf. expr 2 * 2) echo `expr 1 + 3` i=2 (NO SPACES! cf. i = 2) expr $i + 1 let x=1+3 (cf. let x=1 + 3) echo $x let i=$i+1 (also ok let i=i+1) echo $i ((x = 1+ 3)) (spaces not significant) echo $((1+3)) ((i=i+1)) (also ok let i=$i+1)

Comparison operators Examples: Format: 2 5 1 echo $x $i test $x -le $i echo $? (exit status) test $x -le $i -a $i -lt $x echo $? 1 Format: if [ $x OP $y ]; then … (else/elif…) fi [ …. ] is known as test OP: -eq equals -ne not equals -gt greater than -ge greater than or equals -lt less than -le less than or equals

Input At a terminal: read –p "Name: " name read –p "Enter X and Y: " x y echo $x echo $y

Positional Parameters In a shell script: $1: first parameter $#: number of parameters Program: #!/bin/bash echo "Number of parameters: $#" if [ $# -eq 1 ]; then echo "1st parameter: $1" fi Output: sh test.sh Number of parameters: 0 sh test.sh 45 Number of parameters: 1 1st parameter: 45 sh test.sh 45 56 Number of parameters: 2

Running shell scripts Run the program in the current directory: (./ needed if current directory is not in PATH) ./test.sh -bash: ./test.sh: Permission denied ls -l test.sh -rw-r--r-- 1 sandiway staff 98 Sep 4 09:14 test.sh chmod u+x test.sh -rwxr--r-- 1 sandiway staff 98 Sep 4 09:14 test.sh Number of parameters: 0 Supply program filename as a parameter to sh/bash: sh is dash, not bash anymore sh test.sh bash test.sh source test.sh . test.sh (. = source)

First Non-trivial Program Let’s write a simple shell-script BMI calculator it can solicit input from the terminal or take command line arguments try the metric one first

First Non-trivial Program First pass: Several things wrong about this program…

BMI calculator Did you notice bash can only do integers? can use bc (an arbitrary precision calculator) scale = # of decimal places echo "scale=2;2/3" | bc -q .66 but test comparisons (-gt etc.) would then be a pain in the butt can re-scale the formula: Example: weight in kg * 1,000,000 / (height in cm)**2 echo $((68* 1000000 / (165 * 165))) 2497 (24.97) Explanation: echo "…" | means send "..." to next command bc –q means quiet mode Explanation: `…` is command substitution bc -l, use the mathlib In bc, a(1) computes tan-1(1), which is π/4 pi=`echo "scale=50; 4*a(1)" | bc -l` (-l math library) echo $pi 3.14159265358979323846264338327950288419716939937508

First Non-trivial Program Some possibilities:

Homework 3 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 Instructions: email me submit everything in one PDF file! subject of email: course number, your name, homework 3 cite any discussion or source due date: Monday 10th by midnight