Week 5 notes
Concepts
Variables - Enviornment variables are named pieces of data, initilized by a shell or by a script run in that shell.
Logical statments - There are a few core constructs in programming. Versions of these are avilible in most shells.
Return Values - When a progam finishes it may set a variable on the way out. This is called a return value.
Continuing Borne shell
Variables
- $@ - quoted array containing arguments passed at the command line
- $# - Number of arguments passed at command line
- $? - last exit or return value
- $! - Process ID of last command exaecuted
- $- - Last assigned end variable.
- $$ - Process ID of current shell
- $8 - Eighth argument passed to last command
- $string - any variable name you want
Variables for addressing background processes
- %+ - most recent process
- %- - second most recent
- %8 - 8th most recent process
Managing background processes
- [control] [z] - send job to background and stop
- bg - tell background job to start
- fg - bring backgound job to foreground
- jobs - list background jobs in this shell
Control structures
- program1 && program2 - If preceeding program returns success run following program
- program1 || program2 - If preceeding program returns failure run following program
- if condition,then,elifcondition,then,else,fi - string of if conditionals
- whilecondition,do,done - srting of while commands
- casevariable,in,possibile varible 1,),command(s),;;,possibile variable 2,),command(s),;;,esac - string of case conditionals
Aditional Borne stuff
- \char - print the following character literally.
- return - return following integer or variable
- read - read in a variable until newline is reached
Script Homework
namekill - write a script that takes a string of characters as an argument and kills all processes containng that string in their name.
userprog - Using the w command in a script, list all users running a program containing a string of characters passed as an agrument to the script.
extra credit... userprogbg - Just like userprog but it does error checking (a simple test should do it: hint hint) to see if an argument was passed.