1 00:00:00,090 --> 00:00:06,270 Hello the beautiful people and welcome to this very exciting section of the course all about bash scripts. 2 00:00:06,270 --> 00:00:10,470 And in the next few videos you're going to learn how you can create your own bash scripts, which are 3 00:00:10,470 --> 00:00:14,640 incredibly useful tools that will allow you to automate your workflows. 4 00:00:14,640 --> 00:00:19,920 Now, learning about Bash script is a massive field in and of itself, and it would require its own 5 00:00:19,920 --> 00:00:21,810 course to cover properly. 6 00:00:21,810 --> 00:00:26,070 But by the end of the next few videos, you're going to have the fundamental understanding required 7 00:00:26,070 --> 00:00:32,460 about how batch scripts work and how to create them so that you can begin automating your workflows 8 00:00:32,460 --> 00:00:35,040 using the commands that you've learned so far in the course. 9 00:00:35,040 --> 00:00:40,560 So this is going to be an incredible and incredible piece of learning for you to let you understand, 10 00:00:40,880 --> 00:00:45,780 like what bash scripts are, how to use them, and how they basically work. 11 00:00:46,080 --> 00:00:47,880 So it's going to be very, very exciting. 12 00:00:47,880 --> 00:00:50,610 I hope you're ready to gain some serious superpowers. 13 00:00:50,610 --> 00:00:52,410 So let's not let's not mess around anymore. 14 00:00:52,440 --> 00:00:56,370 Let's go jump right in and start making our very first batch script. 15 00:00:56,850 --> 00:00:57,570 All right. 16 00:00:57,570 --> 00:01:00,270 So let's go ahead and make a simple batch script. 17 00:01:00,270 --> 00:01:03,180 Now, to do that, all we need to do is just make a text file. 18 00:01:03,180 --> 00:01:10,170 So to do that, let's go ahead and open up Nano on our desktop and make a file called as script dot 19 00:01:10,170 --> 00:01:11,040 S-H. 20 00:01:11,040 --> 00:01:17,190 Okay, so all I'm doing here, when I give the file the dot S-H file file extension is just making it 21 00:01:17,190 --> 00:01:23,100 easier for us to see that this is a bash script with S-H being short for for the bash shell. 22 00:01:23,100 --> 00:01:23,610 Okay. 23 00:01:23,610 --> 00:01:29,040 So if I open that and press control and oh, to save it, you'll see that the script has popped up on 24 00:01:29,040 --> 00:01:29,400 the left. 25 00:01:29,400 --> 00:01:33,210 And if I just close that and clear the screen, we've now got this file. 26 00:01:33,300 --> 00:01:35,370 Now let's take a closer look at this file. 27 00:01:35,370 --> 00:01:39,540 Let's see if it's anything special to the shell using the file command. 28 00:01:39,540 --> 00:01:45,060 And if I do file our scripts page, we see that it's not it's nothing special as far as the shell is 29 00:01:45,060 --> 00:01:45,540 concerned. 30 00:01:45,540 --> 00:01:47,970 It's just an empty file. 31 00:01:47,970 --> 00:01:53,640 It doesn't matter whether this was dot stage or dot pablum or whatever, it's just an empty file. 32 00:01:53,640 --> 00:01:54,100 Okay. 33 00:01:54,120 --> 00:02:00,690 So with that piece of information, let's go ahead and open our script and let's do the most important 34 00:02:00,690 --> 00:02:07,380 thing that you need to do whenever you write a new bash script, you need to tell it that this is actually 35 00:02:07,380 --> 00:02:10,289 a special file and that this file isn't just a text file. 36 00:02:10,289 --> 00:02:11,400 It needs to be read. 37 00:02:11,400 --> 00:02:13,650 Using a certain interpreter. 38 00:02:13,710 --> 00:02:20,610 And an interpreter is something that will read a file and interpret each of the lines in that file as 39 00:02:20,610 --> 00:02:22,470 a certain type of code. 40 00:02:22,920 --> 00:02:28,890 So instead of just reading Cat, okay, if I told this that this is a bash script, it would read this 41 00:02:28,890 --> 00:02:31,860 as the cat command instead of just cat as in, you know, the animal. 42 00:02:32,310 --> 00:02:36,780 If I read, if I typed echo, it would interpret this file as the echo command. 43 00:02:36,810 --> 00:02:41,550 It wouldn't just interpret it as an echo that you hear in, you know, when you're in a cave, when 44 00:02:41,550 --> 00:02:44,760 you show it into into a hall or somewhere like made of stone. 45 00:02:44,760 --> 00:02:45,140 Okay. 46 00:02:45,180 --> 00:02:47,580 So this is the important part. 47 00:02:47,610 --> 00:02:49,530 We need to tell it that this isn't a normal text file. 48 00:02:49,530 --> 00:02:54,720 It needs to be interpreted as code and more specifically as a bash script. 49 00:02:54,720 --> 00:02:58,260 So to do that, you need to first type what's called the shebang. 50 00:02:58,260 --> 00:03:05,820 Now the shebang is a hash, so and then a bang, an exclamation mark and a hash symbol. 51 00:03:05,850 --> 00:03:07,860 Then an exclamation mark is called the shebang. 52 00:03:07,860 --> 00:03:10,770 And then you type the path to the interpreter. 53 00:03:10,770 --> 00:03:15,060 Now the bash interpreter is at slash bin slash bash. 54 00:03:15,150 --> 00:03:17,100 So this is our first line. 55 00:03:17,100 --> 00:03:19,170 We type the shebang, hash bang. 56 00:03:19,170 --> 00:03:23,040 Okay, then slash bin slash bash. 57 00:03:23,040 --> 00:03:28,140 Now that that slash bin slash bash is the path to our bash interpreter. 58 00:03:28,140 --> 00:03:33,450 And we can see that if I close out of Nano by using the which command and typing bash and we see that 59 00:03:33,450 --> 00:03:37,650 we get slash B and slash bash as the path to the the bash shell. 60 00:03:37,680 --> 00:03:37,990 Okay. 61 00:03:38,130 --> 00:03:42,510 So what we're saying here is, hey, this isn't a normal text file. 62 00:03:42,510 --> 00:03:49,890 You need to interpret this file using the bash, the bash shell interpreter, run this as a bash shell 63 00:03:49,890 --> 00:03:50,430 script. 64 00:03:50,430 --> 00:03:54,050 And the key is this line needs to be the first line. 65 00:03:54,050 --> 00:03:55,050 It cannot be the second line. 66 00:03:55,050 --> 00:03:56,790 It has to be the absolute first line. 67 00:03:56,790 --> 00:03:58,500 And there can't be any spaces on the line. 68 00:03:58,500 --> 00:03:59,970 So you finish the line right away. 69 00:04:00,420 --> 00:04:05,490 And when we save that now if we look at the script with the with the file command, we see that it is 70 00:04:05,490 --> 00:04:09,840 now a shell script, in fact, a born again shell script. 71 00:04:09,840 --> 00:04:11,700 Now the born again shell is. 72 00:04:11,700 --> 00:04:14,370 BASH So you've got born again shell. 73 00:04:14,700 --> 00:04:16,890 Born again shell is. 74 00:04:16,890 --> 00:04:17,649 BASH okay. 75 00:04:17,760 --> 00:04:24,330 So this is saying it's a bash shell script and it knows that because it's got this this shebang line. 76 00:04:24,330 --> 00:04:28,260 Now, if you know anything about Python programming, you'll know that the Python programming language 77 00:04:28,260 --> 00:04:33,690 is also an interpreted programming language and the command to run something as a Python script on, 78 00:04:33,690 --> 00:04:38,370 on, on Linux is Python three for Python three. 79 00:04:38,370 --> 00:04:44,550 So if we type A which Python three, it'll tell us the path to the, to the Python three interpreter 80 00:04:44,550 --> 00:04:45,510 Just like it did for the bash. 81 00:04:45,510 --> 00:04:51,720 Interpreter And if I copy that, open up our script again and I write the shebang line again. 82 00:04:51,720 --> 00:04:53,940 I write shebang, so shebang. 83 00:04:53,940 --> 00:04:56,880 And then I paste in there the path to the python interpreter. 84 00:04:56,880 --> 00:04:59,700 So slash user slash B and slash python three and save the. 85 00:05:00,560 --> 00:05:06,050 If I now look at our script, you can see that the shell sees that this is now a Python script. 86 00:05:06,050 --> 00:05:10,790 So that's how you would do this kind of stuff if you wanted to write a script in the Python programming 87 00:05:10,790 --> 00:05:11,300 language. 88 00:05:11,300 --> 00:05:17,000 But because we want to write a bash script, we're just going to give it the path to the to the bash 89 00:05:17,420 --> 00:05:21,170 interpreter, which is that slash pin, slash bash, save that. 90 00:05:21,170 --> 00:05:25,760 And then when we look, we see that it's again a born again shell script, a bash script. 91 00:05:26,390 --> 00:05:26,640 Okay. 92 00:05:26,720 --> 00:05:28,640 So with that important concept, understood. 93 00:05:28,640 --> 00:05:31,010 Let's go back to editing our bash script in Nano. 94 00:05:31,010 --> 00:05:32,990 So let's clear the screen and edit in Nano. 95 00:05:33,020 --> 00:05:37,820 Now, anything that we write in here with this special line added, anything that we write below, it 96 00:05:37,820 --> 00:05:41,720 will now be interpreted as they do in the normal shell. 97 00:05:41,720 --> 00:05:50,690 So for example, if I write Echo and then something like Hello World, this line will be interpreted 98 00:05:50,690 --> 00:05:51,530 as a command. 99 00:05:51,530 --> 00:05:54,380 It's as if I just typed echo hello world in my shell. 100 00:05:54,380 --> 00:05:55,700 So let's see this in action. 101 00:05:55,700 --> 00:05:58,580 Okay, let's save the file using control and. 102 00:05:58,580 --> 00:06:06,800 Oh, and now if we type bash our script, we see that it's run hello world. 103 00:06:06,800 --> 00:06:07,580 How cool is that? 104 00:06:07,580 --> 00:06:07,790 Right. 105 00:06:07,790 --> 00:06:10,310 So it ran the commands that are inside there. 106 00:06:10,310 --> 00:06:14,510 Now we can do whatever we want in our script, so let's just try and add more stuff. 107 00:06:14,510 --> 00:06:20,690 Okay, let's, let's add, let's open it up with Nano and now let's clear out this echo thing. 108 00:06:20,690 --> 00:06:25,250 Let's just get rid of this using the cut command, the control k I just got rid of the whole line. 109 00:06:25,250 --> 00:06:27,260 So that's a nice little shortcut for deleting a line. 110 00:06:27,290 --> 00:06:29,930 Now let's actually use in here. 111 00:06:29,930 --> 00:06:35,030 Let's, let's try and create a file a folder called Magic on our desktop. 112 00:06:35,120 --> 00:06:43,580 Now inside the magic folder, we're going to make 100 files and then we'll use the LS command to get 113 00:06:43,580 --> 00:06:49,100 information about each of those files and save it to save all that information to a log file on our 114 00:06:49,100 --> 00:06:49,850 desktop. 115 00:06:50,510 --> 00:06:52,460 So what command? 116 00:06:52,490 --> 00:06:53,450 Here's a test for you. 117 00:06:53,450 --> 00:06:58,340 Now what command would we need to create a folder called Magic on our desktop? 118 00:06:59,930 --> 00:07:02,900 Well the mkdir command write the mkdir command. 119 00:07:02,900 --> 00:07:11,240 So let's make a directory, let's type mkdir on our desktop, let's make a file called Magic. 120 00:07:11,540 --> 00:07:14,000 So that's going to be our magic directory. 121 00:07:14,270 --> 00:07:16,760 Now we need to make 100 files in it. 122 00:07:16,760 --> 00:07:18,640 So how how might we do that? 123 00:07:18,650 --> 00:07:25,160 Well, we might move into the directory, so we might move into that magic directory using CD. 124 00:07:25,190 --> 00:07:32,120 Then we might touch, I guess, 100 files of file 1 to 100. 125 00:07:32,540 --> 00:07:33,710 That might be what we do. 126 00:07:34,040 --> 00:07:34,790 So far, so good. 127 00:07:34,790 --> 00:07:37,580 So we've created a file called a folder called Magic. 128 00:07:37,580 --> 00:07:40,700 We've gone into the folder and then we've created 100 files. 129 00:07:41,420 --> 00:07:46,520 Now let's, let's like create, let's use the LS command to get data about each of those files. 130 00:07:46,520 --> 00:07:54,950 So if we use the LS command and we, let's say give it, let's say we want to use long form information 131 00:07:54,950 --> 00:08:02,210 and human readable information and save all that to a file on our desktop called Magic Log. 132 00:08:04,730 --> 00:08:06,170 And let's leave it there. 133 00:08:06,170 --> 00:08:06,370 Okay. 134 00:08:06,410 --> 00:08:12,110 So what we've done, we've used the Mac, we've made a directory on our desktop, we have changed into 135 00:08:12,110 --> 00:08:12,830 that directory. 136 00:08:12,830 --> 00:08:20,630 We've created 100 files and then we use the ls l command on our I should actually. 137 00:08:20,630 --> 00:08:26,120 Good, good thing I checked, I should check our magic folder and then redirect all that information 138 00:08:26,120 --> 00:08:28,730 into a file called Magic Dot log. 139 00:08:28,730 --> 00:08:29,900 So if I save that there. 140 00:08:31,010 --> 00:08:31,790 Close it. 141 00:08:31,790 --> 00:08:36,950 And now we we can see the contents of this file using the cat command just like any other file. 142 00:08:36,950 --> 00:08:39,200 We can just cap that out and we can see the contents. 143 00:08:40,370 --> 00:08:43,760 But now let's go ahead and actually run it. 144 00:08:44,120 --> 00:08:48,050 So if we try to bash our our script. 145 00:08:48,790 --> 00:08:49,240 Wow. 146 00:08:49,270 --> 00:08:50,080 Did you see that? 147 00:08:50,080 --> 00:08:52,420 We managed to create a folder on the desktop. 148 00:08:52,450 --> 00:08:54,880 Inside there there are now 100 files. 149 00:08:54,880 --> 00:08:57,760 And if we look in this magic log file. 150 00:08:59,380 --> 00:09:06,040 We see detailed information about every single one of the files inside that folder, just like we told 151 00:09:06,040 --> 00:09:06,880 the script to do. 152 00:09:06,910 --> 00:09:08,800 So isn't that absolutely awesome? 153 00:09:08,980 --> 00:09:11,180 So this is how bad scripts work. 154 00:09:11,200 --> 00:09:16,540 You basically type in there the commands that you want it to do, just as if you were typing them in 155 00:09:16,570 --> 00:09:17,950 in the in the normal shell. 156 00:09:17,980 --> 00:09:24,270 But the benefit is we now have this file that we can keep and do stuff with whenever we want. 157 00:09:24,460 --> 00:09:27,370 So you can actually save that for later. 158 00:09:27,790 --> 00:09:30,110 Now, this should be a moment of amazement for you. 159 00:09:30,130 --> 00:09:31,450 So think about this for a moment. 160 00:09:31,900 --> 00:09:35,770 In Linux, you can control anything using the command line, whatever you want. 161 00:09:35,800 --> 00:09:39,100 What if you want to change, whether it be your desktop background, whether it be your font color, 162 00:09:39,100 --> 00:09:43,690 whether it be, you know, messages that pop up when you log in, you can change anything using the 163 00:09:43,690 --> 00:09:48,970 command line and you can write scripts to hold command line commands. 164 00:09:49,120 --> 00:09:55,000 So therefore you can write scripts that can do anything on your computer and save them for later so 165 00:09:55,000 --> 00:09:56,710 that you can run them whenever you want. 166 00:09:56,710 --> 00:10:02,260 And batch scripts are therefore part of what gives you complete customizability over your Linux machine. 167 00:10:02,260 --> 00:10:05,830 And they are incredibly, incredibly powerful tools. 168 00:10:05,950 --> 00:10:10,330 So you've seen in this video how to make a bash script, but in the next video, what we're going to 169 00:10:10,330 --> 00:10:14,530 do is we're going to do it all over again and we're going to show you how you can write a batch script 170 00:10:14,530 --> 00:10:16,750 to make regular backups for you. 171 00:10:16,750 --> 00:10:18,160 So that's going to be very, very cool. 172 00:10:18,160 --> 00:10:21,940 And we're going to show you how to run it in a much more convenient way as well. 173 00:10:21,940 --> 00:10:23,470 So I'm going to break the video here. 174 00:10:24,010 --> 00:10:28,120 And in the next video we're going to pick it up again by showing you how to make a batch script to do 175 00:10:28,120 --> 00:10:28,990 backups.