Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING 408/508: Computational Techniques for Linguists

Similar presentations


Presentation on theme: "LING 408/508: Computational Techniques for Linguists"— Presentation transcript:

1 LING 408/508: Computational Techniques for Linguists
Lecture 6

2 Shell Arithmetic at the shell prompt:
expr (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= (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)

3 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

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

5 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

6 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)

7 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

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

9 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* / (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

10 First Non-trivial Program
Some possibilities:

11 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!

12 Homework 3 Instructions: email me submit everything in one PDF file!
subject of course number, your name, homework 3 cite any discussion or source due date: Monday 10th by midnight


Download ppt "LING 408/508: Computational Techniques for Linguists"

Similar presentations


Ads by Google