Hello and welcome to this introductory video on Oracle Linux.
This video discusses and demonstrates bash shell scripting on Oracle Linux.
After watching this video, you will learn about:
Functions and Shell Variables
Flow control with if/else and for loops
And case statements.
The bash shell provides access permitting small programs to run.
These small programs contain command instructions coded in scripts, which are
simply text files.
After creating a script, you can execute the program by calling the script by
name from the command line.
For example, I'll run a simple helloworld script to demonstrate.
Let's take a look at the helloworld script by using the cat command.
The first line starts with the shebang followed by the location of the bash shell.
This line guarantees that the script will be interpreted and used by Bash.
The script simply prints out hello world to standard output.
Before executing, you must use the chmod command and +x argument to set a script
to be executable.
Now run the helloworld.sh script by calling it's name to execute.
As we can see, the script executed without any errors.
Let's now turn towards shell variables.
Variables provide a method to store data in a place by name.
To recall a value of a variable, a dollar sign is used to help represent a variable.
You may already be familiar with variables such as environment variables.
By typing env, you can see examples of environment variables in your shell.
We can set a variable by typing:
Then echo the hello variable by typing:
The word world is displayed because we defined the variable hello to the word world.
Let's now take a look at functions.
Functions are scripts within scripts.
Functions can be defined within a script to be called whenever needed.
For demonstration purposes, a function was created in the script called friendlist.sh.
Let's use the cat command on friendlist.sh to view the function.
In this script, the function is run and receives the arguments "alice bob charlie".
The first line displays friendlist: and then the three arguments "alice bob charlie".
The next line displays the scriptname and location of the script followed by
the variable data "alice bob charlie" stored in variables $1, $2, and $3.
$4 was not used due to only three arguments were passed.
The third step displays the number of arguments which were 3 in total.
Let's run the script with arguments to see the results.
The results show exactly as mentioned.
There's quite a bit more to cover for functions, but for now we'll turn to flow
control in shell scripting.
Flow control within a script allows you to control how the script is executed.
In other words, you can direct how a script processes according to your liking.
One such method for controlling a script is using the conditional construct if/else.
For example, let's use the cat command to take a look at an if/else block added to
the friend script.
Within the angle brackets, our first line passes the argument to the friend
variable, however, if an argument does not exist the script displays the error
message friend is missing and stops the processing.
The next line starts with an if statement that checks to see if the friends
variable does not equal Alice.
If it does not, then the greetings stranger is displayed.
If the friend variable does equal alice, then the code skips over the greetings
stranger condition and executes the else condition by echoing hello alice.
The fi tells the script to close the if/else block ending the execution of the script.
Let's run the friend.sh script with alice as an argument.
As shown, alice is greeted with a hello.
Let's now run the friend.sh script with another name this time with bob.
The greetings stranger was displayed since it was not alice.
Let's now investigate for loops.
Use the cat command to display the code for excited.sh script.
This shell script looks very similar to the friend script except For
an additional for loop.
When the friend variable is equal to alice then a for loop is executed which
simply loops not once, not twice, but three times for displaying hi alice
showing our excitement for seeing alice.
We could of increased or decreased the for loop, but as you can see it is
quite simple to loop a number of times to execute a specific command or commands.
Run the excited.sh script to see the results.
As shown, our hi alice repeats three times because of the for loop added.
Lastly, let's look at an example of a case statement by using the cat command
on friends.sh script.
This time instead of using an if/else block, this script implements a case statement.
As shown, the $friend receives and stores the input argument passed from the
command line.
The first case checks if the $friend equals alice then hello alice is displayed.
The second case checks if the $friend equals bob then hello bob is displayed.
The third case checks if the $friend equals charlie then hello charlie is shown.
Finally, if any other name is held by the friend variable then the
greetings stranger is displayed since it's neither alice, nor bob, or charlie.
Let's run the script to see.
Alice is greeted.
Bob is greeted.
Charlie is greeted.
And when it is someone we don't know, the stanger is greeted with a greetings
stranger.
This concludes the video.
Thank you for watching.