Linux Essentials - Data Streams (stdin, stdout & stderr)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello again everyone and welcome back to learn linux tv in today's video we're going to talk about data streams data streams is how linux handles input output and errors and as part of that conversation we're also going to take a look at output redirection as well there's going to be a lot to learn in today's video so let's go ahead and dive in and get started when it comes to computing i'm sure most of you know the difference between input and output if i type ls that's input when i press enter it shows me the directory listing inside my current working directory that's output so you've already seen an example of input and output and throughout this entire series and pretty much every video on this channel you've seen many examples of input and output but what we're going to do in this video is go a little bit deeper into this subject so that you understand how linux handles input and output as well as errors and like i mentioned in the intro to this video the topic at hand is data streams and there's three specific data streams when it comes to linux their standard input standard output and standard error and the names of these three data streams are often abbreviated for example standard output is abbreviated std out when it comes to standard input it's std in and when it comes to standard error it's often abbreviated as you see here so let's go through these data streams so that you can understand what each one represents now standard input that's what you saw at the beginning of the video when i typed ls i used my keyboard to type those letters and the keyboard itself is an input device so by typing characters into the terminal and pressing enter i'm sending that command via standard input and when it comes to standard output that's what's being shown on the screen so another example if i run sudo apt update and that's a very common command to run on a debian or ubuntu system i'll type in my password another example of standard input and now we can see a bunch of output on the screen it's showing me the results of the command that i just entered in this case it's synchronizing the local package repository index with the remote servers and all of this content that you see on the screen that just flew by on the terminal that was all standard output so when it comes to understanding standard output that's very easy anytime you enter a command and the results of that command are shown on the screen you are actually seeing standard output so at this point you saw examples of standard input and standard output but what about standard error i'm going to show you an example of standard error right now i'm going to run sudo dnf update i'll press enter and here's telling me that the command was not found and that makes sense because dnf is the package manager for distributions such as centos and red hat and since i'm running on an ubuntu system i don't actually have the dnf command available so essentially the command failed now i am seeing output but this is actually standard error and standard error is as simple as that when an error is displayed on the screen that's standard error but you might not always know the difference between standard error and standard output but one thing that we can do is run echo and then dollar sign question mark and what that does is it gives us the return code for the previous command in this case the return code was one okay so what happens when i run sudo apt update i'll try the echo command again and i get a return code of zero and that's certainly interesting but what does return codes have to do with data streams basically when you enter a command any command at all and the command was considered successful the return code is going to be zero zero means success in this example i listed the storage of my current working directory with the ls-l command and there were no errors it worked i received the output we're good and when i check the return code it's zero so the command was successful as you get further and further in the world of linux administration you'll probably reach a point where you start writing scripts and that's a good thing scripts are awesome automation is awesome and when it comes to scripting sometimes you want something special to happen when an error is encountered for example you might want your script to send you an email to let you know that the error happened in that case you'll check for a non-zero return code and then execute another command accordingly now that's why return codes are useful in general now going back to the topic at hand and getting back to data streams where this fits in is that it helps us understand when a command is successful and when it's not successful i mean if the output has the word error in it or failed or permission denied or command not found all of those types of things make it pretty obvious that you've encountered an error in which case you probably don't need to run this echo command but running the echo dollar sign question mark command will give you the return code of the previous command so if you didn't already know if it was a successful command or not you could use that echo command to find out and like i mentioned a return code of zero is successful and a non-zero return code that is not successful so let's see an example of standard output and standard error being displayed at the same time and then what i'll show you how to do is split standard output and standard error and redirect each to their own file so what i'm going to do is type find and i have an entire video about the find command if you're curious how this works but what i'm going to do is start my search in the slash etsy directory and specifically i want a listing of all the files that are inside that directory i'll press enter and we have a list of files here so far nothing seems out of place but if i echo the return code it's a non-zero return code but i did see output here so why do i have a non-zero return code well if i scroll up a bit see if i can find it i'm gonna keep scrolling and right here we actually see an error so what happened is i executed the find command as my normal user account without sudo or root privileges when i got to this particular directory right here apparently my user doesn't have access to see inside that directory and due to that i received a permission denied error so this right here is an error that's standard error but i also have a lot of successful lines here as you can see it's showing me a bunch of files here so i have a lot of successes in this file but i have a few permission denied errors for the directories that i don't have access to so as you saw i have standard output here but i also have an error within the results i probably have more than one error and because i do have errors in the output i didn't get a return code of zero i got a return code of one so there was something wrong here and as you saw i had a few errors within the output and like i mentioned the output of that command gave us standard output and standard error in one shot but what happens if we want to split standard output and standard error and handle them differently so what i'm going to do is execute the find command again and again i'll search the etsy directory i want to show all the files but what i'm also going to do is type 2 and then greater than and then slash dev null and then i'll press enter so as you see here i have some output doesn't look all that different yet but if i scroll up i'm not going to see any errors at all in the output i'm only going to see standard output not standard error and that doesn't mean that the errors didn't happen it's just that the errors were omitted from the output and here's the command that i ran let's take a closer look at this so this section of the command right here is the find command and right here we have a redirect now the greater than symbol allows us to direct the output to a file or some other place but when i put a number in front of it i'm actually telling the command that i want specifically that data stream to go to a different place standard error is represented by the number two in fact standard input standard output and standard error each of those three data streams have their own number designation more specifically when it comes to standard input that has a number designation of zero standard output has a designation of one and standard error like i mentioned before has a value of two so when i ran this command right here by including the number two in front of the redirect operator here the greater than symbol i'm telling it that i want the redirect to only pertain to standard error so at this point i'm deciding that i want standard error to go to somewhere else and specifically i sent it to slash dev null dev null is actually essentially a black hole anything that goes into dev null is never seen from nor heard from ever again it effectively deletes anything you move or copy into it so basically it's a device that contains well nothing so i sent the output of standard air to the void into a black hole it's gone well technically information can't be destroyed but that's a whole nother video for a science channel but for our purposes here anytime you send something to dev null it's being deleted now let's try something interesting what i'm going to do is change the command a bit so what i'm going to do is actually type a single greater than symbol and what i want to do is send the results to a file and i'll send it to results.txt in my home directory notice that i don't have a number designation here at all so what i want to do essentially is send everything into this file although that's not what's going to happen let's see what actually happens when i press enter at this point i receive only errors so as you can see i have these three permission denied messages right here but if i cut out the contents of that text file i have a bunch of lines of output here so when i execute this command right here i'm sending standard output to a file and the thing about that is that standard output is implied since i don't have a number designation here for which data stream i want to send to the file the default is standard output and since standard output does not include errors that means that only standard output is going to be sent to that file but since i didn't tell it to send standard error to a specific place it's actually just going to print standard error on the screen which is why i see these three errors let's go ahead and change this command one more time and i think this is really going to help you guys understand if you don't already the value of being able to split data streams into separate places or handle each one differently so here's the command that we used most recently and just for fun i'm going to put one right here and then at the end of the line i'm going to call out data stream number two which of course is standard error and i'll send that to a different file just like that so what i'm going to do is run the find command and again search for files in the etsy directory i'm going to target specifically data stream 1 which is standard output and send that to results.txt and then i'm going to grab data stream 2 standard error and send that to errors.txt so i'll press enter and nothing happens well technically that's not true in the results.txt file we see standard output meaning all the lines that don't include errors and then inside the errors file we see those three errors that we saw earlier but why is this useful if you are working as a linux administrator then you'll have situations where somebody might want you to test a script or maybe a developer wants you to test a program and as part of that testing you might want to track errors a bit differently and by sending standard error to a file you could then let the developer know the problems that you ran into or they could just check the file themselves if they have access to the server and you're able to do that because you can handle the data streams differently now as a quick aside if i bring that command back since i mentioned that data stream 1 is implied i didn't actually have to type 1 right there so for that reason you never really have to call out data stream 1 you could just omit one and that's assumed and the command is going to execute exactly the same way and since i included a single greater than symbol that means that those files are going to be completely wiped out if i was to type the command like this instead and include two greater than symbols for each that means that the output in each data stream is actually going to be written to the end of the file it's not going to overwrite the file so by including only one greater than symbol each time i'm just making sure that each of these files are going to be recreated each time i run the command i just wanted you guys to be aware of how you append to the end of the file in case you don't want to overwrite it and for a quick example of that i just echoed the string hello world into the hello.txt file and of course it includes that line if i run it again it still only includes that one line if i was to include two greater than symbols it appended that to the end of the line so i can actually just keep running this and as you can see each time i ran that command it appended the output to the end of the file that was just a quick aside anyway getting back to data streams there's three data streams standard input standard output and standard error and standard input is designated by zero standard output is designated by one standard error is designated by two and with this command right here you can see the value of having separate data streams again right here standard output is implied so in that case i'm sending standard output to results.txt standard error is designated by two and i'm going to send that to errors.txt so what i recommend is that you just play around with data streams try to make errors happen within reason of course don't break anything but practice finding out if a command was successful or not and also practice sending standard output and standard air to different places and with enough practice you'll find yourself committing this to memory so there you go i hope this video was helpful in teaching you guys data streams it's a very important concept thankfully it's not a complicated concept but it is important to learn and i hope this video helped you out if it did please click that like button that lets youtube know that you want to see more content just like this and i'll see you in the next video thank you so much for watching [Music] you
Info
Channel: LearnLinuxTV
Views: 7,649
Rating: undefined out of 5
Keywords: Linux, Tutorial, Review, Howto, Guide, Distribution, Distro, Learn Linux, open-source, open source, gnu/linux, LearnLinuxTV, data stream, standard input, stdin, standard output, stdout, standard error, stderr, linux for beginners, linux tutorial, linux shell, linux command line, output stream, command line, stdin stdout stderr linux, redirection in linux, terminal, linux terminal, bash, command-line, commandline, cli
Id: zMKacHGuIHI
Channel Id: undefined
Length: 17min 12sec (1032 seconds)
Published: Mon Nov 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.