Scripting - Bashing!

Hello Peers! Let's check out some basics of shell scripting today.

So narrowing down to what shell scripting is and what is its role in DevOps?

Basically shell like and other programming languages is set a syntax designed in such a manner to promote automation, avoid repetitive task, scheduling routine tasks like backups, adding new functionality. Any shell script is denoted by "./<script_name>" or "<script_name>.sh". Let's get scripting !!

We will be creating a simple script called LetsLearnScripting now which will be print some simple text. As we create the script, we need to give executable permission to it in order to get the desired output. Giving it permissions as 755 , let's have a look over how it goes!!

Above is the script created; now implementing it to get the output by executing it.

Wow! did we just write our first script now!
Now we will try implementing different commands of linux into the script. Taking an example of date command "&&" is used to add one command to another.

User interactive scripts:

We will now check how the use of variables are done and how to read value from end user and then display it via a script, let's have a look on below set of commands:

As we can see in above script, "name" is a variable which stores some value which is later on displayed in the next sentence. A variable in bash scripting is denoted by "$" at the start of it. Now if we want to get an input from user, we use "read" command.

Let's have a look how vi editor looks like:

Now as we can see in below snip, as soon as the editor reaches the read command, it waits for user to enter the value to proceed further:

Post the value it entered; it then displays the whole executed script to the end user, isn't it cool!

Arguments in bash:

In shell scripting, arguments are values that are passed to a script or a function when it is executed. These values can be used as inputs for the script or function to perform operations or make decisions. Shell scripts can take arguments in the form of command line arguments or function arguments. Arguments are denoted by $ followed by the number in bash.

Let us create a file named Arguments_in_bash.sh to learn more about arguments in bash; refer to the second snip for script present in Arguments_in_bash.sh file:

Now as we see in first snip if during the executing of script if we do not give any arguments next to it, the script will consider it as no enter give us the result accordingly.

But what if we do not give all the arguments as mentioned in script? Well in that case it will just print out to number of arguments entered by the end user.

Note: As in snip 1 we can see "Argu1" and "argu1" be taken as 2 different arguments; this is because the case sensitive property of linux.

Also "$#" represents the total number of arguments entered.

Now more of this in next blog!!

Stay tuned folks!!

0
Subscribe to my newsletter

Read articles from janahvi tripathi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

janahvi tripathi
janahvi tripathi