So let's have a look at bash scripting basic components.
So what do we need to know about bash scripting?
Well, a shell script is an executable file
that is used to run tasks.
And shell scripts are commonly used to automate common tasks,
particularly if you are running the same task over and over
again.
A shell script can be as simple as the number
of commands that is sequentially executed.
And scripts normally work with variables
to make them react differently in different environments.
Conditional statements, such as for, and if, and case,
and while can be used to execute tasks
only in specific conditions.
I'll show you some of them.
So shell scripts are common as they
are easy to learn and implement.
And also, shell will always be available to interpret code
from shell script.
If the script uses internal commands,
only it's fast as nothing needs to be loaded.
And there is no need to compile anything.
And for that reason, as a Linux administrator,
you need to have some basic shell scripting skills.
For more complex work, you could use advanced scripting
languages like Python or automation solutions
like Ansible, which are commonly used,
but not covered in this class.
So in order to run a shell script independently,
you store them as an executable file.
It's good practice, but not mandatory,
to use the .sh extension.
Honestly, I only use the .sh extension
if I'm writing a shell script that I'm
putting in a Git repository that is used by other people,
because I want to make it easy for these other people
to find that it's a shell script.
Now, in a shell script, each script
should start with the so-called shebang.
It's a hash exclamation mark slash bin slash bash.
And that identifies the shell that should
be used to run the script code.
In shell scripts, in general, you
can use hash for comments, with the exception
of the first line, because if the first line is starting
with a hash and an exclamation mark, that is a shebang.
And also, you can use white lines for better readability.
Variables in scripts can be used to work with dynamic or site
specific values.
And variables can be provided in many ways.
You can define the variable in the script
by using key is value.
Or you can provide an argument while running the script.
And the value of the argument is stored
in the positional parameter $1.
So in the script, you refer to $1
to do something with the variable.
To define a variable, no dollar is needed.
To refer to it, you need to put a dollar in front.
So color is red defines the variable color.
Echo color is just echoing the text color.
But echo dollar color will echo the value of the variable color
imprinted on your screen.
So let me show you a simple example that is
showing how a script is working.
So I'm creating the famous hello world.
And in hello world, I want the necessary components.
So hash, exclamation mark, slash bin, slash bash.
That's a Seabang.
The Seabang guarantees portability.
If ever the script is using specific bash features
and you're running it on a system that doesn't use
the bash shell by default, then this script
will make sure that the script shell that
is started in the script is a bash shell.
And let me use echo hello, well, not hello world,
hello dollar user.
What is that?
Well, dollar user is a system variable.
And the system variable is defined
when you are logging in.
System variables often are in uppercase.
And that is what you see here.
And this should greet the user by name.
And let's also do something else.
Echo today is dollar between parentheses,
date plus percent d dash, no, not dash, percent m, percent y.
And that will do it.
I'm happy with it.
So I'm going to save my shell script.
What is this again, this dollar parentheses?
That's command substitution.
So within the script, the command date plus percent d,
percent m, percent y is executed.
I do need this thing to be between double quotes,
by the way, because otherwise the spaces are creating issues.
Now I want to run the script.
How am I going to do that?
By using chmod plus x, hello world.
I need my script to be executable.
And now what happens if I use hello world?
I'm getting a command not found.
That's because of the path variable.
We talked about it earlier.
The path variable defines where Linux is going
to look for executable files.
So here we have a list of directories.
And the current directory is not in there.
If I want to run the script from the current directory,
I need dot slash hello world.
And there we can see that today is.
And oh boy, my date formatting string
didn't work out too well.
But you can see the intention was good.
So 0, 5, 29, 25, 05, percent y.
That's not really what I wanted.
Man date should tell me what I do want to do.
I need to go down a little bit on the date format strings.
And there we see what I used, percent d.
And that's the same as percent m, percent d, percent y.
I didn't want to do that.
I wanted something else.
I wanted percent uppercase A, to be honest.
OK, let's fix it.
And make that percent A. And also, let's fix the typo.
I didn't want to type m percent y.
I wanted to type percent y.
And now we can run it again.
And now it is looking a little bit better.
Maybe you want to tweak it a little bit more.
I think for a first demo of a shell script,
this is good enough.