Running Unix Shell Commands
Foreground & Background Processes
Normally, commands are run in the foreground
- Unix assigns the running process a process ID (PID)
- Takes control of your terminal until it is done
Running a process in the background
- put '&' at end of the command
- Unix returns process ID and job number
- shell prompt returns immediately
Job Control Viewing process status
- ps lists process id, terminal, time running, and command name
- jobs lists job number, status, and command name, and indicates
most recently (and next-most recently) jobs put in the background
- fg puts most recent job in the foreground (unless job number
is specified)
- Suspend key (usually ^Z) stops the foreground job and puts it in the
background to be resumed later
Killing a job
- terminate a specified process id or job number
- job number prefixed with '%'
- process number has no prefix
can specify signal
- -TERM is default
- -QUIT
- -KILL
Command Line Editing
Editing the Current Line
Your account is setup to use vi editing mode.
At prompt, start out in insert mode.
- Some editing is available; check out special characters with stty
- <DEL> deletes the previous character
- ^W erases the previous word
- ^V quotes the next character
- ^U erases the entire command line
- ^R reprints the current command line
Press <ESC> key to enter control mode
- Moving around the current line
- h/l moves left/right by one character
- b/w moves left/right by one word
- 0/$ moves to beginning/end of line
- Deleting
- X/dh deletes one character back
- x/dl deletes one character forward
- db/df deletes one word back/forward
- D/d$ deletes to end of line
- d0 deletes to beginning of line
- dd deletes entire line
- C deletes to end of line, then enters input mode
- cc deletes entire line, then enters input mode
- Returning to insert mode
- i/a/I/A returns to insert mode
- R overwrites existing text
Editing Previous Lines
You may also edit commands stored in your $HISTORY file
- Moving around the history file
- k/- move up one line
- j/+ move down one line
- Gn move to line n
- Searching the history file
- ?string search backward for string
- /string search foreward for string
- n repeat search in same direction as previous
- N repeat search in opposite direction as previous
Shell Scripts
Simple Shell Scripts
A shell script is a file that contains commands to be executed by the
shell.
- Commands are executed sequentially
- Blank lines, tabs, and spaces are ignored
- # precedes comments
- #! /bin/ksh indicates which shell to use
You need to make the file executable with chmod
.profile
Executed automatically when you login, this customizes your shell environment
with
- aliases define a shorthand for shell commands
- options change the shell's behavior
- variables are names with associated values
- user-created
- keyword
- environment
Aliases
Aliases let you define a shorthand for shell commands that you use frequently
- alias [-x] new=original
- new is a short mnemonic starting with a letter (usually lower-case)
- original is a string that gets substituted when new is typed
- -x option exports alias to child processes
- use single quotes around original when it contains >1 word
- alias rm='rm -I'
- cannot contain arguments
Options
Options are set either on or off
- set -o optionname # turns option on
- set +o optionname # turns option off
- set -o # shows options that are set
| Option |
Description |
Default |
| bgnice |
run bg jobs at low priority |
on |
| ignoreeof |
don't logoff with ^D |
off |
| noclobber |
don't allow overwrite with > |
off |
| noglob |
don't expand file wildcards |
off |
| nounset |
using undefined variable produces error |
off |
User-Created Variables
You can make up variables to represent useful string values
- varname=value
- varname uses letters (typically lower-case), digits, and '_'
- value is some string
- $varname or ${varname} represent the value of the variable
Quoting
How a string is represented determines how the shell interprets it
- No quotes: all special characters are evaluated
- Double quotes: some special characters are evaluated
- variables
- command substitution
- tilde
- Single quotes: no special characters are evaluated
Keyword Shell Variables
Built-in variables have specific meaning to the Unix shell. They generally
use all caps.
- Editing mode variables
COLUMNS, LINES, HISTFILE, EDITOR, VISUAL
- Mail variables
MAIL, MAILCHECK
- Prompting variables
PS1, PS2
- Terminal type
TERM
- Command search path
PATH
- Others
HOME, SHELL, PWD
Environment Shell Variables
Carry over to all sub-processes
- Many built-in variables are also environment variables:
HISTFILE, HOME, LOGNAME, MAIL, MAILPATH, PATH, PWD, SHELL, TERM
- Any variable can be made an environment variable with the export
command
- May define variables to be in environment of specific subprocesses
only
- ENV file defines aliases, options, and variables for all subprocesses
Read-Only Shell Variables
Some variables may only be read by shell scripts
- Command line arguments
- PID numbers
- Exit status