1 / 19

CIS*2450(W05) - Seminar 3

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.

darrin
Download Presentation

CIS*2450(W05) - Seminar 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS*2450(W05) - Seminar 3 Shell Features Command Processing Quick Sort Terminal I/O Structure Charts Q/A on Assignment 2

  2. Shell Features • I/O Redirection • File Stream Redirection • Program I/O Redirection • Environment Variables • Command Line Substitutions

  3. 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

  4. 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

  5. Advanced File Stream Redirection • The syntax for more advanced redirection differs between shells • Use the command chsh to change shells

  6. 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

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

  8. 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.

  9. 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

  10. 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]); } }

  11. 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

  12. 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; }

  13. 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);

  14. 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

  15. 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);

  16. 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

  17. Structure Diagram - byob

  18. Assignment #2 Q/A Any Questions?

  19. The End • Questions? • Comments? • Reminder: A2 program due on Feb 14 • That’s just 11 days!

More Related