120 likes | 331 Views
LING 408/508: Programming for Linguists. Lecture 6 September 11 th. Administrivia. Homework 3 review today Finish off section on shell programming. Homework 3 Review. S hell-script BMI calculator it can solicit input from the terminal or take command line arguments. Homework 3 Review.
E N D
LING 408/508: Programming for Linguists Lecture 6 September 11th
Administrivia • Homework 3 review today • Finish off section on shell programming
Homework 3 Review • Shell-script BMI calculator • it can solicit input from the terminal or take command line arguments
String manipulation • 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 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)) $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
Remove File/Directory • rm removes a file (dangerous!) • rm –d removes a directory (empty) • rm –r recursive remove (extremedanger!) • rm –rf forced recursive remove (!!!) • Examples: • touch file.txt • rmfile.txt (you have default write permission) • touch protected.txt • chmod u-w protected.txt • rmprotected.txt override r--r--r-- sandiway/staff for protected.txt? • rm –f protected.txt (no interaction: forced removal) • rm –ifile.txt (ask it to ask you for confirmation) remove file.txt?
Remove File/Directory 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) • unaliasrm (remove alias) • Aliases don't work for shell scripts: #!/bin/bash if [ $# -ne 1 ]; then echo "usage: filename" exit 1 fi touch $1 rm $1 define a function in ~/.bash_profile (absolute path: otherwise recursive) rm () { /bin/rm -i "$@" } export –f rm rm –iwon't be called!
Other commands • -i (interactive confirm option) before overwriting a file • mv -irename file • cp -icopy file
Useful tidbits • Pre-programmed interaction: • (here document: inline file) rm () { /bin/rm -i "$@" } export -f rm #!/bin/bash bash confirm.sh$1 <<EOF y EOF #!/bin/bash if [ $# -ne 1 ]; then echo "usage: filename" exit 1 fi touch $1 rm $1 #!/bin/bash bash confirm.sh$1 <<<y
Interaction using expect • expect is a program that executes preprogrammed interaction using commands including: • expect string look for string in input • send string respond with string (/r for return) • spawn string execute a program under expect • interact gives control to user output will appear on terminal #!/usr/bin/expect -f expect "hello" send "world"
Interaction using expect • Let's interact with the BMI homework solution …