CSC 352 - UNIX System, Spring 2015 Study guide for the CSC352 midterm exam (20% of grade). Dr. Dale E. Parson, http://faculty.kutztown.edu/parson We will have a midterm on March 19 on material we have covered through spring break except for any Shell programming we might start on March 5. The purpose of this study guide is to limit the amount of material that you need to try to memorize. The UNIX commands and related system information go both deep and wide. We need to limit the scope of an exam. Major topics appear by number below. 1. Files, directories, and related commands. Know about the various types of files: regular files, directories, symbolic links, named pipes, character I/O devices (such as a terminal that reads or writes one character at a time) and block I/O devices (such as a disk that can transfer a block of data at a time). Below are some examples from running ls ld on respective files of those types. The first field in each line shows the type of file referenced by that directory entry. -rw-r--r-- 1 parson faculty 93 Mar 14 2010 bignum.py (regular file) drwxr-xr-x 6 parson faculty 512 Feb 20 19:12 unix (directory) lrwxrwxrwx 1 parson faculty 52 Jan 9 19:40 threadwait -> /export/home/faculty/parson/multip/csc580/threadwait (a symbolic link) prw-r--r-- 1 parson faculty 0 Feb 7 17:08 mypipe (named pipe created via mknod) crw--w---- 1 parson tty 24, 1 Feb 21 10:42 /devices/pseudo/pts@0:1 (character I/O, this is a terminal) brw-r----- 1 root sys 32, 16 Feb 21 10:44 /devices/pci@0/pci@0/pci@1/pci@0/pci@1/pci@0/usb@0,2/storage@2/disk@0,0:a (block I/O, here a disk) Know about the ls command and its options for inspecting the contents of a directory. Here are the options to know. You can combine multiple options together, such as ls alrtr. -l (create a long listing the option letter is ell the above listings are long listing) -a (show all entries including the ones whose names begin with a period) -t (sort the output according to modification time, most recent at the top) -r (reverse the direction of the sort; ls lrt places the oldest at the top) -i (print the inode number every file has an inode number, and multiple hard links may refer to the same, single inode number. The inode number is the actual file ID.) -R (apply this ls command and its options recursively to every subdirectory below) -d (list a directory entry without showing that directories contents. This option shows the directory s entry in its parent, rather than showing what is in that directory.) If you give ls a command line of file names, it will list those. If you give it files names with wildcards such as * without quoting those wildcards, the shell will expand the wildcards before running ls. If any of those names is a directory, it will show the contents of that directory unless you use the d option. You can give absolute paths to ls or any command requiring a file name; and absolute path starts at /, which is the root directory. You can give a relative path as well; a relative path does not have a leading /, and it starts in the current directory. CSC 352, UNIX midterm preparation, page 1 of 6, Spring 2015
The file command shows you what kind of contents are in files. This command looks into the files. Here are some outputs for the file command from the above listed files and some others. -bash-3.00$ file bignum.py bignum.py: ascii text -bash-3.00$ file unix unix: directory -bash-3.00$ file threadwait threadwait: directory (It follows the symbolic link to its target directory or file.) -bash-3.00$ file mypipe mypipe: fifo (A first-in-first-out FIFO is another name for a pipe.) -bash-3.00$ file /devices/pseudo/pts@0:1 /devices/pseudo/pts@0:1: character special (24/1) -bash-3.00$ file /devices/pci@0/pci@0/pci@1/pci@0/pci@1/pci@0/usb@0,2/storage@2/disk@0,0:a /devices/pci@0/pci@0/pci@1/pci@0/pci@1/pci@0/usb@0,2/storage@2/disk@0,0:a: block special (32/16) -bash-3.00$ file TwoDimensionalBoardGame.java TwoDimensionalBoardGame.java: ascii text -bash-3.00$ file TwoDimensionalBoardGame.class TwoDimensionalBoardGame.class: java class file -bash-3.00$ file seeargs.c seeargs.o seeargs seeargs.c: c program text seeargs.o: ELF 32-bit MSB relocatable SPARC Version 1 seeargs: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, not stripped Note that the java class file and the ELF SPARC files above hold executable binary data for compiled Java and C programs respectively. The grep command is good for searching through textual files. We will get to regular expression wildcard patterns later this semester. Here is what you need to know for now. grep STRING FILENAMES grep -i STRING FILENAMES grep STRING -bash-3.00$ ls grep make makefile makejar makelib (Search for STRING in one or more files.) (A case-insensitive version search STRING in upper or lower case.) (Without file names it searches lines in stdin for the STRING.) Example of this last one: Here are some other commands relating to files that you should know about. cat more less wc Concatenate one or more files and send their contents to standard out. Scroll the contents of a file to the terminal. Scroll the contents of a file to the terminal. Count lines, words and characters in a file. Example: CSC 352, UNIX midterm preparation, page 2 of 6, Spring 2015
-bash-3.00$ wc seeargs.c 31 181 1190 seeargs.c find Search a directory given as the first argument for files matching the qualifiers. Qualifiers to know: -type for the file type, such as type d for directories, -type f for regular files. -name for a name, such as name S*.java for all Java source files starting in S. Make sure to quote any name that has a pattern, so the shell does not expand the pattern in the current directory. -mtime for modification time, such as mtime -30 for any file modified in the last 30 days, or mtime +30 for any file with modification older than 30 days ago. zip bundle files into a ZIP archive. zip r recursively includes subdirectory contents. unzip Unzip a ZIP archive created with zip. Examples: zip r seeargs.zip seeargs creates archive seeargs.zip from contents of directory seeargs. unzip seeargs.zip extracts directories and file from above archive. tar Create a so-called tape archive file in a UNIX format. It is not compressed. This command can also extract the files from a tar archive file. gzip GNU zip, a predecessor to zip that can compress an existing file such as a tar archive. zcat,gzcat,gunzip Variants of the extractor for GNU gzip files. emacs vi diff Editor for textual files. Editor for textual files. Compare two textual files for differences. 2. Shell commands, variables, and processes File redirection operators include the following. I have put them in quotes so that MS Word does not alter them. > FILE Sends stdout to the named FILE, either creating or truncating the FILE. >> FILE Sends stdout to the named FILE, either creating or appending the FILE. 2> FILE Sends stderr to the named FILE, either creating or truncating the FILE. 2> &1 Sends stderr to the same destination as stdout. < FILE Reads stdin from the named FILE. command1 commands2 Pipes the stdout of command1 to the stdin of command2. Shell variables. MYVAR=STRING set MYVAR STRING export MYVAR export MYVAR=STRING I will use bash on the exam. Sets the value of MYVAR to STRING in all shells except C-shell. Sets the value of MYVAR to STRING in C-shell. Make MYVAR a global variable visible to processes created by this shell. Same as last line, this form also sets the value on the same line. CSC 352, UNIX midterm preparation, page 3 of 6, Spring 2015
setenv MYVAR STRING echo $MYVAR This is how C-shell exports a global variable. Echo the shell variable to stdout. Use a $ to get the value of a shell variable. Important shell variables: SHELL HOME CDPATH Path to the shell program started by some programs such as script. Path to your login directory. Colon-separated list of directory paths where the shell searches when you use the cd command. PATH Colon-separated list of directory paths where the shell searches for the command Files such as ls that you enter into the shell. Example: -bash-3.00$ echo $PATH /usr/jdk/jdk1.6.0_02/bin/:/opt/csw/bin:/usr/sbin:/opt/csw/graphviz2/bin:/usr/local/smlnj/bin:/bin:/usr/bin:/usr/local/bi n:/usr/ccs/bin:/opt/csw/gcc3/bin:/opt/csw/mysql5/bin:/usr/sfw/bin:/opt/csw/bin:/opt/sunwspro/bin:/usr/jdk/jdk1.6.0 _02/bin:/usr/local/instantclient_10_2 -bash-3.00$ which ls /usr/bin/ls /usr/bin is a component of my $PATH. -bash-3.00$ which kjerhbjke no kjerhbjke in /usr/jdk/jdk1.6.0_02/bin/ /opt/csw/bin /usr/sbin /opt/csw/graphviz2/bin /usr/local/smlnj/bin /bin /usr/bin /usr/local/bin /usr/ccs/bin /opt/csw/gcc3/bin /opt/csw/mysql5/bin /usr/sfw/bin /opt/csw/bin /opt/sunwspro/bin /usr/jdk/jdk1.6.0_02/bin /usr/local/instantclient_10_2 Command line substitution of wildcard characters by the shell occurs before the shell invokes your program. The * (asterisk) substitutes for zero or more characters in the specified directory, and the [abc] pattern matches any one of those characters in the square braces. Example: -bash-3.00$ ls -d $HOME/[Uu]*[xX]* /export/home/faculty/parson/unix /export/home/faculty/parson/unixsysprog Back-quotes, also known as back-ticks, run the command enclosed in the back-quotes, and then substitute whatever that command writes to its stdout, back into the command line containing the back-quotes. For example: -bash-3.00$ cd ~/unix -bash-3.00$ echo `pwd` /export/home/faculty/parson/unix # Substitute stdout from `pwd` back into echo s command line Double quotes allow you to enclose spaces and tabs within a single command line arguments or other string value within the shell. Double quotes inhibit wildcards for file name characters, but double quotes allow shell substitution for $VARIABLES and back-quotes. Here are some examples. -bash-3.00$ ls -d *t runcat -bash-3.00$ ls -d "*t" *t: No such file or directory -bash-3.00$ ls -d $HOME /export/home/faculty/parson CSC 352, UNIX midterm preparation, page 4 of 6, Spring 2015
-bash-3.00$ ls -d "$HOME" /export/home/faculty/parson -bash-3.00$ ls -d "`pwd`" /export/home/faculty/parson/unix -bash-3.00$ echo hello > "three word file" -bash-3.00$ ls -l "three word file" -rw-r--r-- 1 parson faculty 6 Feb 21 20:25 three word file -bash-3.00$ cat "three word file" hello A \ backslash can escape shell special treatment of any single character, such as a space or *. -bash-3.00$ cat three\ word\ file hello -bash-3.00$ echo \$HOME $HOME -bash-3.00$ echo \* * -bash-3.00$ echo \`pwd\` `pwd` Single quotes inhibit wildcards, $VARIABLES and back-quotes from expansion by the shell. Compare the following examples with the double quoted examples above. -bash-3.00$ ls -d *t runcat -bash-3.00$ ls -d '*t' *t: No such file or directory -bash-3.00$ ls -d '$HOME' $HOME: No such file or directory -bash-3.00$ ls -d '`pwd`' `pwd`: No such file or directory GNU make uses a makefile to interpret rules contained in that makefile. Each rule consists of: Target: Dependency Action Where the Target is mandatory, and one or Dependencies (on the same line as the Target) and Actions (on separate lines) are optional. Each Target defines a goal to be accomplished. It may be a target file that needs to be compiled. Each Dependency (if any) is something that the Target depends on. A Dependency may be a source file on which a Target file depends for compilation. A Dependency may also be a Target in another rule; that rule must be executed as part of Dependency checking of the above rule. The Actions are invocation lines passed to the shell, for example to compile files or to remove files. 3. UNIX processes CSC 352, UNIX midterm preparation, page 5 of 6, Spring 2015
When you invoke a program from the shell, the shell requests that the underlying UNIX system A) loads that machine-code program from disk into memory, and then B) runs that program in memory using CPU resources such as registers and arithmetic units. A process is a running program. See the computer diagram from week 1. Many processes spend most of their lifetime in the sleep state, waiting for input (from a terminal, disk drive or network interface device), or waiting for output operations (to a monitor, disk drive or network interface device) to complete. Bill has two CPUs, so only two processes can actually be running at a time. (Harry has 64, Hermione has 16, and Ron has 32.) When a process is asleep, UNIX may page portions of its memory contents to disk, or it may swap the entire snapshot of memory to disk. When the input or output operation that the process was awaiting is completed, UNIX can page or swap the memory images from disk back to memory, then resume running the process until it is ready to sleep again. For processes that do a lot of computation with little I/O so-called CPU-bound processes UNIX interrupts these processes periodically, forcing them to sleep for a while to free up CPU for other processes. -bash-3.00$ ps f # Note the user ID, the process ID, its parent process ID, and its command UID PID PPID C STIME TTY TIME CMD parson 28322 28320 0 20:00:13 pts/33 0:00 -bash parson 242 28322 0 20:55:41 pts/33 0:00 ps f -bash-3.00$ ps f -p 0 # Process ID 0 schedules process to run on the CPU. UID PID PPID C STIME TTY TIME CMD root 0 0 0 Jul 25? 0:12 sched -bash-3.00$ ps f -p 1 # Process ID 1, init, starts other major UNIX processes and cleans up processes. UID PID PPID C STIME TTY TIME CMD root 1 0 0 Jul 25? 21:36 /sbin/init CSC 352, UNIX midterm preparation, page 6 of 6, Spring 2015