Changes

Jump to: navigation, search

Tutorial5: Redirection

1,059 bytes added, 16:54, 14 October 2020
INVESTIGATION 3: ISSUING MULTIPLE UNIX/LINUX COMMANDS
<br>
===Main Objectives of this Practice Tutorial===
 
:* Understand the purpose of the following file manipulation commands: '''cut''', '''tr''', and '''wc'''
:* Define the terms '''Standard Input''' ('''stdin'''), '''Standard Output''' ('''stdout'''), and '''Standard Error''' ('''stderr''')
:* Understand the purposes of the redirection symbols '''>''', '''>>''', '''2>''', '''2>>''', and '''|''' (pipe)
:* Understand the purpose of the following file manipulation commands: the '''cut/dev/null''', '''tr''', file and the '''wcHere Document'''
:* Define the term '''pipeline command''' and explain how a pipeline command functions
===Redirection (Standard Input, Standard Output, Standard Error)===
<i>''... '''standard Standard streams''' are preconnected '''input and output communication channels''' between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called '''standard input''' ('''stdin'''), '''standard output''' ('''stdout''') and '''standard error''' ('''stderr'''). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams '''abstract''' this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with '''redirection''' or a '''pipeline'''. ''</i>
Reference: https://en.wikipedia.org/wiki/Standard_streams
[[Image:stdin-symbol.png|thumb|right|250px|The '''standard input''' ('''stdin''') symbol that describes where a Unix/Linux command receives '''input''']]
'''Standard input''' ('''stdin''') is a term which describes from where a command receives '''input'''.<br>
This would apply only to Unix/Linux commands that accept stdin input <br>(like ''cat'', ''more'', ''less'', ''sort'', ''grep'', ''head'', ''tail'', ''tr'', ''cut'', ''wc'', etc.).<br>
''Examples:''
<span style="font-family:courier;font-weight:bold">cat <<+<br>Line 1<br>Line 2<br>Line 3<br>+<br><br>
 
===Piping (Using Pipes)===
''Example:''
<span style="font-family:courier;font-weight:bold">(echo "Who is on:"; w) > whoson</span><br>('''''Note: ''' <u>all</u> command output is sent to a file'')
<br>
In this section, you will learn how to redirect ''standard input'', ''standard output '' and ''standard error '' when issuing Unix / Linux commands.
# Save editing changes and exit the text editor.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' < data.txt</span><br><br>What does this command do?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' < data.txt > output.txt</span><br><br>What does this command do? What are the contents of the file ''output.txt''?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' > output.txt < data.txt</span><br><br>What does this command do? Is there any difference in terms of this command and the previous command issued?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' >> output.txt < data.txt</span><br><br>What happens to the content of the '''output.txt''' file? Why?<br><br>
=INVESTIGATION 2: REDIRECTION USING PIPES =
In this section, you will learn to issue '''pipeline commands and learn how ''' to perform tasks<br>using issue Linux commands with or without the generation of having to generate temporary files.
'''Perform the Following Steps:'''
# Change to your Confirm that you are still located in the '''home~/redirect''' directory and confirm that you are now in your home directory.<br><br># Issue the '''ls''' command to view the contents of your '''~/redirect''' directory.<br><br>These are all temporary files that you created in your previous investigation.<br>The '''problem''' with creating temporary files, is that they take up space on your server,<br>and should be removed.<br><br># Issue the following Linux command to remove all temporary files in your ''redirect'' directory: <span style="color:blue;font-weight:bold;font-family:courier;">rm -r ~/redirect</span><br>and confirm that you have removed this directory and its contents.<br><br>'''NOTE:''' You will be issuing a '''pipeline command ''' which will use the pipe symbol "|"<br>that will send the stdout from a command as stdin into another commandwithout having to create temporary files.<br><br># Issue the follow Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin | more</span><br><br>What happened?<br><br># Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin | who</span><br><br>What happened? Why is the result different than antipated?[[Image:pipe-diagram-1.png|thumb|right|350px|]]<br><br>'''NOTE:''' When issuing pipeline commands, commands to the right of the pipe symbol must be designed to <u>accept stdin</u> '''standard input'''. Since the ''who '' command does not, you did NOT see the contents of the '''/bin''' directory but only information relating to the ''who'' command. Therefore, the '''order''' of which you build your pipeline command and the '''type of command''' that is used as a ''filter'' is extremely important!<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp ls /bin/?? > listing.txt</span><br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort -r listing.txt</span><br><br>
# Issue the following Linux command to remove the listing file: <span style="color:blue;font-weight:bold;font-family:courier;">rm listing.txt</span><br><br>
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort -r</span><br><br>You should notice that the output from this pipeline command is the same output<br>from the command you issued in '''step #67'''<br><br># Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort -r | more</span><br><br>What is different with this pipeline command as opposed to the <u>previous </u> pipeline command?<br><br>
# Issue the '''ls''' command.<br><br>You should notice that no files have been created. Let's get practice issuing more pipeline commands<br>using commands (previously learned or new) to be used as filters.<br><br>
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort -r | head -5</span><br><br>What did you notice?<br><br># Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | sort -r | grep r | tail -2</span><br><br>What did you notice?<br><br># Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | sort -r | grep r | cut -c1-3</span><br><br># Issue the following Linux '''pipeline command''':<br><span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | tee unsorted.txt | sort -r | tee sorted.txt | tee unmatched.txt | grep r | tail -2</span><br><br>What did you notice?<br><br>
# Check the files that were created to see how the '''tee''' command was used in the previous pipeline command.<br><br>
# Change to your home directory.<br><br>
'''Perform the Following Steps:'''
# Issue a Linux command to change to your home directory.<br><br>
# Confirm you are located in your '''home''' directory.<br><br>
# Issue the following Linux commands (using semicolon to separate each command): <span style="color:blue;font-weight:bold;font-family:courier;">cal;pwd;date</span><br><br>Note the from the output the order of how each of those commands were processed.<br><br>
# Issue the following Linux commands: <span style="color:blue;font-weight:bold;font-family:courier;">(cal;pwd;date)</span><br><br>Was there any difference in the output of this command as opposed to the previous command?<br><br>Let's see how grouping affects working with redirection.<br><br># Issue the following Linux commands: <span style="color:blue;font-weight:bold;font-family:courier;">cal;pwd;date > output.txt</span><br><br># Issue a Linux command to view the contents of the file called '''output.txt'''<br><br>What do you notice?<br><br>Let's use grouping to make modification to the previous command<br><br># Issue the following Linux commands: <span style="color:blue;font-weight:bold;font-family:courier;">(cal;pwd;date) > output.txt</span><br><br># Issue a Linux command to view the contents of the file called '''output.txt'''<br><br>What do you notice? What did grouping the three Linux commands do?<br><br>
# Issue the following Linux pipeline command (using \ at the end of most lines):<br><span style="color:blue;font-family:courier;font-weight:bold">echo "This will be split over multiple \<br>lines. Note that the shell will realize \<br>that a pipe requires another command, so \<br>it will automatically go to the next line" |tr '[a-z]' '[A-Z]'</span><br><br>Did the command work? What does this command do?<br><br>
# After you complete the Review Questions sections to get additional practice, then work on your '''online assignment 2 '''<br>and complete '''section3 ''' labelled: '''Redirection and Pipes'''.
<br><br>
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
When answering Linux command questions, refer to the following Inverted Tree diagramDiagram. The linux directory is contained in your home directory. Assume that you just logged into your Matrix account. Directories are <u>underlined</u>.
[[Image:week5-dir.png|thumb|left|300px|]]
'''Review Questions:'''
# Write a single Linux command to provide a detailed listing of all files in the '''/bin ''' directory, sending the output to a file called listing.txt in the “projects” “'''projects'''” directory (append output to existing file and use a relative pathname)# Write a single Linux command to redirect the stderr from the command:<br>'''cat a.txt b.txt c.txt ''' to a file called '''error.txt ''' contained in the “assignments” “'''assignments'''” directory. (overwrite previous file’s contents and use only relative pathnames) # Write a single Linux command: '''cat ~/a.txt ~/b.txt ~/c.txt ''' and redirect stdout to a file called “good.txt” to the “tests” directory and stderr to a file called “bad“'''bad.txt” txt'''” to the “tests” “'''tests'''” directory. (overwrite previous contents for both files and use only relative-to-home pathnames).# Write a single Linux command to redirect the stdout from the command:<br>'''cat a.txt b.txt c.txt ''' to a file called wrong.txt contained in the “projects” “'''projects'''” directory and throw-out any standard error messages so they don’t appear on the screen (append output to existing file and use only relative pathnames).<br><br># Write a single Linux '''pipeline command ''' to display a detailed listing of the “projects “directory '''projects''' directory but pause one screen at a time to view and navigate through all of the directory contents. Use a relative-to-home pathname.# Write a single Linux '''pipeline command ''' to display the sorted contents (in reverse alphabetical order) of the “linux” “'''linux'''” directory. Use a relative pathname.# Assume that the text file called “'''.answers.txt” txt'''” contains 10 lines. Write a single Linux pipeline command to only displays lines 5 through 8 for this file. Use only relative pathnames.# Write a single Linux '''pipeline command ''' to only display the contents of the “assignments” “'''assignments'''” directory whose filenames match the pattern “murray” “'''murray'''” (both upper or lowercase). Use an absolute pathname.# Write a single Linux '''pipeline command ''' to display the number of characters contained in the file called “'''.answers.txt”txt'''”. Use a relative-to-home pathname.# Write a single Linux '''pipeline command ''' to display the number of lines contained in the file called “questions“'''questions.txt”txt'''”. Use a relative pathname.# Write a single Linux '''pipeline command ''' to display only the first 10 characters of each filename contained in your current directory. Also, there is will be a lot of output, so also pause at each screenful so you can navigate throughout the display contents. Use a relative pathname.
# Create a '''table''' listing each Linux command, useful options that were mentioned in this tutorial for the following Linux commands: '''cut''' , '''tr''' , '''wc''' , and '''tee'''.
[[Category:ULI101]]
13,420
edits

Navigation menu