1 00:00:00,000 --> 00:00:12,400 As I want to be to the point on shell scripting, I'm going to organize this entire lesson around 2 00:00:12,400 --> 00:00:15,920 a couple of shell scripting examples. 3 00:00:15,920 --> 00:00:17,280 Here is example number one. 4 00:00:17,280 --> 00:00:19,600 For your convenience, I've put it in a slide. 5 00:00:19,600 --> 00:00:21,760 I'm going to write it for you. 6 00:00:21,760 --> 00:00:26,440 And while I write, I will explain what it is doing and what these different components 7 00:00:26,440 --> 00:00:27,440 are all about. 8 00:00:28,440 --> 00:00:32,599 OK, let's give it a very original name, script1. 9 00:00:32,599 --> 00:00:36,720 Notice that in a shell script name, you don't need an extension. 10 00:00:36,720 --> 00:00:41,279 And that is because the Linux operating system is very capable of detecting the contents 11 00:00:41,279 --> 00:00:45,320 of any file using the file command. 12 00:00:45,320 --> 00:00:50,040 Now every shell script should start with what you call the shebang. 13 00:00:50,040 --> 00:00:55,160 That's calling the interpreter for the subshell that is running your script. 14 00:00:55,160 --> 00:00:59,360 The reason is that not always bash is used. 15 00:00:59,360 --> 00:01:03,520 Bash is often installed, but you might be running a script from a different shell. 16 00:01:03,520 --> 00:01:08,080 And this will make sure that the code in the script will be interpreted by bash. 17 00:01:08,080 --> 00:01:13,440 Now particularly when you are using more advanced features, this is important because the advanced 18 00:01:13,440 --> 00:01:17,919 features will not always be available in all the other shells. 19 00:01:17,919 --> 00:01:24,400 As good practice in a shell script, you should put comments as well behind the hash. 20 00:01:24,639 --> 00:01:30,400 And that is as a documentation, so that later on you can still understand what you are doing. 21 00:01:30,400 --> 00:01:34,519 And other people have some documentation in your script to find out what you are trying 22 00:01:34,519 --> 00:01:35,519 to do. 23 00:01:35,519 --> 00:01:42,400 Next, I'm going to use construction that is known as a here document. 24 00:01:42,400 --> 00:01:45,320 Now in this case, it's a very simple here document. 25 00:01:45,320 --> 00:01:49,519 You might be wondering, why don't we just use echo? 26 00:01:49,519 --> 00:01:53,000 Because the here document is printing a message on screen. 27 00:01:53,000 --> 00:01:57,120 So the construction is cat, smaller than, smaller than, keyword. 28 00:01:57,120 --> 00:01:59,120 Use any keyword that you want. 29 00:01:59,120 --> 00:02:02,760 I like to write these keywords in uppercase, it's not mandatory. 30 00:02:02,760 --> 00:02:06,000 So feel free to use lowercase if you're better with that. 31 00:02:06,000 --> 00:02:10,000 And then we can see the text that I am going to send to cat. 32 00:02:10,000 --> 00:02:13,520 And that will be text that is displayed on screen. 33 00:02:13,520 --> 00:02:16,320 Now echo is typically for one-liners. 34 00:02:16,320 --> 00:02:21,479 And this here document can be used if you want to print more information on screen. 35 00:02:21,479 --> 00:02:25,479 Where the nice thing about the here document is that you don't define lines. 36 00:02:25,479 --> 00:02:27,479 You define a block of text that should be shown. 37 00:02:27,479 --> 00:02:32,479 And regardless of current screen resolution, it will always look good. 38 00:02:34,479 --> 00:02:36,479 Then the script continues with a read. 39 00:02:36,479 --> 00:02:41,479 A read is how a script is going to wait for user input. 40 00:02:41,479 --> 00:02:46,479 So the script will stop and it will put the user input in a variable. 41 00:02:46,479 --> 00:02:49,479 And the name of this variable is there. 42 00:02:49,479 --> 00:02:54,479 And next we are going to work with that variable using cd$dir. 43 00:02:54,479 --> 00:02:58,479 If you define a variable, you just define the name of the variable. 44 00:02:58,479 --> 00:03:03,479 If you use the value of the variable, you need to put a dollar in front of it. 45 00:03:03,479 --> 00:03:08,479 And that's why I'm doing cd$dir followed by pwd followed by ls. 46 00:03:08,479 --> 00:03:11,479 And then I'm using exit0. 47 00:03:13,479 --> 00:03:15,479 Now the exit0 is not needed at all. 48 00:03:15,479 --> 00:03:19,479 But the only reason I've got it here is because I want to tell you about exit codes. 49 00:03:19,479 --> 00:03:25,479 The exit code is telling the parent shell about the success or failure of the script. 50 00:03:25,479 --> 00:03:27,479 And exit0 is implicit. 51 00:03:27,479 --> 00:03:32,479 If all went all right, then the script will exit with the exit code exit0. 52 00:03:32,479 --> 00:03:39,479 And you can use exit$? from the parent shell to figure out that everything was doing all right. 53 00:03:40,479 --> 00:03:42,479 So now I'm done with my script. 54 00:03:42,479 --> 00:03:46,479 And I need to make it executable chmod plus x on script 1. 55 00:03:46,479 --> 00:03:49,479 And now I can run it using script 1. 56 00:03:49,479 --> 00:03:53,479 And there we can see that script 1 is asking what directory? 57 00:03:53,479 --> 00:03:55,479 Well, I want to go to slash home. 58 00:03:55,479 --> 00:03:58,479 And there we can see that it works on slash home. 59 00:03:58,479 --> 00:04:01,479 And it shows the contents of the slash home directory. 60 00:04:01,479 --> 00:04:05,479 And I'm back in slash root. 61 00:04:05,479 --> 00:04:07,479 You might be surprised about that. 62 00:04:07,479 --> 00:04:09,479 What is the reason behind it? 63 00:04:09,479 --> 00:04:12,479 Well, the reason behind it is that when you run a script, 64 00:04:12,479 --> 00:04:14,479 the script is running a subshell. 65 00:04:14,479 --> 00:04:18,480 And all of the commands are happening in the subshell. 66 00:04:18,480 --> 00:04:22,480 And that is why if you cd to a directory in a script, 67 00:04:22,480 --> 00:04:27,480 that cd, change directory, is valid within the context of the script. 68 00:04:27,480 --> 00:04:30,480 But it doesn't affect the parent shell. 69 00:04:30,480 --> 00:04:32,480 There's a way to do that. 70 00:04:32,480 --> 00:04:36,480 And if you want to run the commands in the script in the parent shell, 71 00:04:36,480 --> 00:04:38,480 you need to source it. 72 00:04:38,480 --> 00:04:41,480 Let me show you how you can source a script as well. 73 00:04:41,480 --> 00:04:44,480 In order to source it, I need a couple of modifications. 74 00:04:44,480 --> 00:04:48,480 I need to remove the shebang for the simple reason 75 00:04:48,480 --> 00:04:50,480 that the script is included in the current shell. 76 00:04:50,480 --> 00:04:53,480 So you are bound to the current shell. 77 00:04:53,480 --> 00:04:56,480 I also don't want the exit zero 78 00:04:56,480 --> 00:04:59,480 because that would close the current shell. 79 00:04:59,480 --> 00:05:01,480 Exit zero in a subshell is okay 80 00:05:01,480 --> 00:05:03,480 because you need to close the subshell anyway. 81 00:05:03,480 --> 00:05:07,480 Exit zero in a script that is included in the current shell 82 00:05:07,480 --> 00:05:08,480 would close the current shell. 83 00:05:08,480 --> 00:05:10,480 And that is not okay. 84 00:05:10,480 --> 00:05:13,480 So now I'm going to use the source command. 85 00:05:13,480 --> 00:05:16,480 And source script one is asking what directory? 86 00:05:16,480 --> 00:05:17,480 Well, slash home. 87 00:05:17,480 --> 00:05:21,480 And you can see it has permanently changed my directory. 88 00:05:21,480 --> 00:05:25,480 So I need to manually get back to my original directory. 89 00:05:25,480 --> 00:05:27,480 I need to show you another way to source a script, 90 00:05:27,480 --> 00:05:29,480 and that is the dot command. 91 00:05:29,480 --> 00:05:31,480 Dot script one. 92 00:05:31,480 --> 00:05:34,480 First time you see that, you might be surprised 93 00:05:34,480 --> 00:05:38,480 because you might be used to dot slash run from the current directory. 94 00:05:38,480 --> 00:05:40,480 Now here we don't do dot slash 95 00:05:40,480 --> 00:05:44,480 because the dot does not have the meaning of the current directory. 96 00:05:44,480 --> 00:05:48,480 The dot is the command that is equivalent to source. 97 00:05:48,480 --> 00:05:51,480 As you can see, it is doing the exact same thing. 98 00:05:51,480 --> 00:05:53,480 And that was script one. 99 00:05:53,480 --> 00:05:55,480 I hope you liked it.