“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

[Note]An Introduction to Bash Scripting

Bash Script Introduction

Fancy yourself as a computer scientist, amateur, or tech nerd? Then, at some point, you will or should consider using Bash scripts in your digital workspace.

Bash (Bourne Again Shell) is an interpreter responsible for handling commands on the command line of Unix systems. It is freeware written by Brian Fox and released in 1989 as an alternative to Sh (Bourne Shell). Bash is used by developers, data scientists, system administrators, network engineers, and anyone else who relies heavily on the Unix operating system in their daily work. In general, Bash scripts are used to automate routine remedial tasks that computer scientists might undertake. In short, a shell script is nothing more than a series of commands stored in a file, such as a list.

You can use Bash on Linux and MacOS machines, and even on Windows 10 machines through the Windows subsystem for Linux. Bash typically runs in a text window where the user can type commands to make the computer perform operations. The language can also be used to read and execute commands from files, called shell scripts. Shell scripting is a programming language in itself, and Bash, like any other language, is a tool that can be used in a variety of ways.

If you’ve seen a machine running a Linux operating system (or uniX-like environment) before, you’ve probably seen a terminal console, too. A terminal is a way for the user to interact with the shell interpreter using certain commands. Commands such as CD to navigate file directories, ls to list files in the current directory, and Nano to edit files.

Use Bash code in the terminal, which will be run by the Bash interpreter. Commands like ls are binary executables in the /bin directory. When the shell receives this command (when you type it in the terminal and press Enter), it executes the ls file and lists the files in the current directory for the user. Run the ls /bin command to execute binary ls with path /bin as an option to list the files in the /bin directory. Executing ls -al runs the ls command with the -a and -l flags as options to list all files and directories in the current directory path and details about those items.

Touch is another such binary executable, commands that the user can use in the terminal. The output of this command is a new file, with the name entered by the user as an option. For example, a user could write touch Hello.txt, and the output would be a file hello.txt.

How do I run multiple Bash commands

To run multiple Bash commands and have them executed at once, users can save these commands in a single file for execution with Bash. Assuming you’re working in a Unix/ Unix-like environment, let’s consider what we discussed earlier.

Once you open the command terminal, start with your favorite text editor, such as Nano or vi. Writing:

nano make_a_file.txt
Copy the code

Then, write the following:

#create a file
touch hello.txt

#list files from this directory
ls -al
Copy the code

Save and exit the file, and run the new script using one of the following command syntax:

sh make_a_file.txt
Copy the code

or

./make_a_file.txt
Copy the code

or

bash make_a_file.txt
Copy the code

If an error occurs while executing this file, proceed to set executable permissions for the script file you just wrote by typing:

chmod +x hello.sh
Copy the code

If you followed this example, you just created a file containing multiple Bash commands. The Bash interpreter runs these commands sequentially and ignores lines that begin with the hash symbol # because they are comments. Running this file produces terminal output of a list of files, which will contain a hello.txt that was not there before.

Typically, a Bash script file is saved in a format that includes the.sh extension, indicating that the file is a shell script. However, when a file begins with “she-bang” or “hashbang”, we can execute it as if it were a binary file.

When creating scripts, we should take into account that every binary shell file begins with what is commonly known as “she-bang” (also known as sh-bang or hashbang). This is the beginning of the script title, and the first line of code indicates which shell you will use. When writing scripts, we have several options to choose from, including shell (sh), C shell, Z shell, and so on. In this case, we’ll continue to use Bash for our scripting needs. She-bang is a set of symbols ** “#” and “! “at the beginning of the script. ** We know that a pound sign (#) indicates that a line is a comment. However, with She-BANG, the program interpreter of unix-like systems parses the rest of the first line as interpreter instructions. In this case, write #! When /bin/bash, hash symbols and exclamation marks are used as program loaders to indicate that it uses bash Shell programs in the /bin/bash directory.

How do I create variables in Bash

Like most other Unix shells, Bash has variables, pipes, filename wildcards, here documents, command substitution, and control flow. Bash also supports alternation (which it shares with the C shell), command-line completion, and signal processing and basic debugging. With these features, it is not surprising that bash has become the default command interpreter for Unix and UniX-like systems.

Like other programming languages, we can declare variables when writing scripts using Bash. However, unlike other languages, Bash does not require keywords to declare variables or assign data types to them. Bash has no type system and only stores variables as string values. However, Bash can automatically convert variables to appropriate types based on certain operations, such as arithmetic operations. To write a VARIABLE and fill it with values, write in VARIABLE=VALUE format, making sure no Spaces are included. Here is an example of how to create variables in Bash:

#! /bin/bash

#write a variableNAME = "William"#use that variable
echo"Hello!$NAME"Copy the code

The user can also populate variables with user input:

#! /bin/bash

echo"Hello!The $1, that is a $2The name"Copy the code

In the terminal:

~$bashName. sh "William" "great" Hello William, that is a great nameCopy the code

You can also use user-typed variables at run time using commands like read:

#! /bin/bash

echo"What is your name?"read name

echo"Hello!$name"Copy the code

In the terminal:

~$bash name.sh
What is your name?
~$William
Hello William
Copy the code

The if statement in Bash

We can also implement if statements for additional functionality.

#! /bin/bash

echo"Who is there?"read name

if [ $name ]
echo"Hello!$name"else
 echo"Must 've had my imagination"fi
Copy the code

In the terminal:

~$bashname.sh Who is there? ~$Must have been my imaginationCopy the code

How do I create backup management scripts in Bash

Other items to consider include setting up backup management scripts. This can be a simple project that you can start and revisit later. With this, you can make a simple script to compress one or more files and folders using the tar library and place them in a new backup directory of your choice. The following script is a basic backup script that creates a.zip file for the files that need to be backed up and marks them according to their creation date:

#! /bin/bash

#get the month, day, and year of the current date
TIME_OF_BACKUP=`date +%m-%d-%y`

#create a backup file using the current date in its name
DESTINATION=/path/[BACKUP FOLDER]-$TIME_OF_BACKUP.tar.gz

#the folder that contains the files that we want to backup
TARGET_FOLDER=/path/[TARGET FOLDER]

#create the backup
tar -cpzf $DESTINATION $TARGET_FOLDER
Copy the code

As a bonus, you might want to add some complexity to the project and increase the level of automation by adding scheduled execution to the backup script. To do this, you can use the Crontab program and command library. If you need to install Cron, be sure to update your current package library before continuing to install Cron.

sudo apt-get update
sudo apt-get install cron
Copy the code

After a successful installation, you can continue to use the Cron library to schedule the execution of the script.

crontab -e
Copy the code

This will open the /etc/crontab file and allow you to write the following command to schedule the execution of the script:

@weekly /path/backup_script.sh
Copy the code

I won’t go any further into what you can do with crontab, because it’s beyond the scope of this article.

conclusion

Through this getting started guide, you’ll learn what Bash is, what scripts are, and what scripts are within Bash. You can do a lot with Bash, and you don’t need to know a lot about programming to throw different Linux applications and tools together and make something useful. The Bash script is a very useful tool, and I hope you find inspiration in this article to automate your ideas.