Inputs and outputs

Inputs and outputs#

So far we have been using files as arguments for the commands we have practiced. The computer looks at the memory where the file is stored and then passes it through RAM to the processor, where it can perform whatever you have asked it to. We have seen output on the terminal, but it’s equally possible to store that output in memory, as a file. Similarly, if we want to use the output of one command as the input to a second command, we can bypass the step where we make an intermediate file.

The command line understands this in terms of data streams, which are communication channels you can direct to/from files or further commands:

../../_images/stdin_stdout.PNG
stdin: the standard data input stream
stdout: the standard data output stream (defaults to appearing on the terminal)
stderr: the standard error stream (also defaults to the terminal)

Although you can usually give files as input to a program through an argument, you can also use stdin. Further, you can redirect the output of stdout and stderr to files of your choice.

# Copy and rename the file containing the E.coli genome
cd
cp /nfs/teaching/551-0132-00L/1_Unix/genomes/bacteria/escherichia/GCF_000005845.2_ASM584v2/GCF_000005845.2_ASM584v2_genomic.fna E.coli.fna

# Using the standard streams
head < E.coli.fna                  # send the file to head via stdin using '<'
head E.coli.fna > E.coli_head.fna  # send stdout to a new file using '>'
head E.coli.fna 2> E.coli_err.fna  # send stderr to a new file using '2>'

# Advanced use of standard streams
head E.coli.fna &> Ecoli_both.fna  # send both stdout and stderr to the same file using '&>'
head < E.coli.fna > E.coli_head.fna 2> E.coli_err.fna   # combine all three stdin, stdout, stderr

Exercise 2.3#

Exercise 2.3

  • Print the first ten lines of the file E.coli_K12_MG1655.fna in your genomes folder by sending the file to head via stdin

# First go to your home folder
cd
# Then go to the genomes folder within your home folder
cd genomes
# Print the first 10 lines using the standard streams to define that your file is used as input file to the command
head -n 10 < E.coli_K12_MG1655.fna # using stdin
head -n 10 E.coli_K12_MG1655.fna # identical to sending the file to the command via stdin
# Note that in many cases sending the file to a command using stdin is not needed, because this is the default behavior of a command
# It mostly becomes useful if you want to specify both the stdin and stdout in the same line of commands
  • Save the first ten lines of the file E.coli_K12_MG1655.fna in your genomes folder into a new file called E.coli_K12_MG1655_first10.fna

# First go to your home folder
cd
# Then go to the genomes folder within your home folder
cd genomes
# Save the first 10 lines into a new file
head -n 10 < E.coli_K12_MG1655.fna > E.coli_K12_MG1655_first10.fna # using both stdin and stdout
head -n 10 E.coli_K12_MG1655.fna > E.coli_K12_MG1655_first10.fna # identical, but using only stdout
  • Create a stderr file for running the command to extract the first ten lines of the file E.coli_K12_MG1655.fna in your genomes folder

# First go to your home folder
cd
# Then go to the genomes folder within your home folder
cd genomes
# Save the error of the command to extract the first 10 lines into a new file
head -n 10 E.coli_K12_MG1655.fna 2> E.coli_K12_MG1655_first10_err.fna # note that the error file is empty, thus no error occurred while running the command

# Note that stderr results are now saved into your output file, while stdout results of your command are now printed to the terminal and not saved
  • Use advanced options to save both stdout and stderr streams to one or multiple files

# Save both stdout and stderr into the same file
head -n 10 E.coli_K12_MG1655.fna &> E.coli_K12_MG1655_first10_both.fna # note that in this file you have stdout and stderr combined (although stderr is empty)

# Remove the files that you created so far
rm E.coli_K12_MG1655_first10.fna
rm E.coli_K12_MG1655_first10_err.fna
rm E.coli_K12_MG1655_first10_both.fna

# Combine stdout and stderr commands to create separate files for the stdout and stderr
head -n 10 E.coli_K12_MG1655.fna > E.coli_K12_MG1655_first10.fna 2> E.coli_K12_MG1655_err.fna