layout: true
Class 3: Unix and You
--- class: center, middle # Class 3: Unix and You --- # Overview 1. Announcements 2. Review 3. Q&A 4. Exercises 5. Basic assignment --- # Announcements * Basic/Adv - Git 1 due May 18 * Basic/Adv - Unix due May 22 --- # Review * Programs vs. processes * Processes have extra info * Process ID (PID) * User running the process * Current working directory * Program image * Working memory (stack and heap) * Environment variables * File descriptor table --- # Review * Signals are another communication mechanism * ^C: SIGINT * ^Z: SIGTSTP * `kill` --- # Review * Everything is a "file" * Files are a read/write interface * Become a general communication mechanism * Can be used to represent anything that'd work with such an interface * Terminals are files as well! -- * Files have metadata including permissions * Represented in decimal/octal, with three bits * `rwx` ``` user group other rwx rwx rwx 110 100 100 6 4 4 ``` * `ugo` --- # Help/manual pages * Most commands have a flag for showing a brief help message, typically `-h` or `--help` * Many commands and programs have more extensive "man" pages * `man ls` * `man git` -- * More concise man pages available through the `tlrc` package * MacOS: `brew install tlrc` * WSL/Linux: do a web search for "tlrc", find the GitHub page, use one of the options there (nixpkgs or a prebuilt `.deb` file frome GitHub Releases) --- # Shell operation 1. Receive a command from a file or terminal input * `ls -l $HOME > some_file` -- 2. Splits it into tokens separated by __white-space__ * Takes into account _"quoting"_ rules * `ls`, `-l`, __`$HOME`__, `>`, `some_file` -- 3. Expands/substitutes special tokens * `ls`, `-l`, __`/home/akamil`__, `>`, `some_file` -- 4. Perform file redirections (and making sure they don't end up as command args) * `ls`, `-l`, `/home/akamil`; __(set standard output to `some_file`)__ -- 5. Execute command (remember our friend `exec()`?) * `argc` = 3 * `argv` = [`"ls"`, `"-l"`, `"/home/akamil"`] * Standard output redirected to `some_file` * First "normal" token is the command/utility to run --- # Shell operation * File redirection * `<`, `>`, `>>` * Variable assignment * `varname=value` * Variable expansion * Text substitution before execution * `echo $HOME` * `$` indicates variable expansion --- # Shell scripts * Just files with shell commands in them * Same stuff that you'd type at terminal * Shebang to specify interpreter when you execute the file * `#!/bin/bash` * Applicable to other interpreted languages e.g. Python --- ### [signals - turnoff.us](http://turnoff.us/geek/signals/)
--- ### [the real reason not to use sigkill - turnoff.us](http://turnoff.us/geek/dont-sigkill/)
--- # Q&A --- # Exercises 1. Write a command that saves the output of `ls` to a file `listing` -- 2. The command `rev` reverses a line of text and `sort` takes lines of input and outputs them in a sorted manner * Write a command that takes the output of `ls`, reverses the name of each file, sorts these reversed names and saves it to a file called `gnitsil` -- 3. Write a command that runs `git status` and saves the standard output to `out.txt` and standard error to `err.txt` -- 4. The command `date` outputs a timestamp * Write a command that appends the current timestamp to a file called `timestamps.log` --- # Exercises * Write a shell script that appends an ISO 8601 format timestamp, then appends the first argument to a file named `log` * `date -Isec` can get this timestamp for you * `date "+%Y-%m-%dT%H:%M:%S%z"` for macOS (or if you want to be cross compatible) * `$1` expands the first argument * Make sure to give it a shebang * Make sure to `chmod` it so it's executable * Run it with an argument e.g. `$ ./myscript this-is-an-argument` --- class: center, middle # Basic assignment --- class: center, middle # Addenda ---