1 00:00:00,620 --> 00:00:01,850 Hello everyone. 2 00:00:01,850 --> 00:00:09,320 In this demo I'm going to talk about bash scripting and also I will show you how to run Bash scripting 3 00:00:09,680 --> 00:00:12,890 before I'm going to do the demo or run any command. 4 00:00:12,920 --> 00:00:19,340 Let's understand what is the bash and also what is the shell and what is the script. 5 00:00:19,340 --> 00:00:26,900 So let's start with the Bash Bash is a Unix shell and it's a short for porting against Shell. 6 00:00:26,930 --> 00:00:33,980 Basically, it's the command line interface, the CLI that interacts with the operating system, for 7 00:00:33,980 --> 00:00:36,830 example, the slash pen slash bash. 8 00:00:37,880 --> 00:00:39,470 Next, what is Shell? 9 00:00:39,500 --> 00:00:45,350 Shell runs Linux commands in order to interact with computers. 10 00:00:45,560 --> 00:00:52,730 The terminal that we use in Linux operating system this is the terminal right now is considered a default 11 00:00:52,730 --> 00:00:55,490 shell of the Linux operating system. 12 00:00:57,730 --> 00:00:58,330 Next. 13 00:00:58,330 --> 00:00:59,280 What is the script? 14 00:00:59,290 --> 00:01:08,320 Scripts are used to run a series of commands and can execute the commands interactively, interactively, 15 00:01:08,320 --> 00:01:09,610 one by one. 16 00:01:09,850 --> 00:01:16,030 All the commands that we run from the command line can be used in a bash scripts. 17 00:01:16,240 --> 00:01:25,270 So I hope that you guys get the idea about what is shell and what is bash and what is the script. 18 00:01:25,300 --> 00:01:32,860 Now let's do let's do a demo, an example in order to understand the these concepts. 19 00:01:32,860 --> 00:01:37,340 So I would like to run the following commands in order, so the following commands. 20 00:01:37,340 --> 00:01:38,830 So I'm going to run Echo. 21 00:01:39,700 --> 00:01:45,730 Welcome to our xxA course. 22 00:01:49,800 --> 00:01:56,770 Marked and I hit close the the double quotation hit enter as we can see over here. 23 00:01:56,920 --> 00:01:59,530 After I run this command, it showed me this message. 24 00:01:59,620 --> 00:02:01,360 So the second command also I'm going to run. 25 00:02:01,390 --> 00:02:02,870 Who am I? 26 00:02:02,890 --> 00:02:09,039 And also, if I would like to run the the command date to see what is the date today. 27 00:02:09,130 --> 00:02:13,070 And also, I would like to run the calendar here. 28 00:02:13,090 --> 00:02:13,940 Perfect. 29 00:02:13,960 --> 00:02:20,860 So what I did here, when I run this, this commands why I run these commands, because I would like 30 00:02:20,860 --> 00:02:26,530 to interact with the computer, which is the default shell of the Linux operating system. 31 00:02:27,010 --> 00:02:32,200 So as we can see over here, so this is the default shell of the Linux operating system. 32 00:02:32,200 --> 00:02:36,350 And what we and what is the what the shell did. 33 00:02:36,370 --> 00:02:43,090 So after I run those commands, the shell is going to retrieve those commands. 34 00:02:43,090 --> 00:02:51,140 So the echo home I and the date and the calendar and also is going to display the outcome on the screen, 35 00:02:51,140 --> 00:02:53,930 as we can see here on the terminal. 36 00:02:55,250 --> 00:03:03,950 So if I would like to know what is the default interpreter or the shell for the current user? 37 00:03:04,490 --> 00:03:10,700 So I can run the command echo, then dollar sign, uppercase shell. 38 00:03:11,420 --> 00:03:19,220 So the reason to run this command, because I would like to know what is the current shell or the. 39 00:03:20,270 --> 00:03:24,680 The default interpreter for this current user. 40 00:03:24,680 --> 00:03:30,110 So we're going to use this one, the slash pen slash bash when we're going to write the script. 41 00:03:30,110 --> 00:03:33,140 So I'm going to explain it when we're going to write the script. 42 00:03:33,140 --> 00:03:39,800 So next, if I would like to know where is the bash Interpreter is located? 43 00:03:40,850 --> 00:03:44,240 So I run the command which bash. 44 00:03:44,270 --> 00:03:47,930 I hit enter and is located under the slash user. 45 00:03:47,930 --> 00:03:49,730 Slash pen slash bash. 46 00:03:49,880 --> 00:03:51,920 This is the bash command. 47 00:03:52,400 --> 00:03:54,650 Now let's write the first script. 48 00:03:54,650 --> 00:04:01,040 So first I'm going to create a new directory in order to save all the bash scripts under this directory. 49 00:04:01,040 --> 00:04:10,040 So I'm going to call this directory bash script, CD Bash dash scripts. 50 00:04:10,040 --> 00:04:13,640 We are right now inside this directory. 51 00:04:14,660 --> 00:04:15,860 And next. 52 00:04:16,040 --> 00:04:28,140 Now I would like to create the first script so I can use either the nano editor or the Vim sub to you. 53 00:04:28,160 --> 00:04:28,850 I will go. 54 00:04:29,120 --> 00:04:30,920 For me, I prefer to go with the vim. 55 00:04:31,080 --> 00:04:34,850 So vim then the name of the script. 56 00:04:35,960 --> 00:04:37,790 And it has to be. 57 00:04:39,400 --> 00:04:48,400 Uh, so the extension or the type of this one has to be a shell, a bash script file or a script file. 58 00:04:48,400 --> 00:04:50,590 So it has to end with ssh. 59 00:04:51,620 --> 00:04:56,690 So the name of the script then that is H hit enter. 60 00:04:56,840 --> 00:04:59,390 Then I in order to insert. 61 00:04:59,420 --> 00:05:05,120 Now is the first line that I'm going to write is the shebang line. 62 00:05:05,390 --> 00:05:12,710 And also it's called hash bang line because of the two character with the hash and the bang, then slash 63 00:05:12,710 --> 00:05:14,740 bin slash bash. 64 00:05:14,750 --> 00:05:22,640 As you guys remember what I said about the well, the default interpreter or the shell for the current 65 00:05:22,640 --> 00:05:23,090 user. 66 00:05:23,090 --> 00:05:29,360 So this is the default interpreter or the shell for the current user slash pen slash bash. 67 00:05:29,930 --> 00:05:37,970 Then the second line, this is the best practice usually as a programmer or if you would like to write 68 00:05:37,970 --> 00:05:41,240 a script, you write a comment at the top. 69 00:05:41,240 --> 00:05:43,640 So hash then this is a comment. 70 00:05:43,640 --> 00:05:47,180 So the script, when we're going to run the script, so it's going to ignore this line. 71 00:05:47,180 --> 00:05:49,700 So it's a best practice to write a comment. 72 00:05:49,700 --> 00:05:51,900 What is the purpose of this script? 73 00:05:51,900 --> 00:05:55,080 So I'm going to type this is my first 74 00:05:56,820 --> 00:05:58,530 script. 75 00:05:58,890 --> 00:05:59,850 Perfect. 76 00:06:02,380 --> 00:06:04,360 So this is a comment. 77 00:06:04,360 --> 00:06:06,610 And then what is next? 78 00:06:06,640 --> 00:06:13,570 The next line I'm going to write the Or type the commands that I already run them before. 79 00:06:13,570 --> 00:06:14,220 So Echo. 80 00:06:16,690 --> 00:06:24,940 Echo as you can notice that once we type the comment so that the command it will the and and and the 81 00:06:24,940 --> 00:06:27,010 script here, it will change the color. 82 00:06:27,040 --> 00:06:34,360 Next I'm going to type the message welcome to is a. 83 00:06:36,600 --> 00:06:40,440 Course and exclamation mark. 84 00:06:40,470 --> 00:06:44,370 Then we close with the double quotation. 85 00:06:44,370 --> 00:06:50,030 So as the reason I use the double quotation because I'm going to use the exclamation mark or the bank 86 00:06:50,040 --> 00:06:50,850 over there. 87 00:06:50,880 --> 00:06:53,780 Then after that, I'm going to type the second command. 88 00:06:53,790 --> 00:06:54,570 Who am I? 89 00:06:54,840 --> 00:06:55,850 Date? 90 00:06:56,160 --> 00:06:57,630 Then calendar. 91 00:06:58,080 --> 00:07:03,410 So what I did, I wrote four commands and I'm going to save the file. 92 00:07:03,420 --> 00:07:08,880 So Colon X, then the bank or the exclamation mark hit enter. 93 00:07:09,480 --> 00:07:19,050 Now, after after I wrote the first script, what I'm going to do, I would like to run the script from 94 00:07:19,050 --> 00:07:20,130 the command line. 95 00:07:20,970 --> 00:07:24,570 With like from the command line with the bash interpreter. 96 00:07:24,570 --> 00:07:25,650 So this is the shell. 97 00:07:26,070 --> 00:07:33,870 So what I'm going to do right now, if I will type only the name of the script file and I hit enter 98 00:07:33,920 --> 00:07:35,730 is not going to work. 99 00:07:36,600 --> 00:07:45,630 So in order to solve this problem, we need to type either bash then the name of the. 100 00:07:46,980 --> 00:07:48,030 Script file. 101 00:07:48,060 --> 00:07:49,430 Then hit enter. 102 00:07:49,440 --> 00:07:56,250 Or I can do another option because we are in the current directory here. 103 00:07:56,280 --> 00:08:06,390 This is the Bash Dash scripts and the script is inside this directory so I can do the dot and the slash. 104 00:08:07,150 --> 00:08:09,640 Because we are in the current directory right now. 105 00:08:09,640 --> 00:08:12,580 So we are allowed to do that in order to run the script. 106 00:08:12,700 --> 00:08:20,560 But if even though if I will do that and I type or I hit enter. 107 00:08:21,480 --> 00:08:24,660 Also is going to show me an error. 108 00:08:24,660 --> 00:08:25,550 Permission denied. 109 00:08:25,560 --> 00:08:25,920 So hit. 110 00:08:25,920 --> 00:08:26,430 Enter. 111 00:08:27,000 --> 00:08:28,230 Permission denied. 112 00:08:28,260 --> 00:08:31,140 Because I need to change the permission in order. 113 00:08:31,140 --> 00:08:31,440 Why? 114 00:08:31,440 --> 00:08:33,510 I need to change the permission of this file. 115 00:08:33,510 --> 00:08:34,130 So ls. 116 00:08:34,130 --> 00:08:35,280 Let me see. 117 00:08:35,610 --> 00:08:37,020 LSD Welcome. 118 00:08:37,740 --> 00:08:41,190 As we can see over here, we need to change the permission. 119 00:08:42,059 --> 00:08:47,250 In order to allow the script to be executed executable for this user. 120 00:08:47,250 --> 00:08:52,830 We don't have an execute permission for this script, so it's easy. 121 00:08:52,830 --> 00:08:56,670 So the only thing is change mode plus X. 122 00:08:56,670 --> 00:09:05,340 So I'm going to do give an execute permission to to the user and the group and the other then the name 123 00:09:05,370 --> 00:09:12,150 of the script file hit Enter lSl Welcome, hit Enter. 124 00:09:12,150 --> 00:09:21,570 As we can see here, we assign an execute permission to this script file. 125 00:09:21,570 --> 00:09:24,030 And also we can notice that the color. 126 00:09:24,750 --> 00:09:27,380 Has changed here to green. 127 00:09:27,390 --> 00:09:29,940 It means this is a script file. 128 00:09:30,090 --> 00:09:32,610 Now, if I will do like that bash. 129 00:09:32,970 --> 00:09:33,930 Welcome. 130 00:09:34,500 --> 00:09:35,150 Hit enter. 131 00:09:35,160 --> 00:09:39,930 As we can see here, remember the first message cat? 132 00:09:40,110 --> 00:09:42,000 So that echo. 133 00:09:42,030 --> 00:09:47,480 Will come to our CSR course and the whom I join. 134 00:09:47,490 --> 00:09:51,030 Let me show the content of this file. 135 00:09:51,480 --> 00:09:54,180 So as I said, this is the shebang line. 136 00:09:55,940 --> 00:10:00,980 The shell or the default for the default interpreter for this current user. 137 00:10:01,310 --> 00:10:06,860 This is the comment, as I said, that when we run the script is going to ignore that. 138 00:10:06,860 --> 00:10:12,860 It's just it's a good practice to to to do it in order to see what is the purpose of your script. 139 00:10:13,310 --> 00:10:15,680 And this is the first line. 140 00:10:16,850 --> 00:10:20,120 Welcome to our course. 141 00:10:20,120 --> 00:10:21,110 Then who am I? 142 00:10:21,470 --> 00:10:22,430 I'm user John. 143 00:10:22,850 --> 00:10:27,140 Then here is the date and here is the calendar. 144 00:10:27,860 --> 00:10:28,700 Perfect. 145 00:10:28,700 --> 00:10:30,170 So far, so good. 146 00:10:30,950 --> 00:10:34,250 Now let's move on to. 147 00:10:34,280 --> 00:10:35,630 Well, I will show you another. 148 00:10:35,660 --> 00:10:40,760 We use what bash we can do also the another one. 149 00:10:41,150 --> 00:10:41,630 What? 150 00:10:41,630 --> 00:10:45,560 I said that because we are right now in the current directory. 151 00:10:46,240 --> 00:10:54,070 Since we are here inside this directory, we can run the bash script by typing the dot and slash and 152 00:10:54,100 --> 00:10:56,770 hit enter as we can see over here. 153 00:10:56,800 --> 00:11:00,370 This is the outcome of the script file. 154 00:11:03,090 --> 00:11:06,840 As I mentioned before, just pay attention to it. 155 00:11:06,840 --> 00:11:14,100 So if I if I try to run the script by only typing, only by, by by typing only, the name is not going 156 00:11:14,100 --> 00:11:14,560 to work. 157 00:11:14,580 --> 00:11:16,770 However, there is a way to do it. 158 00:11:17,500 --> 00:11:20,470 So I can avoid that so I don't have to type either. 159 00:11:20,470 --> 00:11:24,820 Bash Then the name of the script or if I do. 160 00:11:25,510 --> 00:11:30,400 Dot, then slash then the name of the script file. 161 00:11:30,430 --> 00:11:32,170 There is a way to do it. 162 00:11:32,200 --> 00:11:41,680 I can add the path of the Bash Scripts directory to the set of directories that are saved in the path 163 00:11:41,680 --> 00:11:42,610 variables. 164 00:11:42,610 --> 00:11:52,360 So what I do like if I type the command echo, then dollar sign uppercase path hit enter. 165 00:11:52,360 --> 00:11:54,340 So as we can see over here. 166 00:11:55,220 --> 00:11:57,440 The colon here separates the basi. 167 00:11:57,470 --> 00:11:59,750 This is directory. 168 00:12:00,260 --> 00:12:02,090 And so here is the colon. 169 00:12:02,090 --> 00:12:03,920 This is separates the directory. 170 00:12:04,430 --> 00:12:07,850 So it's going to separate the path of every directory. 171 00:12:07,850 --> 00:12:09,890 And this is the second directory here. 172 00:12:10,040 --> 00:12:14,360 And this one here is the third, fourth, fifth, sixth. 173 00:12:14,360 --> 00:12:18,050 And the colon is going to separate the path of every directory. 174 00:12:18,260 --> 00:12:28,790 So whenever we run a command line from anywhere in the system, for example, Cat or Echo or fdisk whatever, 175 00:12:28,790 --> 00:12:29,750 we're going to run. 176 00:12:31,120 --> 00:12:32,470 Anywhere in the terminal. 177 00:12:32,740 --> 00:12:40,720 The system is going to search for the executable files that are stored in the bin directories. 178 00:12:41,080 --> 00:12:45,250 Here is the bin directory here in order to run those commands. 179 00:12:45,430 --> 00:12:51,160 So basically, the path of all the all the commands here. 180 00:12:51,460 --> 00:12:58,000 It's it's under that what under the bin directories so it doesn't matter if you guys notice when we 181 00:12:58,000 --> 00:13:03,820 are inside directories or we are for example if I go back here, it doesn't matter. 182 00:13:04,480 --> 00:13:12,430 So here right now, under slash home slash zone, if I write for example, that so so the what is going 183 00:13:12,430 --> 00:13:18,200 to do so the system is going to search for the executable files that are stored in the bin directories. 184 00:13:18,220 --> 00:13:20,920 Here is the bin directories in order to run those commands. 185 00:13:21,400 --> 00:13:23,110 Let's go inside the file again. 186 00:13:24,130 --> 00:13:25,000 Hit enter. 187 00:13:25,150 --> 00:13:32,660 So as I said, if I would like to run the bash scripts from anywhere, so so far I only have one bash 188 00:13:32,660 --> 00:13:36,830 script here or bash file a script file. 189 00:13:36,950 --> 00:13:42,140 So if I would like to run the bash script or the script file from anywhere in the terminal, I can add 190 00:13:42,140 --> 00:13:43,190 the path. 191 00:13:43,880 --> 00:13:44,480 BWT. 192 00:13:45,020 --> 00:13:45,800 The path. 193 00:13:45,800 --> 00:13:46,910 This is the path. 194 00:13:47,680 --> 00:13:49,860 Of the bash script directory. 195 00:13:49,870 --> 00:13:52,990 This is the bash script to the. 196 00:13:53,960 --> 00:13:54,920 I just said Echo. 197 00:13:55,130 --> 00:13:55,850 Where is it? 198 00:13:55,850 --> 00:13:56,360 Here. 199 00:13:58,190 --> 00:13:58,940 To where? 200 00:13:59,450 --> 00:14:00,080 To here. 201 00:14:00,080 --> 00:14:01,610 To the bath variables. 202 00:14:02,410 --> 00:14:05,410 So I can do that as I said that BWT. 203 00:14:05,440 --> 00:14:13,450 So I'm going to copy this one and then I'm going to type the command export bath. 204 00:14:13,510 --> 00:14:21,730 So this is the command that we need to run in order to save the bath of this batch directory to. 205 00:14:23,050 --> 00:14:26,140 To the to the to the bath variables over here. 206 00:14:26,140 --> 00:14:36,880 So in order to avoid, uh, you know, typing the bash before the script file or the dot slash before 207 00:14:36,880 --> 00:14:46,270 the name of the script file, then after that equal dollar sign and uppercase bath then. 208 00:14:47,560 --> 00:14:53,200 Calling, then we're going to we're going to copy this here, the bath here. 209 00:14:55,550 --> 00:14:57,870 I'm going to just delete this one here. 210 00:14:57,890 --> 00:14:58,730 Perfect. 211 00:14:59,060 --> 00:15:01,610 So as we can see here, the command export. 212 00:15:01,610 --> 00:15:08,750 So the command here, the the command, the export command is going to add the path of the bash script. 213 00:15:08,750 --> 00:15:11,210 So bath equal. 214 00:15:12,730 --> 00:15:20,710 Dollar sign, bath, colon, then slash home slash join slash bash scripts and I hit enter. 215 00:15:21,940 --> 00:15:24,850 Now, if I would type welcome 216 00:15:27,700 --> 00:15:30,130 that is it hit enter. 217 00:15:30,130 --> 00:15:33,220 As we can see here, we can see the magic over here. 218 00:15:33,550 --> 00:15:36,190 We don't have to write the bash. 219 00:15:36,220 --> 00:15:42,670 Then the name of the script or script file or we do do that slash. 220 00:15:43,870 --> 00:15:45,100 But there is. 221 00:15:45,100 --> 00:15:46,150 Okay, there is a quick note. 222 00:15:46,150 --> 00:15:47,650 So I kind of written about it. 223 00:15:47,650 --> 00:15:50,410 So we need to run this command again. 224 00:15:51,420 --> 00:15:52,470 Every like. 225 00:15:53,430 --> 00:15:54,020 So what? 226 00:15:54,030 --> 00:15:59,130 I mean, like we need like if I exit from the terminal and I open a new terminal. 227 00:16:00,360 --> 00:16:02,760 I need to run this command again. 228 00:16:03,810 --> 00:16:07,950 So in order to avoid that, I need to save it permanently. 229 00:16:09,440 --> 00:16:12,740 I may like to save this line permanently. 230 00:16:14,090 --> 00:16:15,650 In the file. 231 00:16:15,650 --> 00:16:16,280 Let me show you. 232 00:16:16,310 --> 00:16:17,150 What is the file? 233 00:16:17,180 --> 00:16:17,690 CD. 234 00:16:19,280 --> 00:16:20,540 Go back here. 235 00:16:20,960 --> 00:16:21,460 Clear. 236 00:16:21,500 --> 00:16:24,350 LS ls dash la. 237 00:16:24,410 --> 00:16:33,200 So even is going to show the hidden directories so you can save it over here if you want to use it for 238 00:16:33,200 --> 00:16:34,940 this term or for this user. 239 00:16:35,680 --> 00:16:44,230 So in this file VM that bash each enter, we can save it over here. 240 00:16:44,620 --> 00:16:45,760 Type here 241 00:16:49,360 --> 00:16:51,220 and escape. 242 00:16:51,940 --> 00:16:53,000 Save the file. 243 00:16:53,020 --> 00:16:53,890 Perfect. 244 00:16:54,220 --> 00:16:59,380 After we do that, we also we need to run in order to update that. 245 00:17:00,280 --> 00:17:07,720 In order to update this file, we need to type source dot bash args. 246 00:17:10,020 --> 00:17:18,660 And if I were going to see, okay, so we are not even inside that the bash that Dash Scripts directory. 247 00:17:18,660 --> 00:17:27,569 So if I'm going to type welcome that it hit enter as we can see here, here is the magic over here. 248 00:17:27,869 --> 00:17:33,150 We just typed only the name. 249 00:17:34,510 --> 00:17:38,710 And the system or the interpreter show us the outcome. 250 00:17:40,610 --> 00:17:43,130 Okay, let's conclude our session. 251 00:17:43,160 --> 00:17:46,040 Thank you and see you in the next lesson.