Learn CentOS 19 - Streams

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to my sent away series and today I am continuing the theme of things that I probably should have taught you guys earlier in the series and in today's video the theme is in output redirection so you've probably seen me do some sort of output redirection throughout the series so far but I definitely wanted to give it its own video in this episode today so let's go ahead and get started now to illustrate this example I'm again going to use SSH I don't know why I keep deciding to use this as an example but you know what I've been doing it I may as well continue doing it and I'm going to cat out the Etsy sshd config file right here and of course permission is denied I knew that honest so I'm going to recall that command with sudo and put in my super secret password and you know basically I just printed the entire content of this file and you know what it's quite long isn't it so I'm actually looking for a very specific thing so I'll clear my screen and then I'll recall the previous command and what I'm going to do is pipe that into the grep command and then what I want to grep for is port I'll press enter and it's using port 22 that's the default for SSH I knew that but the point is if I'm looking for something very specific I can cat a file and grep a particular thing of that file and only show that one thing and that's basically an example of piping output from one command to another which is the first thing I'm going to talk about today because it's a very common thing now honestly I didn't even need to pipe output to do the same thing I could have just done this I could have ran sudo graph and then I'll just type port and then the path and it does the exact same thing note output redirection is required grep can be used by itself you don't actually need to pipe things into grep although that works just fine and everybody does it you know you don't have to do it that's just an example so then what happens if we run a command like this I'm going to run echo and then I'll do hello obviously you know that by itself is simply going to echo whatever I type there but what if I redirect that to another command and what I'm going to do is use the greater than symbol to redirect that to my file dot txt I'll press ENTER and if I cat the contents of that file we can see that it has exactly what I sent to it I basically sent the output of the echo command to my file dot txt and similarly I could just do LS dash L and then I can use that same greater than symbol and then I'll just call mine my file too this time and if I cat the contents of that we have the output of the LS command in that file so what happens then if I just run that command again and again and again and again and again a bunch of times right well let's see what happens nothing absolutely nothing happened and the reason for that is because when we redirect the output of the LS dash L command with a single greater than symbol is going to overwrite that entire file which is why you have to be very careful if you were intending for the output to go to the end of the file and you wanted to preserve what's in the file now well you just blew it away so that's the one important thing I want to tell you guys is always be very careful with output redirection because you can actually destroy very important files but what I can do here is use two greater than symbols now let's see what that file contains and it contains the output that many times as you can see so that gives you an idea of overwriting a file versus appending again if I was just to basically run this with a single greater than symbol and then I kept the file as you can guess I blew it away and it now only contains the output of that most recent command now what this constitutes is actually the concept of streams there is standard input that's a stream there's standard output and there's also standard error so there's three of these and it's very important to understand the difference but thankfully it's a very easy concept now standard output is actually something that you guys have been using all along without even knowing it so if I run LS dash L you know as you already know it basically just gives you the entire directory listing of everything in your current working directory we've run this probably a hundred times by now or at least it feels like it but that standard output if you run a command and it prints something on your screen its standard output it's that simple so if I was to run again echo and then hello the word hello is printed to standard output again anything that's printed to your terminal is standard output and then also I mentioned in a previous video if you echo dollar sign question mark that gives you the exit code of the previous command in this case zero zero means success anything other than zero is not a success and this was a successful command because I simply told it to print hello to standard output which puts it on the screen it was able to do that so that was a successful command so it gets the exit code zero now if I was to run this command again echo hello but I just fat-fingers something and I just don't type it correctly well let's see what happens and you already know if you watched a previous video where I covered this it gives me an exit code in this case 127 it's not zero that's failure now that standard error that was an actual error because the command is not found and now what's confusing is it is being printed on the screen so it is showing the results in standard output so it basically showed me in standard output that well there was a problem but this is standard output but it's also standard error because there was an error and if something is registered as an error you can basically do something different with it which I'm going to show you in this video but I wanted to show you guys the difference now when it comes to standard input it's actually a little bit more challenging to show you guys because it's not something that we generally run on the command line although we do actually use it every day I'll explain so anytime you are being prompted to enter some information for example when you run sudo you are being asked to enter your password you are entering your password standard input anything that's input from the user is standard input it's information going in now obviously when you are just running simple commands you usually won't redirect something to standard input there are some examples that I'm going to give you that's going to show you what that actually looks like in practice it's just not as common as standard output and standard error now one example that I'm going to give you guys is my sequel dump now I'm not going to give you guys a course on database administration or anything like that but as a Linux administrator you will most likely find yourself at some point dumping a database into a file and then restoring it I'm not going to go over my sequel dump or even my sequel in its entirety or anything like that I'm just going to show you what the command would look like if we were actually doing that now with the my sequel dump command essentially you would have - Aitch and then you would have some URL to a database obviously this command won't work and you don't have to try it I'm just showing you an example you'll have a user and you'll have a password but you don't type the password here you don't want people to in the clear and then basically you will give it a database name and since we want to create a backup of a database we will use standard output a single greater than symbol and then we will call it something like my DB dot sequel for example and you know just so you know you don't actually need the space here it works without that it just is seen as cleaner by some people maybe most people are some number of people that it just looks easier to read this way so that's why people put a space there but essentially this is just an example command and right now we are illustrating standard output because we have a database and we're going to connect to an external database I type the URL here whatever that is the password or excuse me the user and then - P means I intend to type a password it will ask you for the password which is another example of standard input because it's asking you for something and then the database name the one I want to backup and then I'm going to redirect it into a dump file now when you want to restore such a backup the command will look something like this so first of all it looks very similar to the previous command doesn't it and you know there might be some syntax errors with this command that's not the point it's just an example and of course I'll type the DB name here now if you are an expert in my sequel you know you can avoid typing the database name there's there's a way to automate that not going there but what we're going to use this time is standard input because I want to take the contents of a file and use that as input to the my sequel command so that's when I use the DB dump file whatever I named it and I already forgot what that was but basically my DB dump whatever it is sequel whatever you named it and basically what we're doing is we're taking the contents of this file and this is the symbol for standard input we're using this file the contents of this file is input to the my sequel command which connects with - H for host which you know you type the URL again like I mentioned the user and you will type a password so you just type P it'll ask you for that so it's going to ask you for the password standard input and then it's going to use my DB dump as standard input this file should already exist if you were actually creating a database backup and restoring it and it's going to basically restore that into my sequel so basically you're getting an example of standard input now this is not something you will use every day but it's something that you definitely will use during your career as a Linux administrator so now I'm going to give you guys some examples of output redirection maybe you want to redirect standard output a certain way or maybe you want to direct standard error to a file you could basically handle these streams independently when it comes to your commands so I'll give you an example the command I will use to illustrate this is the find command which is very awesome and what I want to do is look in the Etsy directory for anything let's just say name so we're gonna search by name and I want to search for anything that ends in dot CFG so I want to find any files that ends in CFG in the SC directory I'll press ENTER I get a bunch of permission denied error now I did get some genuinely good output I mean look at this we have a file right here that meets that criteria we have another one right here we have one here and we have one here but we have a bunch of permission denied error because some of the directories in Etsy are not readable by normal users now I could have ran sudo and this would work so I'll clear the screen and I'll just put sudo in front of this and then my password and you know it works just fine I don't see the errors but without sudu I'm going to see errors because I don't have permission to see everything so I expected that to happen so what we can do is handle the standard error independently of standard output so what I will do is recall this command and I will type 2 and then greater than just like that and then I'll pipe that to dev no I'll explain in a moment well press Enter and you know I didn't use sudo I did get my results here so the command worked just fine now obviously I'm missing some information because there's some directories I don't have permission to search in but I didn't get any airs why well actually standard air is designated by a - that's important to know when you want to redirect standard output standard error handles standard input you want to know what the number classification is for each and right here we can see that I was looking for - like anything that is code - I want to basically redirect that to dev null and Dev null is like this place where anything that goes into it is never seen from or heard from again it's basically the same as deleting something so if you redirect something into dev null it's just gone now similarly standard input is 0 standard output is 1 and again standard air is 2 so this allows us to do basically anything we want to separate standard output and standard error again I have this command right here I could redirect this / - let's just say errors txt for example I'll press Enter and again we get the actual results but if I kept the contents of airs we could see that those are the errors that I would have received on the command line or in standard output I would have seen those errors had I not redirected standard error somewhere else I redirected standard error to go into errors XT rather than being printed on the screen because I didn't want to see those errors I just wanted to see the good information but I still wanted to make a note of the errors in case I felt like maybe those do matter and I do want to investigate that a bit I could actually see well what wasn't included in the search and I know that because I have that in Eric's T so I can refer to that now so it's really important to know how to differentiate standard input and standard error now let's go ahead and try a different example here so I brought this back and again we are redirecting standard error again that's code 2 into air is DXT but i want to redirect standard output - let's just call it success dot txt for example I'll press ENTER and there's no output no standard output nothing and we could check the exit code here it's not zero so there was a problem we know there was a problem because the exit code is not zero there was an issue but we don't see any problems on the screen right so we can just cat errors again it's just going to contain the same text like it did before and then we can also check the contents of success and it's going to show the ones that were successful so I was able to split the errors into a file and then the successful output into its own file so I can basically separate that however I see fit you might be wondering well why does this matter and you know it's all up to you the administrator on whether or not you have a use case for this if you're writing scripts for example you might want the heirs to go into a file that you can check later because maybe you have a batch processing job that's handling a bunch of directories files or something like that but if there's any problems you want those problems to be logged somewhere so you might actually want standard air to put those in a log file of some kind and you would then be able to see what it might have a problem with so that way you don't have an issue that goes on notice so you can redirect the errors into an errors file and then have a success file that shows you the directories files or whatever you're working with that actually were handled successfully so that's why you would want to separate those and it's important to know how to do that now even though the concept of streams and output redirection and all those related things there's a relatively simple concept it does get a little bit more advanced from here so I'm going to link a how-to geek article in the description below that's actually going to give you a lot more information and you can read that article it has a ton of info about this probably more than you would ever want to know if you want to go ahead and explore this concept further but for me and for this video that does it you know how to handle standard output standard input you know what that is you know how to handle standard air you know that we have the number classification of each you understand the concept of streams if all of that is true then you are all set and you are ready to go to go on to the next video as soon as I have that uploaded I will see you there thanks so much for watching [Music]
Info
Channel: Learn Linux TV
Views: 6,108
Rating: undefined out of 5
Keywords: LearnLinux, Linux, Tutorial, Review, Howto, Guide, Distribution, Distro, Learn Linux, operating system, os, open-source, open source, gnu/linux, centos, centos 8, stdin, stderr, stdout, standard input, standard output, standard error, redirection, centos lesson, centos tutorial, streams, linux centos tutorial, linux tutorial, centos 8 server, centos 8 tutorial for beginners, tutorial centos 8, centos 8 basic commands, shell scripting for beginners, centos server administration
Id: ssosU-ad5Eo
Channel Id: undefined
Length: 18min 40sec (1120 seconds)
Published: Mon Jun 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.