Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How the below shell redirection works ? Appears to be complicated to understand the order of execution. Any easy explanation ?

uuencode host-file  < host-file >encodedfile
share|improve this question
    
diff <(uuencode 1.rkt 1.rkt) <(uuencode 1.rkt < 1.rkt) && echo "Speed thrills but" –  Manav Apr 10 at 15:51

3 Answers 3

uuencode is the command. host-file is its argument. < host-file redirects the file to the standard input of the command. >encodedfile redirects output of the command to the file.

share|improve this answer

When a console program is launched, it opens three standard file descriptors:

0 STDIN
1 STDOUT
2 STDERR

Normally the STDIN filehandle reads from the terminal input (i.e. the keyboard). But when the < redirection is used, input is read from that file instead. Likewise, STDOUT usually writes to the terminal, but when > is used, output is written to that file instead.

STDERR can similarly be redirected by using 2>. You might also see STDERR being redirected to the same place as STDOUT by using 2>&1.

share|improve this answer

uuencode host-file < host-file >encodedfile

Let's break this down into 4 parts:

  1. uuencode is the name of the command to run. The shell searches the PATH environment variable ($PATH) and looks for an executable file named uuencode in each directory in the PATH. In a standard install, this will be /usr/bin/uuencode. The uuencode program takes a binary file and turns it into text in a special format so it can be sent over a network that can't handle non-ascii characters.
  2. The first host-file is the command line argument to uuencode. The uuencode command needs one argument so that it can put the name of the file in the encoded version. The first line of the encoded file will look something like this: begin 644 host-file
  3. < host-file is the shell using a file named host-file as standard input (stdin) for the uuencode process. So when uuencode reads some bytes of input, instead of coming from your keyboard, they come from that file.
  4. >encodefile is the shell using a file named encodedfile as standard output (stdout). Thus, when uuencode writes some output, instead of going to your screen, it goes to that file.

There is not really an order of execution, since only one command, uuencode, is being executed. If you want technical details, read on.

Behind the scenes, the shell calls fork(), then the child process opens the host-file file for reading, opens the encodedfile file for writing, and calls exec(), which replaces the child process (a copy of the shell process) with the uuencode process. The child process then becomes uuencode, with the input file descriptor (a way the operating system keeps track of open files) set to that of host-file and the output file descriptor set to that of encodedfile.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.