As I want to be to the point on shell scripting, I'm going to organize this entire lesson around
a couple of shell scripting examples.
Here is example number one.
For your convenience, I've put it in a slide.
I'm going to write it for you.
And while I write, I will explain what it is doing and what these different components
are all about.
OK, let's give it a very original name, script1.
Notice that in a shell script name, you don't need an extension.
And that is because the Linux operating system is very capable of detecting the contents
of any file using the file command.
Now every shell script should start with what you call the shebang.
That's calling the interpreter for the subshell that is running your script.
The reason is that not always bash is used.
Bash is often installed, but you might be running a script from a different shell.
And this will make sure that the code in the script will be interpreted by bash.
Now particularly when you are using more advanced features, this is important because the advanced
features will not always be available in all the other shells.
As good practice in a shell script, you should put comments as well behind the hash.
And that is as a documentation, so that later on you can still understand what you are doing.
And other people have some documentation in your script to find out what you are trying
to do.
Next, I'm going to use construction that is known as a here document.
Now in this case, it's a very simple here document.
You might be wondering, why don't we just use echo?
Because the here document is printing a message on screen.
So the construction is cat, smaller than, smaller than, keyword.
Use any keyword that you want.
I like to write these keywords in uppercase, it's not mandatory.
So feel free to use lowercase if you're better with that.
And then we can see the text that I am going to send to cat.
And that will be text that is displayed on screen.
Now echo is typically for one-liners.
And this here document can be used if you want to print more information on screen.
Where the nice thing about the here document is that you don't define lines.
You define a block of text that should be shown.
And regardless of current screen resolution, it will always look good.
Then the script continues with a read.
A read is how a script is going to wait for user input.
So the script will stop and it will put the user input in a variable.
And the name of this variable is there.
And next we are going to work with that variable using cd$dir.
If you define a variable, you just define the name of the variable.
If you use the value of the variable, you need to put a dollar in front of it.
And that's why I'm doing cd$dir followed by pwd followed by ls.
And then I'm using exit0.
Now the exit0 is not needed at all.
But the only reason I've got it here is because I want to tell you about exit codes.
The exit code is telling the parent shell about the success or failure of the script.
And exit0 is implicit.
If all went all right, then the script will exit with the exit code exit0.
And you can use exit$? from the parent shell to figure out that everything was doing all right.
So now I'm done with my script.
And I need to make it executable chmod plus x on script 1.
And now I can run it using script 1.
And there we can see that script 1 is asking what directory?
Well, I want to go to slash home.
And there we can see that it works on slash home.
And it shows the contents of the slash home directory.
And I'm back in slash root.
You might be surprised about that.
What is the reason behind it?
Well, the reason behind it is that when you run a script,
the script is running a subshell.
And all of the commands are happening in the subshell.
And that is why if you cd to a directory in a script,
that cd, change directory, is valid within the context of the script.
But it doesn't affect the parent shell.
There's a way to do that.
And if you want to run the commands in the script in the parent shell,
you need to source it.
Let me show you how you can source a script as well.
In order to source it, I need a couple of modifications.
I need to remove the shebang for the simple reason
that the script is included in the current shell.
So you are bound to the current shell.
I also don't want the exit zero
because that would close the current shell.
Exit zero in a subshell is okay
because you need to close the subshell anyway.
Exit zero in a script that is included in the current shell
would close the current shell.
And that is not okay.
So now I'm going to use the source command.
And source script one is asking what directory?
Well, slash home.
And you can see it has permanently changed my directory.
So I need to manually get back to my original directory.
I need to show you another way to source a script,
and that is the dot command.
Dot script one.
First time you see that, you might be surprised
because you might be used to dot slash run from the current directory.
Now here we don't do dot slash
because the dot does not have the meaning of the current directory.
The dot is the command that is equivalent to source.
As you can see, it is doing the exact same thing.
And that was script one.
I hope you liked it.