Changes

Jump to: navigation, search

Tutorial5: Redirection

1,260 bytes added, 14:51, 11 February 2021
Redirection (Standard Input, Standard Output, Standard Error)
<table align="right"><tr><td>[[Image:stdout-symbol-1.png|thumb|right|250px|The '''standard inputout''' ('''stdinstdout''') symbol with one greater than sign '''overwrites''' existing file content with command output]]</td><td>[[Image:stdout-symbol-2.png|thumb|right|250px|The '''standard inputoutput''' ('''stdinstdout''') symbol with two greater than signs '''add''' command's output to '''bottom''' of existing file's contents.]]</td></tr></table>
'''Standard output''' ('''stdout''') describes where a command sends its '''output'''.<br>In the examples below, output from a command is sent to the '''monitor''', unless it is sent to a '''text file'''.
[[Image:pipe-diagram-1.png|thumb|right|450px|A '''pipeline command''' sends a command's '''standard output''' directly to '''standard input''' of other command(s) without having to create temporary files.]]
'''Pipeline Command:''' Having commands send their '''standard output''' <u>directly</u><br>to '''standard input''' of other commands WITHOUT having to use '''temporary''' files.
Pipes that are used in a '''pipeline command''' are represented by the '''pipe''' "|" symbol.<br>
[[Image:tee-diagram-1.png|thumb|right|250px|The '''tee''' utility can be used to '''split''' the flow of information. For example to save in a file as well as display on a screen. <br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
The The '''tee''' utility  utility can be used to <u>split </u> the flow of information'''standard output'''<br>between a '''text file''' and the '''terminal screen'''. <br><br>The '''tee ''' option '''-a''' can be used to add content to the '''bottom ''' of an existing file <br>as opposed to ''overwriting '' the file's previous contents.
The reason for the name "'''tee'''" is that the splitting of the flow of information resembles a capital T.
Commands may also be '''spread-out over multiple lines''', making it easier (for humans) to interpret a long command.<br><br>You can add a The '''backslash\''' symbol "\" at “''quotes-out''” the end of a line, to get rid meaning of the special meaning'''ENTER''' key as <u>text</u><br>(i.e. ''new-line'' as instead of newline (to end a ''running'' the command line).
# Issue the '''cat''' command to view the contents of the '''cars''' file.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -c1-10 cars</span><br><br>What did this command do?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f5 cars > field5.txt</span><br><br>What did this command do?<br>Check the contents in the file '''field5.txt''' to see what happened.<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f1-3 cars > field123.txt</span><br><br>What did this command do?(check file contents)<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f1,5 cars > field15.txt</span><br><br>What did this command do?(check file contents)<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">wc cars > count.txt</span><br><br>What information does the '''count.txt''' file contain?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">wc =-l cars > count1.txt</span><br><br>What information does the '''count1.txt''' file contain?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">wc -w cars > count2.txt</span><br><br>What information does the '''count2.txt''' file contain?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -l > listing.txt</span><br><br>What information does the '''count2listing.txt''' file contain?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">pwd > listing.txt</span><br><br>What happenned to the original contents of the file called '''listing.txt'''? Why?<br><br># Issue the following Linux command (use 2 greater-than signs): <span style="color:blue;font-weight:bold;font-family:courier;">date >> listing.txt</span><br><br>What information does the '''listing.txt''' file contain? Why?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars > combined.txt</span><br><br>What information does the '''combined.txt''' file contain? Why?<br><br>'''NOTE''': The '''cat''' command stands for "'''concatenate'''" which means to '''combine''' contents of multiple files into a single file.<br>This is why the command is called "''cat''".<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars murray 2> result.txt</span><br><br>What is displayed on the monitor? What information does the '''result.txt''' file contain? Why?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars murray > myoutput.txt 2> /dev/null</span><br><br>What is displayed on the monitor? What happened to the error message?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars murray > myoutput.txt 2> result.txt</span><br><br>What is displayed on the monitor? what do those files contain? Why?<br><br>The '''Here Document''' allows you to redirect stdin from with the Linux command itself. Let's get some practice using the Here Document.<br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat <<+<br>line 1<br>line 2<br>line 3<br>+</span><br><br>What do you notice?<br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep 2 <<+<br>line 1<br>line 2<br>line 3<br>+</span><br><br>What do you notice? How does this differ from the previous command? Why?<br><br>
# If you encounter errors, make corrections and '''re-run''' the checking script until you receive a congratulations message, then you can proceed.<br><br>
# Issue the '''ls''' command to see all of the '''temporary files''' that were created as a result of redirection.<br><br>The problem with using these redirection symbols is that you create '''temporary text files''' that take up '''space''' on your file system.<br><br>
# Issue a Linux command (using '''Filename Expansion''') to '''remove''' those temporary text files in the current directory.<br><br>
# Issue the following Linux command to check that you removed ALL of those temporary text files:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week5-check-2</span><br><br>
# If you encounter errors, make corrections and '''re-run''' the checking script until you receive a congratulations message, then you can proceed.<br><br>
:In the next investigation, you will be learning how to issue '''pipeline Linux commands''' which can <br>accomplish tasks <u>without</u> creating temporary files.<br><br>
=INVESTIGATION 2: REDIRECTION USING PIPELINE COMMANDS =
'''Perform the Following Steps:'''
# Confirm that you are still located in the '''~/redirect''' directory.<br><br># Issue the '''ls''' command to view the contents of your '''~/redirect''' directory.<br><br>The '''problem''' with creating temporary files, is that they take up space on your server,<br>and should be removed.<br><br># Issue You actually did that in the following Linux command to remove all temporary files in your ''redirect'' directory: <span style="color:blue;font-weight:bold;font-family:courier;">rm ~/redirect/*</span><br>and confirm that you have removed those files for that directoryprevious investigation.<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 command <br><u>without </u> 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?Press '''q''' to exit display.<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? This wonAlthough this pipeline command provides output,<br>it '''t does <u>not</u> work ''' properly as a pipeline command since the '''who''' command is<br>'''NOT''' designed to accept standard input.[[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</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;">ls /bin/?? > listing.txt</span><br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort 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 </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 #75'''.<br><br># Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort | more</span><br><br>What is different difference with this pipeline command as opposed to the <u>previous</u> pipeline command?Press '''q''' to exit display.<br><br># Issue the '''ls''' command.<br><br>You should notice that '''no files have been created'''.<br>Let's get practice issuing more pipeline commands using commands<br>(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 | 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 | grep r | tail -2</span><br><br>What did you notice? Could you predict the output prior to issuing this pipeline command?<br><br># Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | sort | grep r | cut -c1-6</span><br><br>Try to explain step-by-step each process in the pipeline command (including ''filters'')<br>to explain the final output from this pipeine command.<br><br>
# Confirm that you are still located in the '''~/redirect''' directory.<br><br>
# Issue the following Linux '''pipeline command''':<br><span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | tee unsort.txt | sort | tee sort.txt | grep r | tee match.txt | grep r | head </span><br><br>
# Issue the '''ls''' command to view the contents of this redirectory.<br><br>What did you notice?<br><br>
# View the <u>contents </u> of the '''text files ''' that were created to see how the '''tee''' command<br>was used in the previous pipeline command.<br><br>What was the purpose of using the '''tee''' command for this pipeline command?<br><br>You will now run a shell script to confirm that you properly issued that Linux pipeline command<br>using the '''tee''' command and redirection.<br><br># Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week5-check-3</span><br><br>If you encounter errors, make corrections and '''re-run''' the checking script until you receive<br>a congratulations message, then you can proceed.<br><br>
# Change to <u>your</u> '''home''' directory.<br><br>
# Remove the '''~/redirect''' directory and its contents.<br><br>
:In the next investigation, you will learn various techniques to issue '''multiple Linux commands '''<br>on the same line,<br>or long issue a '''single Linux commands command over multiple lines'''.
<br><br>
'''Perform the Following Steps:'''
# Confirm you are located in your '''home''' directoryin your Matrix account.<br><br># Issue the following Linux commands (using the ''semicolon '' character "''';'''" to separate <u>each </u> Linux command): <br><span style="color:blue;font-weight:bold;font-family:courier;">cal;pwd;date</span><br><br>Note the from the output as well as the <u>order </u> of how what each of those commands were processedLinux command results.<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>What happened? Where is the output for the '''date''' command?<br>Why isn't the output for the '''cal''' and '''pwd''' commands are NOT contained in that file?<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>What did you notice?<br><br># Issue a Linux command to view the contents of the file called '''output.txt'''<br><br>What does ''grouping'' do you notice? What did grouping the three when issuing multiple Linux commands do(separated by a semi-colon ";") that uses redirection?<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 is the purpose of issuing a Linux command in this command doway?<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>

Navigation menu