190 likes | 328 Views
CIS*2450(W05) - Seminar 3. Shell Features Command Processing Quick Sort Terminal I/O Structure Charts Q/A on Assignment 2. Shell Features. I/O Redirection File Stream Redirection Program I/O Redirection Environment Variables Command Line Substitutions. File Stream Redirection.
E N D
CIS*2450(W05) - Seminar 3 Shell Features Command Processing Quick Sort Terminal I/O Structure Charts Q/A on Assignment 2
Shell Features • I/O Redirection • File Stream Redirection • Program I/O Redirection • Environment Variables • Command Line Substitutions
File Stream Redirection • Use of a file for stdin/stdout/stderr • Useful for many reasons • Repeating test cases (identical input) • Storing test results • Logging error messages • Easier than using explicit file access • Some syntax varies between different shells • e.g. bash, csh, etc
Basic File Stream Redirection • Using a file for input (via stdin) • byob -review <myfile.marc • Sends in myfile.marc to byob as stdin • Using a file to capture output (to stdout) • ls -l >myfiles.txt • Stores a (long) listing of the files in the current directory into myfiles.txt • Doing Both at the same time • byob -keep t=RISC <in.marc >out.marc • Using >> instead of > appends to the file if it exists
Advanced File Stream Redirection • The syntax for more advanced redirection differs between shells • Use the command chsh to change shells
Program I/O Redirection • Takes the output of one program and uses it for the input on another • Referred to as “piping” • e.g. cat myfile.c | less • Takes the output (stdout) of cat myfile.c and uses it for the input (stdin) of less • In bash, can pipe stderr as well (2|) • Can combine multiple pipes and stream redirections • cat byob.c | tail -10 |grep '//' >c.txt • Store comments from last 10 lines of byob.c into c.txt
Environment Variables • Variables used to configure the system environment and program parameters • bash • To set: export VARNAME="value" • To unset:export -n VARNAME • csh • To set: setenv VARNAME "value" • To unset: unsetenv VARNAME • Can be accessed in makefiles • Can be useful to have a debugging variable
Command Line Substitutions • Add flexibility to command lines • No substitution • echo '$HOME' • Outputs $HOME to stdout • Variable substitution • echo "$HOME" • Outputs the value of HOME (e.g. /stud/username) to stdout • Program output substitution • cat `ls *.c *.h` • Takes the list of all c/h files and gives it to cat as command line arguments.
Command Line Arguments • int argc • Number of arguments given to program • Includes program name • char *argv[] • Array of arguments given to program • Each is a null terminated character array • Arguments are separated by whitespace • Arguments can contain whitespace by putting them in quotes
Using Command Line Arguments int main (int argc, char *argv[]) { int a; for (a=0;a<argc;a++) { printf(“%d) %s\n”,a,argv[a]); } }
Quick Sort • An sorting function built into standard C library • #include <stdlib.h> • Can sort array of any data type • Needs element count and element size • Also requires a comparison function • Otherwise how do you determine rank? • Takes pointers to two elements • Returns <0, 0, >0 if first argument is less than, equal to or greater than second • Pass pointer to function as argument to qsort
Quick Sort - Comparison Function int compareTo(void *a,void *b) { //cast void pointers to correct type int *ai=(int*)a; int *bi=(int*)b; //do the comparison if (*ai<*bi) return -1; else if (*ai==*bi) return 0; return 1; }
Quick Sort - Using //create a pointer to a function int (*funcptr)()=compareTo; //array - array name //8 - number of elements //sizeof(int) - size of an element //funcptr - pointer to comparison function qsort(array,8,sizeof(int),funcptr);
Terminal I/O • Can access current terminal directly • Works even if stdin/stdout is redirected • Simple way • Access current terminal (/dev/tty) as file stream • See pages 164-165 in text • More advanced way • Termios • Overkill for our assignment
Terminal I/O //creating file streams for the current terminal FILE *in=fopen("/dev/tty","r"); FILE *out=fopen("/dev/tty","w"); //read in a string from the terminal //display it back to the terminal //until user enters "quit" do { fprintf(out,message); fscanf(in,"%s",buffer); fprintf(out, "You entered \"%s\"\n",buffer); } while(strcmp(buffer,"quit")!=0);
Structure Diagrams • Represents a File • Usually a file on disk but can be other streams as well • Represents a module • moduleA calls moduleB myModule moduleA moduleB
Assignment #2 Q/A Any Questions?
The End • Questions? • Comments? • Reminder: A2 program due on Feb 14 • That’s just 11 days!