Help you learn what you need to know about the UNIX operating system and the bash shell to complete the course -- and maybe more
Loukides and Oram, Chapter 2
Sample directory and files:
testunix
infloop: a program that contains an empty infinite loop
mysort: a line sorting program
testfile: a file containing some arbitrary text (show contents)
Relationship between hardware, UNIX kernel, UNIX system functions, standard C functions, C application programs, and shell
Behavior of shell (when used interactively):
Write prompt
Read command(s)
Execute commands (often using UNIX system calls)
Repeat
bash (Bourne Again Shell) is one such UNIX shell
Part of GNU tool set -- a programming system from the Free Software Foundation
Summary of UNIX and bash fundamentals
UNIX file system is hierarchical
Separator character is "/" (not "\")
Examples:
cd /u/rdondero/testunix absolute dnamecd /u/rdondero cd testunix relative dnamecat /u/rdondero/testunix/testfile absolute fnamecd /u/rdondero cat testunix/testfile relative dname
Examples:
cd /u/rdondero/testunix cat t*file cat tes?file cat "testfile" cat 'testfile' cat test\file E.g. # is a special character in bash cd ~rdondero pwd cd ~cs217 pwd cd ~rdondero/testunix pwd cd .. pwd cd . . is typically used only in PATH environment variable
stdin: Special file, always open, default = keyboard, can redirect
To read from stdin: scanf("d", &i); etc.
stdout: Special file, always open, default = video screen, can redirect
To write to stdout: printf("%d", i); etc.
stderr: Special file, always open, default = video screen, can redirect
To write to stderr: fprintf(stderr, "%d", i);
C program should write error messages to stderr
Allows error messages to be separated from normal output (via redirection, as we will see...)
Examples:
mysort one two three ^dmysort < testfilemysort < testfile > junk cat junk rm junkmysort < testfile 2> junk cat junk rm junkmysort < testfile > junk 2> junk bash: junk: cannot overwrite existing file rm junkmysort < testfile > junk 2>&1 cat junk rm junkOrder is somewhat unpredictable because of buffering
Order can be controlled by calling fflush functionmysort < testfile | more mysort < testfile | sort | moreWithin a pipeline:
Can redirect stdin for only first command; can redirect stdout for only last command<up arrow> <down arrow> !mys history !numberbash history list is effectively a file that you can edit using Emacs commands
^c (later) ^z (later) command & (later)
Some commands are bash built-in commands
E.g. cd
Marked with (bash)
bash interprets itself
Some commands are executable binary files
E.g. cat
To execute, bash forks a child process, and the child process then overlays the child process with the executable binary code
Expensive -- see end of course for details
Some are both
E.g. pwd
bash interprets, for efficiency
Rarely need to know the difference
But see Assignment 6!!!
Fundamental: pwd, ls, mkdir, rmdir, cd
Examples of others:
cd /u/rdondero/testdir pwd pushd ~cs217 pwd dirs popd pwd dirs
Fundamental: cat, more, cp, mv, rm
Note -i and -r options
Example: the easiest way to create a file
cat > junk one two ^d cat junk rm junk
Summarized by the UNIX/bash process control summary sheet
Four process states
Foreground process "has the keyboard"
Background can be either running or stopped
Examples:
infloop ^cinfloop ^z jobs bg %1 fg %1 ^cinfloop & jobs kill -9 %1infloop & fg ^cinfloop & ps kill -9 pid
To turn Running Background Process into Stopped Background Process (rarely used):
kill -23 %1 (-24, -26, and -27 also work)
See kill -l for a list of all signals
bash interprets homedir/.bash_profile when you log in
bash interprets homedir/.bashrc when you explicitly invoke bash via "bash" command
Rare if bash is your login shell
Typically:
.bash_profile
source .bashrc
.bashrc
configuration commands
Often placed in .bashrc:
Definitions of environment variables
Inherited by child processes (see final assignment)
PATH environment variable lists directories that UNIX should search to find executable binary commands
Definitions of shell variables
Not inherited by child processes
PS1 environment variable sets bash command prompt
Definitions of shell options
ignoreof: ^d does not terminate bash
noclobber: > and 2> do not overwrite files
Definitions of aliases
Abbreviations for commonly used commands
alias ll='ls -Fla'Etc.
Suggestion: Study given .bash_profile and .bashrc files
bash (and all common UNIX shells) implements a primitive programming language
Control structures: if, while,for
Not covered in this course
See brief description in Loukides and Oram book
File and directory permission commands
Any file has Read, Write, and eXecute permissions for user, group, and other
Read: Can read from file
Write: Can write to file
eXecute: Can execute file (meaningful only if file contains executable binary code or a shell script)
Any directory has Read, Write, and eXecute permissions for user, group, and other
Read: Can list directory contents
Write: Can add file to directory or delete file from directory
eXecute: Can access files in the directory
ls -l
Displays file permissions
chmod
Sets file permissions
umask
Sets default file permissions
Others
Illustrate difference between executable binary command and shell built-in command:
which cat which exitUsed in assignment 6:
echo printenvEtc.
Covered incrementally through course
Examples:
man cat<space>, <enter>, b, q
man cdShell built-in command
man manman setenv man -s3 setenv(See Loukides and Oram p. 29 for man sections)
apropos setenvSearches file of brief command descriptions for specified keyword
Copyright © 2002 by Robert M. Dondero, Jr.