ling 408/508: programming for linguists lecture 5 september 9 th
TRANSCRIPT
![Page 1: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/1.jpg)
LING 408/508: Programming for Linguists
Lecture 5September 9th
![Page 2: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/2.jpg)
Adminstrivia
• Homework 3 graded• Remember: – Submit PDF file1. Always submit the program2. Always submit sample runs3. Explanation appreciated!
![Page 3: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/3.jpg)
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
![Page 4: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/4.jpg)
Today's Topic
• Homework 3 review• Another lecture on the bash shell …
![Page 5: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/5.jpg)
Homework 3 Review
• Shell-script BMI calculator– it can solicit input from the terminal or take
command line arguments
![Page 6: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/6.jpg)
Homework 3 Review
![Page 7: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/7.jpg)
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.jpgBrace expansion:touch f{1,2,3}.jpg
![Page 8: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/8.jpg)
loops
• while-loop:– while COND; do …; done– break
(out of while-loop)
#!/bin/bashline=""while truedo read -p "Next word: " word line="$line $word" if [ ${#line} -gt 30 ]
then break fidoneecho ${#line}:$lineexit 0 until COND; do …; done
${#var} length of stringstored in var
![Page 9: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/9.jpg)
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 $argdone
for arg in "$@"do echo $argdone
![Page 10: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/10.jpg)
Example
• Sum all numbers supplied on command line#!/bin/bashsum=0while [ $# -ne 0 ]do ((sum=sum+$1)) shiftdoneecho $sumexit 0
bash sum.sh 1 2 3 4 515bash sum.sh 1 23bash sum.sh 11bash sum.sh0
![Page 11: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/11.jpg)
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 lsin conjunction with globbing
![Page 12: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/12.jpg)
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
![Page 13: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/13.jpg)
File extension renaming
Shell script:#!/bin/bashif [ $# -ne 2 ]; thenecho “usage: ext1 ext2”exit 1fifor filename in *.$1# Traverse list of files ending with 1st argument.do mv "$filename" "${filename%$1}$2"doneexit 0
create a subdirectorywith some .JPG filessh ../rename_ext.sh JPG jpg
Delete suffix: ${string%substring}
![Page 14: LING 408/508: Programming for Linguists Lecture 5 September 9 th](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697bff61a28abf838cbddef/html5/thumbnails/14.jpg)
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)) $USERtext /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
Substring substitution:${string/substring/replacement}