Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In BASH, digit1>&digit2 means redirecting descriptor digit1 to the same file as

ID: 3725683 • Letter: I

Question

 In BASH, digit1>&digit2 means redirecting descriptor digit1 to the same file as descriptor digit2. Please explain the following two commands, given that BASH process a command line from left to right. ./command >outfile 2>&1 ./command 2>&1 >outfile (1) Explain, if the command has no permission to write to the "outfile", what will happen to the first command line, and what will happen to the second command line. Note that you will see error messages for both command lines. However, the underlying mechanism of the two error message is different. (2) Make an experiment to verify your analysis and show the screenshots of your experiment. 

Explanation / Answer

Things to note

In linux File Descriptor 1 is for standard output and File descriptor 2 is Standard Error.

I made a simple python program which prints 2 lines. One to standard output and one to standard Error.

I am attaching screenshot of the running of these commands below:

When we simply run the file both standard message and error message gets printed to standard output.

When you execute first command ie./command>outfile 2>&1, It redirects the output descriptors to outfile.

As the command executes from lef to right so, all program messages first get written to outfile and then it changes the error descriptor to point to standard output, which is of no use now as the program has finished its execution.

In second command ie ./command 2>&1 >outfile, It first redirects the error descriptor to point to standard output then it outputs to file. In this case error messages gets printed on standard output as shown in screenshot and the standard messages goes to file.

After changing the permissions of file to just read only i.e. the process cannot write to file.