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 the 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,320
And that is because the Linux operating system is very capable of detecting the contents

11
00:00:41,320 --> 00:00:44,599
of any file using the file command.

12
00:00:44,599 --> 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:07,760
And this will make sure that the code in the script will be interpreted by bash.

17
00:01:07,760 --> 00:01:12,839
Now, particularly when you are using more advanced features, this is important because

18
00:01:12,839 --> 00:01:17,919
the advanced 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:41,519
Next, I'm going to use construction that is known as a here document.

24
00:01:41,519 --> 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,080
So the construction is cat, smaller than, smaller than, keyword.

28
00:01:57,080 --> 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:12,880
And that will be text that is displayed on screen.

33
00:02:12,880 --> 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,960 --> 00:02:25,479
But the nice thing about the here document is that you don't define lines.

36
00:02:25,479 --> 00:02:28,119
You define a block of text that should be shown.

37
00:02:28,119 --> 00:02:34,639
And regardless of current screen resolution, it will always look good.

38
00:02:34,639 --> 00:02:36,839
Then the script continues with a read.

39
00:02:36,839 --> 00:02:41,360
A read is how a script is going to wait for user input.

40
00:02:41,360 --> 00:02:47,160
So the script will stop and it will put the user input in a variable.

41
00:02:47,160 --> 00:02:50,000
And the name of this variable is there.

42
00:02:50,000 --> 00:02:55,000
And next, we are going to work with that variable using cd$dir.

43
00:02:55,000 --> 00:02:59,080
If you define a variable, you just define the name of the variable.

44
00:02:59,080 --> 00:03:03,520
If you use the value of the variable, you need to put a dollar in front of it.

45
00:03:03,520 --> 00:03:09,320
And that's why I'm doing cd$dir followed by pwd followed by ls.

46
00:03:09,320 --> 00:03:13,399
And then I'm using exit0.

47
00:03:13,399 --> 00:03:15,919
Now, the exit0 is not needed at all.

48
00:03:15,919 --> 00:03:20,399
But the only reason I've got it here is because I want to tell you about exit codes.

49
00:03:20,399 --> 00:03:25,960
The exit code is telling the parent shell about the success or failure of the script.

50
00:03:25,960 --> 00:03:27,919
And exit0 is implicit.

51
00:03:27,919 --> 00:03:33,080
If all went all right, then the script will exit with the exit code exit0.

52
00:03:33,080 --> 00:03:40,479
And you can use exit$? from the parent shell to figure out that everything was doing all right.

53
00:03:40,520 --> 00:03:42,320
So now I'm done with my script.

54
00:03:42,320 --> 00:03:47,039
And I need to make it executable, chmod plus x on script 1.

55
00:03:47,039 --> 00:03:49,520
And now I can run it using script 1.

56
00:03:49,520 --> 00:03:53,039
And there we can see that script 1 is asking, what directory?

57
00:03:53,039 --> 00:03:55,479
Well, I want to go to slash home.

58
00:03:55,479 --> 00:03:58,080
And there we can see that it works on slash home.

59
00:03:58,080 --> 00:04:01,160
And it shows the contents of the slash home directory.

60
00:04:01,160 --> 00:04:05,520
And I'm back in slash root.

61
00:04:05,520 --> 00:04:07,399
You might be surprised about that.

62
00:04:07,399 --> 00:04:09,720
What is the reason behind it?

63
00:04:09,720 --> 00:04:15,240
Well, the reason behind it is that when you run a script, the script is running a subshell.

64
00:04:15,240 --> 00:04:19,119
And all of the commands are happening in the subshell.

65
00:04:19,119 --> 00:04:24,839
And that is why if you cd to a directory in a script, that cd, change directory,

66
00:04:24,839 --> 00:04:31,440
is valid within the context of the script, but it doesn't affect the parent shell.

67
00:04:31,440 --> 00:04:32,799
There's a way to do that.

68
00:04:32,799 --> 00:04:38,320
And if you want to run the commands in the script in the parent shell, you need to source it.

69
00:04:38,320 --> 00:04:41,279
Let me show you how you can source a script as well.

70
00:04:41,279 --> 00:04:44,519
In order to source it, I need a couple of modifications.

71
00:04:44,519 --> 00:04:50,839
I need to remove the shebang for the simple reason that the script is included in the current shell.

72
00:04:50,839 --> 00:04:53,359
So you are bound to the current shell.

73
00:04:53,359 --> 00:04:59,359
I also don't want the exit 0, because that would close the current shell.

74
00:04:59,359 --> 00:05:03,920
Exit 0 in a subshell is OK, because you need to close the subshell anyway.

75
00:05:03,920 --> 00:05:09,119
Exit 0 in a script that is included in the current shell would close the current shell.

76
00:05:09,119 --> 00:05:11,440
And that is not OK.

77
00:05:11,440 --> 00:05:14,079
So now I'm going to use the source command.

78
00:05:14,079 --> 00:05:16,959
And source script 1 is asking what directory?

79
00:05:16,959 --> 00:05:22,119
Well, slash home, and you can see it has permanently changed my directory.

80
00:05:22,119 --> 00:05:25,920
So I need to manually get back to my original directory.

81
00:05:25,920 --> 00:05:32,559
I need to show you another way to source a script, and that is the dot command, dot script 1.

82
00:05:32,559 --> 00:05:34,720
First time you see that, you might be surprised,

83
00:05:34,720 --> 00:05:39,279
because you might be used to dot slash run from the current directory.

84
00:05:39,279 --> 00:05:45,239
Now here we don't do dot slash, because the dot does not have the meaning of the current directory.

85
00:05:45,239 --> 00:05:49,000
The dot is the command that is equivalent to source.

86
00:05:49,000 --> 00:05:52,799
As you can see, it is doing the exact same thing.

87
00:05:52,799 --> 00:05:55,040
And that was script 1. I hope you liked it.

