Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Redirection

When running programs, it is useful to provide input from a file or to direct output to a file. The syntax [process] > [file] tells your shell to redirect the process’s standard output to a file. Similarly, the syntax [process] < [file] tells your shell to feed the contents of a file to the process’s standard input.

Modfiy your shell so that it supports redirecting stdout and stdin to/from files. You do not need to support redirection for shell built-in commands. You do not need to support stderr redirection or appending to files (e.g. [process] >> [file]). You can assume that there will always be spaces around special characters < and >. Be aware that < [file] or > [file] are not passed as arguments to the program (i.e. you will not find these characters in the argv argument of the main function in shell.c). You will, however, be able to access them via struct tokens* tokens.


Testing

Once you complete your implementation, test the functionality with this provided example test. Follow the instructions below:

  • Create a test file test_redirection_in with the contents, i love pintos and cs162 and save.
  • Start your shell executable and run wc with multiple directions of redirection to ensure your solution is correct:
    ./shell
    0: /usr/bin/wc < test_redirection_in > test_redirection_out
    1: exit
    
  • Inspect the output of test_redirection_out, the out file we created in this test, and ensure the contents roughly match this:
     1  5 24
    
  • This tells us our file is 1 line, has 5 words, and has 24 total characters, which is expected. There may be slight variations in this for a still correct solution. If you see that your output contains a test_redirection_in after the third number, this indicates an error in the first redirection, as your code is reading from the infile rather than getting it from stdin. We recommend testing redirection using this test and other simplified variations you may come up with on your own, before submitting to the autograder.