Linux Foundation Certified System Administrator (LFCS) : Shell scripting (Part 6)

Creating our first script

Let’s assume that we are about to launch a rocket to lunar/moon

Let’s create a folder called “lunar-mission” . Then create commands like “rocket-add” and then directory name to add a rocket

In this way, other commands like “rocket-start-power” followed by directory name every single time.

The whole task can be done using a shell script.

Let’s give it a name create-and-launch-rocket.sh and copy all of the commands

then we can execute that using “bash” command and then the shell script

once done, it will run the script line by line

We can also use this script file as a command. We can just create a file without any .sh extension and then use it as a command.

But here we go! An issue came up as the OS does not know any command by this name.

When a command runs, it looks for that at the $PATH . If it can’t find that, the command does not work

You can now update the path and mention to check the working path (which is /home/michael here)

Because /home/michael location has create-and-launch-rocket file and thus

If you run this one, it works as a command.

To verify if this command actually refers to the script file , we can use this

Here you can see the location of this file.

You may also face this issue

Basically you might not give any execution access (x) to this file. Verify this by ls -l <file location>

As user access us rw, group access is rw and others access is r, we need to add execution access (x) here using chmod

Now we can see we have proper access and the command will work

Variables

We can set a variable which can be changed in our code. Here mission_name can be a variable with value “lunar-mission” or, “mars-mission”

We set the value by mission_name= value and then call that using a $ sign

We can’t use hiphen in a variable name but alphanumeric and underscore is aceepted

We can also save results of commands in a variable

Here, we have rocket-status command which gives 3 results

Now, we can save them in rocket_status variable

here $mission_name has the value lunar-mission. So, these are exactly the same commands.

we save them following this format

variable_name = $(command)

Then we can print the variable according to our wish

Command line argument

Now assume that your have created a script “create-and-launched-rocket” and you ran it first

Then you thought that the mission_name should be changed and you want to set that in your terminal.

You want to set the mission_name after the script name

To do this, you need to modify your script file and use something called command line argument

here in the terminal we have

create-and-launch-rocket saturn-mission

these are referred as $0 and $1.If we can modify the script file now with $1 as the value of the variable, that’s it

Now we can try with our new values for the mission_name

Read Inputs

What if we want to input the mission name

instead of writing it with the command?

To do this, we add read variable_name in the script

We can also make it better by adding some texts (read -p <prompt> <variable_name>)

now if we run this script by using the command, it will take input like this

Arithmetic operations

To use arithmetic operations , use + , -, /, \*

We can also set variable and use these

We can also use double parentheses

We don’t get floating value in general

But we can have it using basic calculator (bc)

Conditional Logic

If Logic

We know that a script might be successful or fail.

for example, we do run thee script lunar-mission and check it’s status

So, if the script fails, we can add a logic in the script

Here, if condition ends with fi (opposite of if), also elif also have then block .

This is how we can create blocks for if and elif.

We can also have conditional operators

For Loop

Rather than running commands 7 times, we can use a loop

We can use

for mission in <missions list?

do

<command>

done

Or, we can list all of the mission names in a text file and then use it in the for loop

or,

Other examples:

we can also set for 0 to 100

In real life, this is how files are installed

We can also check the uptime for the servers listed in servers.txt

While loop

Note: While statement ends with done

Now, assume that the rocket is getting launched and when it’s done, you want to wait 2 seconds and it checks again it that’s loading and waits 2 seconds.

Here is the while code

Real life use case

Case statement

We can have case statements instead of these if , elif, else

Case statement ends with esac(opposite of case)

It can be written as this too (using while )

Shebang

We used for loop but does this script applies for every shell?

No! For bash shell, it works fine but for shells like sh shell, it does not event know how to expand {0..10} .

In modern devices, Bourne shell (sh) basically refers to Bourne again shell (bash)

he ls -l shows file permission and location

To run bash files in sh, we call bash and then call the script

But if you need to share the code with someone and the person does not know these difference?

we can just solve this issue by using SHEBANG #!<shell type>

here, in this launch-rockets.sh script, the developer mentioned this !#/bin/bash and thus this script will run only in bash

Exit codes

When a command is successful, it returns exist status 0 and when failed, it returns other values

To check that value, we can use echo $?

In our rocket script, if the rocket fails to launch, we can individually add exit value to 1 (more than 0) so that, we can always get 1 when this script fails

Functions

If we want to use the launch script again and again for different launches, we can use function

we can create a method called launch-rocket() and keep all the code within that.

Now it’s ready to use!

We can now call this function and specify the mission name (as we did set mission_name =$1, once we call the script as a command, it waits for an input which it replaces with $1)

So, this will be the final code for four missions

Another thing is, we did set exit as 1 once the script fails. Now, we are running 4 missions and if one fails, we should not stop our launch.

So, we can set return to 1 so that the function stops for a mission and moves to second mission. Otherwise the whole script would not run if we had exit as 1

We can check that return value like this .

Use cases:

Here we called add method following by 3 and 5

add will look for function add() and once it gets into that, it will look for $1 and $2. Here, $1 means 3 and $2 means 5. Now that will sum up!

If we need to get the summation value and add that as a variable, we can do this

Here we can only get numbers as return value and we need to set a variable using $? in case of return

Or, we can use normal way

Here echo prints the summation and that value exactly goes to the sum variable

Done!