1 / 12

LING 408/508: Programming for Linguists

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.

dara-craft
Télécharger la présentation

LING 408/508: Programming for Linguists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LING 408/508: Programming for Linguists Lecture 6 September 11th

  2. Administrivia • Homework 3 review today • Finish off section on shell programming

  3. Homework 3 Review • Shell-script BMI calculator • it can solicit input from the terminal or take command line arguments

  4. Homework 3 Review

  5. 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

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

  7. 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?

  8. 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!

  9. Other commands • -i (interactive confirm option) before overwriting a file • mv -irename file • cp -icopy file

  10. 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

  11. 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"

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

More Related