قالب وردپرس درنا توس
Home / Tips and Tricks / Bash Automation & Scripting Basics (Part 1) – CloudSavvy IT

Bash Automation & Scripting Basics (Part 1) – CloudSavvy IT



Shutterstock / Mopic

Automation of a repetitive task tends to bring joy to the Bash developer: instead of typing endless commands over and over, a script just does the job repeatedly. Bash is ideal for such automation. This series will get you started …

What̵
7;s Bash automation

The Bash shell is a powerful Linux shell that allows deep automation of repetitive tasks. Not only is the Bash Linux shell a great choice for DevOps, database and test engineers alike, users can take advantage of slowly learned, ever-increasing Bash skills every day. Bash is also a scripting and coding language that grows with you. I’ve been actively coding in Bash since 2012 and have been using it for a lot longer than that.

Bash also lends itself to many different application domains and use cases. For example, you can easily use it for handling Big Data, and surprisingly it seems to lend itself extremely well to this task due to the myriad word processing tools available in it, or available as easy-to-install packages. It is also great for backup and database planning and maintenance, or handling large file storage solutions, automating web servers, and more.

One thing I have discovered is that when the following problem arises, a little research in a search engine, or the various Stackoverflow websites, will quickly provide not only a solution to the problem, but also an opportunity to grow and grow. to learn. This is a similar experience to someone teaching the editor vi where the same is true; when a problem arises, the solution is near.

This three-part miniseries is the first, and in it we’ll look at Bash automation and scripting basics.

Shack!

You may be wondering what kind of title that is. And you would be right, unless you were talking to an experienced Linux developer. They would smile at best. That’s because the first two letters of a well-written Bash script are always, well, Shebang!

There are two characters #! which can be placed at the top of a script, which indicate to the shell what the interpreter can, should and will be used to process the script in question. It is a coincidence that this symbol as a whole Shack As a whole.

To expand on our previous statement, the first line of a well-written Bash script should start with #!/bin/bash to indicate to the shell (whatever shell is used, for example, it can be bash or sh or zsh) that we want the binary file /bin/bash (our Bash shell) to run this code.

Let’s put this into practice by defining a little script test.sh as follows:

#!/bin/bash

echo 'Hello CloudSavvyIT Readers!'

You can create this script with your favorite text editor, which is preferably an editor that only uses monospace fonts, such as we vim nano or any plain text-based editor on your Linux Desktop, and preferably avoid things like a word processor like OpenOffice Writer etc. as these can write additional unwanted binary data or characters to our script. In other words; we must use plain text.

Once this script is defined, we make it executable by executing chmod +x test.sh on the command line. Then we can start the script by simply calling the name with the prefix ./ ./test.sh

Our first bash script with shebang and echo

Input parameters

Once you start writing scripts, you will likely find yourself wanting to pass input parameters to the script. One way to do this is to use the simple positional parameters available by default in Bash. Let’s take a look at an example script test2.sh, which we define as follows:

#!/bin/bash

echo "${1}"

Here we have used the positional parameter ${1}This variable returns the first word (separated by a space by default) passed to our script, unless a quote is used. Let’s see how this works:

Using positional parameters in our second bash script

After making our script executable again with chmod +x test2.sh we do the same and pass on a single word hello as the first positional parameter (${1}The result is that hello reflects back to us.

This is because when the script was started, the variable ${1} (or $1 although I recommend always putting quotation marks around variable names) was set to the value of the first positional parameter; the first word or string in quotation marks after the script name.

Then we passed hello worldhowever, this only echoed hello back to us. The reason is simple; the second word (separated by a space by default) is considered the second positional parameter and is thus initialized as ${2} in the script, no ${1}

We got around this behavior by putting quotes around the input when we passed 'Hello CloudSavvyIT Readers!'Single or double quotes would have both worked, although their operation differs, more on this in the next part of this series. The result is our first positional parameter ${1} is set to full text Hello CloudSavvyIT Readers!, and so the same is reflected back to us.

If you’d like to learn more about variables, our Bash functions and Exporting Local Variables and Variables in Bash: The Why and How articles might also be of interest.

Shut down

In this article, we’ve discussed the first set of basics of Bash automation and scripting. We learned what Bash automation and a Shebang is, and how to pass input variables to our scripts. In the next part of this series, we’ll take a look at how to quote variables and more! Stay tuned!


Source link