CS 330 Lecture 3 – $h3||
Agenda
- quarter sheets
- tr
- echoing a line (in Java)
- system call (PHP and sed)
- tcsh vs. bash vs. zsh vs. intellectual development
- subshells in parentheses vs. curly braces
- question
- numbered parameters
- environment variables
- redirection and pipes
- lineN with grep
- new mail
- spamming you all
- what’s the frequency of kenneth?
TODO
- Read http://www.regular-expressions.info/quickstart.html.
- Bring a quarter sheet with two questions and two observations to class.
Perry’s stages of intellectual development
- Dualism. The world is black and white. A is right, and B is wrong. iPhones are phat.
- Multiplicity. Rightness is just an opinion each person holds. Others feel differently. Phones are stupid.
- Relativism. What is right is often backed by reason, evidence, and personal experiences. My iPhone is more than cool; it’s a product of fine engineering and aesthetic sensitivity.
- Commitment. We form our identity using reason, evidence, and experience—tolerating others who have done the same. We recognize our fallibility and adjust when new information comes along. My iPhone is cool, but those Androids have some cool features too.
On shells
- http://www.insectnation.org/howto/bash-vs-tcsh
- http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
- http://stackoverflow.com/questions/4317247/bash-vs-csh-vs-others-which-is-better-for-application-maintenance
- http://shallowsky.com/blog/tags/csh/
Question
What is better? Linear search or binary search?
Code
LineN.java
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class LineN {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File(args[0]));
int targetLine = Integer.parseInt(args[1]);
int i = 0;
while (in.hasNextLine()) {
++i;
String line = in.nextLine();
if (i == targetLine) {
System.out.println(line);
}
}
}
}
linen.php
#!/usr/bin/env php5
<?php
/* print_r($argv); */
$file = $argv[1];
$targetLine = $argv[2];
system("sed -n '$targetLine p' $file");
?>
fixext
#!/usr/bin/env zsh
ext=$1
shift
for file in $*; do
mv $file ${file:r}.$ext
done
TLDL
- Solving the same problem over and over leads to abstractions leads to a language’s strengths.
- Huffman coding: find the frequently-occurring prefixes, swap them out for something shorter. “Economy of expression.”
- Most languages are Turing complete, equally as powerful as one another.
- Shell’s strong suit is in manipulating the file system and gluing programs together.
Haiku
I have a sickness
I’m real allergic to mice
And it’s terminal
I’m real allergic to mice
And it’s terminal