Princeton University
COS 217:  Introduction to Programming System

Precept 2:  UNIX and bash

Purpose

Help you learn what you need to know about the UNIX operating system and the bash shell to complete the course -- and maybe more

Reading

Loukides and Oram, Chapter 2

Setup

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)

UNIX and bash

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

Filenames and Dirnames

UNIX file system is hierarchical

Separator character is "/" (not "\")

Examples:

cd /u/rdondero/testunix        		absolute dname
cd /u/rdondero
cd testunix                            	relative dname
cat /u/rdondero/testunix/testfile    	absolute fname
cd /u/rdondero
cat testunix/testfile    		relative dname

Special Filename and Directoryname Characters

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

Special Command Characters

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
^d
mysort < testfile
mysort < testfile > junk
cat junk
rm junk
mysort < testfile 2> junk
cat junk
rm junk
mysort < testfile > junk 2> junk
bash: junk: cannot overwrite existing file
rm junk
mysort < testfile > junk 2>&1
cat junk
rm junk

Order is somewhat unpredictable because of buffering
Order can be controlled by calling fflush function

mysort < testfile | more
mysort < testfile | sort | more

Within a pipeline:
Can redirect stdin for only first command; can redirect stdout for only last command

<up arrow>
<down arrow>
!mys
history
!number

bash history list is effectively a file that you can edit using Emacs commands

^c (later)
^z (later)
command & (later)

Commands

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

Directory-Related Commands

Fundamental: pwd, ls, mkdir, rmdir, cd

Examples of others:

cd /u/rdondero/testdir
pwd
pushd ~cs217
pwd
dirs
popd
pwd
dirs

File-Related Commands

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

Process Control Commands

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
^c
infloop
^z
jobs
bg %1
fg %1
^c
infloop &
jobs
kill -9 %1
infloop &
fg
^c
infloop &
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

Configuration Commands

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

Miscellaneous Commands

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 exit

Used in assignment 6:

echo
printenv

Etc.

Software Development Commands

Covered incrementally through course

Commands for Getting Help

Examples:

man cat

<space>, <enter>, b, q

man cd

Shell built-in command

man man
man setenv
man -s3 setenv

(See Loukides and Oram p. 29 for man sections)

apropos setenv

Searches file of brief command descriptions for specified keyword

Copyright © 2002 by Robert M. Dondero, Jr.