 Hello and welcome this video is part of a series. This is video number two be sure to check out the previous video in series There should be a link in the description to the full playlist and I'm trying to very quickly go over some basic Shell script commands for bash And I went way longer than it last time. I do have previous videos that go into more detail I'm gonna try to be a little bit quicker with this So if I'm going too fast watch my previous series where I go a little bit slower So let's go ahead and just open up the script. We've created so far basically we're printing stuff to the screen reading stuff and then Printing that back to the screen with a few screen clears and sleeping. So real quick. We're gonna say echo Hello and It goes to the screen if I want to dump that into a file for saving so real quick by LS I'll list every File in the directory. I'm in if I say hello world if I do a greater than symbol I'm going to redirect that into a whatever file. I give it and I'll just call this my log Because I'm logging stuff now if I cat out cat will display whatever is inside a file I can say cat my log and just says hello if I go back up to here and I say hello World now it's gonna dump hello world in that file if we cut that out you can see it says hello world But we lost our first line of hello So what we need to do if we want to append to a file So we're writing to a file when we do the greater than symbol if we do greater than greater than it's going to Append to that file meaning adding a new line So here I'll just say hello John and now if I cut out that file you can see it says hello world. Hello John and now I can say hello oops Hello Tom if I cut that out. It's gonna say hello world. Hello John. Hello Tom So real quick if I was to say name or let's do the re-command read What is your name and we want to have a P here, what is your name? And we're gonna put that in the variable name. We can say Bob and then I can say echo Hey dollar sign name and I can pipe that into our log file log Now if I cat out my log You can see hey Bob's at the end and if I read name again this time I'll say Tim and And then I can pipe that into our sorry redirect that append it to our file I can now append that to the file as well So let's go ahead and go back into our script now that you know how to Write to a file which it will create a file either of these will create a file if it doesn't exist My program So again, I'm using Vim use whatever text that you're comfortable with I'm gonna clear the screen. Hello world. What is your name? Hello, whoever sleep for two seconds clear the screen How old are you and it's gonna say name is so many years old and then I'm gonna clear the screen sleep and clear the screen But let's say after getting the name the age. I want to log that to a file what I'll do here because I'll just say echo and I'll say Dollar sign name and then I'll say comma dollar sign age And I'm going to append that to a file which will call People log again the extension doesn't mean anything. It's plain text file, but that's just let you know is a log file So now I Can run my script? What is your name? I'll say Chris. What is your age in a second? 48 I'm not 48 Goodbye And I'll run that again this time. I'll say John and I'll say John is 99 And of course we're getting the same output as far as the end user is concerned This is the same script as before say Tim and I'll say Tim is 23 But now I can cat out My people log file and you can see I've logged those people's names and their ages Which is great So another way you can append to a file is using the t command t e Which is common command, which is commonly on many systems and what t does is it allows you to view? What's being added to a file and you can use t-a to append to file so again control L to clear the screen and and what we're going to do is we're going to um Pipe here, so I don't think I've talked about piping yet Basically one of the strong things about shell scripting is you can take the output of one command and do what's called piping Basically taking that and putting that into the input of another command and by doing that You're using the pipe symbol which on a quarter keyboard. It's going to be usually above your enter key It's the same key as the back slash we have to hit shift and it looks like that just straight line up and down Sometimes we'll have a little break in the middle. That's a pipe symbol So if I use the t command so again, let me say I want to echo Test and I want to put that into a file called file. I Can now cat out file. It says test if I run that again, but I'll say test this Well now it's going to say test this but you'll notice it when I'm writing that command Echoing tests, but we're not seeing Test this on the screen is going directly into that file now, but not being split screen Let's say you want to display it to the screen, but then also put it into a file at the same time Well, that's what the t command does so let's go ahead and clear the screen again And this time instead of using greater than symbols what I'm going to do is I'm going to say pipe into t And then give the file name and now when I hit enter you can see it displays that message on the screen But it also put it into the file command and if I run that again test this I'll say test this out You see it says test this out and if we can't it you can see it's now replaced that so we've written to that file We didn't append to that file if you want to use t and append to the file all you have to do is use the dash a So basically what this is doing is it's saying echo this test this out We're taking that output of test this out We're putting it's t t is the swing at the screen in this case It's going to also append it to this file So I'm gonna do that and we'll do that a couple of times and now if I cut out that file You can see that it's all there Let's go ahead and change our message just to demonstrate again, and we'll do another one with This and now if I cut out that file you can see that has appended to it So that's very useful if you want to view the output But also have it go into a file and you don't want to have to do like an echo command twice So the t command is very useful in that aspect. So again, it's a very common tool It's t to put it into a file t dash a to append to a file. So so far we have been able to print things to the screen with echo We're ready to get user input we know to take the user input print a screen put it into a file print screen and put it Into a file now let's and we've also used the cat command to view the entire file But let's say we want to look for a particular line Well, we have the rep command. Oh, which has so many uses. I'm just going to show you the very very basic Usage of it. So again, if I cat out My we'll use people log as an example here. We have this but let's say I just want a line that says John What I can do is I can say grep and then I can say in quotations. You don't need the quotations begin It's a good idea John and I can say people log So now it's going to look at the people log and only return lines that say John Let me go ahead and open up people log and I'm going to add another line. I'll say John as a different John, I guess I'll say that he's 77 now if I do grep John people log It's going to show both those lines and if I come in here and I add another line I'll add one that says Johnson. Let's give him an age to I guess If I now grab John it's gonna show the line with Johnson because it has a match Johnson has the word John in it If I just want lines to say Johnson I Can just look for Johnson and it's going to normal ones to say John now. Here's one more example Vim I'm going to say let's say this John was written with a lowercase J now if I look for John It's not going to show up because it's case sensitive if I said John With a lowercase J That one's gonna show up the other two are going to show up But if I do dash I what that's going to do is tell grep to be case insensitive Do not care about the case of the letters So now we'll find all the lines with John on it whether it's John Johnson capital J lowercase J It does not care Again, this is a very basic example Check out my my website go to films by chris comm and just type in grep I have very in-depth tutorials with grep. There's a lot of other things could do One more thing grep can do again. This is just a quick overview Let's say I wanted any line that doesn't have the word John on it I can do dash V which inverts the search So now it's showing every line that doesn't have John with a lowercase J if I do Capital J here It's going to find every line that doesn't contain John with a capital J and of course I can combine these I can say Dash I dash V so same case insensitive Display all lines that don't have John and now it's only going to display the lines that don't have John again Grep has lots of uses. This is just a quick overview of some of the more common things you might do which with grep so we've been able to Print things to screen get user input print that to the screen save it to a file Display the entire file and then find particular lines in the file One more thing I'm going to go over again. I'm going over these commands real quick But I have more thorough videos on them is the cut command if I was to say cut I can say cut with a delimiter so dash D delimiter and whatever character I want and depending on the character I usually always just do a backslash because some of the characters you might use or special characters I'm gonna say backslash comma, which I don't think you need the backslash for comma But you do for like the pipe symbol or maybe a dollar sign for example so this is saying take the file that I'm giving it and Cut it into columns based on the dot on the comma there and then I say dash F One in this case. I just want the first field so the F is for field So now I can give it the file name of people and what's gonna do is it's just gonna show that first column Okay, if I wanted to see the second column I can see say field to and now it's just showing me the ages And we can combine this with the grep command So what I could do I could say instead of doing cut and giving it that file name I can pipe into it. So as we talk about pipe, we're taking the output of one command putting into another Let's say I just want the age of Tim. I can say grep for Tim in our file of people log and I can pipe that into this So now I can see get Tim's age Obviously if there was more Tim or if there were other people with Tim in their names, we'll get more than one line Again, this is just very basic stuff Obviously each line if you're actually doing something like this Well, if you're really doing something like this you probably want to use a database But even if you were using a plain text file, you want to give each line its own unique ID of some sort We're not gonna look at this in this tutorial, but we're gonna do is here. We got Tim's age and if I want Chris's age I can say Chris and I can see Chris is 48 So again, if I got rid of the second command That's just gonna find the line that has Chris and then the cut command is saying Use the limer of comma cut it into fields in this case field 2. I got Chris's age If I did field 1 I can get Chris's name. So if I want to I can say here instead of Chris I could go okay I want to see everybody who's 48 and it will display whoever's 48. Let me go into my people file here And let's just make two people 48. So I'll make this John and Chris both 48 So here I can say fine any line that has 48 in it and I'm gonna cut it So we're only showing names. So I should theoretically get a list of people who are 48 boom so that is some quick examples again, this these are basic examples and You'll use commands similar to this in the real world But not these exact commands because for example, you would never put someone's age Into a file like this. I always say never you normally when you would put their birthdate and then use your code to calculate How old they are because your age is gonna change if my birthday is tomorrow my age is gonna change We're gonna go back and and update everyone whose birthday just happened today every day if you have a bunch of people in there No, you put their birthdate in and you calculate out from there. Anyway, so that is a Basic command a few basic commands to get you started again I'm going over these quickly showing you demonstrating how they can work I have videos more in depth go to films by Chris calm That's Chris the K there you can search through all my videos type grep type cut And you should get a list of videos that died where I go over these things in more detail I do thank you for watching and as always I hope that you have a great day