Home / Archive

Bash script for beginners

Bash scripting for beginners Part 1

What is Bash script?

A Bash script is mainly a set of commands that can be executed in a Terminal. You can create a Bash script file (filename.sh) and execute the set of commands inside that file as many times as you want. So if you want to execute a set of commands periodically then you should think “Bash script”.

Bash script is more than that. You can actually use that as a programming language in order to create small programs that can be executed in a terminal.

And as they say the most easy way to learn a language is to create a program with that language and try to improve it. (I don’t really know if people say that, but that is the way of this tutorial.

So in this tutorial i will explain how to create bash scripts while you are creating a backup program.

Lets start Bash scripting

echo Comments

To do so, let’s create some folders that we will need in order to store our scripts and backups.

Open a terminal and type:

cd ~/Desktop

mkdir bash

cd bash

mkdir code

mkdir backup

Now go into “code” folder

cd code

Just before we start the backup program let’s create first a bash script for “Hello  World”, so you can understand how to create and then execute a script.

In order to create a Bash script you will just need a text editor such as “gedit”. You can use your favorite text editor but I will use gedit since this editor comes with Ubuntu.

Open a terminal and type:

gedit ~/Desktop/bash/code/helloworld.sh

enter the following text to gedit,

#!/bin/bash  
# Bash scripting with allaboutlinux.eu 
echo Hello World

press “save” and close gedit

Almost done! Now you have to make that file executable. Open a terminal and type:

chmod +x helloworld.sh

And now you are ready to execute your first Bash script. Open a terminal and type:

cd ~/Desktop/bash/code

./helloworld.sh

Now you should see “Hello World”.

Just a quick explanation of the script:

You can use “#” in order to “comment” a line. So the line “# Bash scripting with allaboutlinux.eu” is just a comment and has nothing to do with the script. there is an exception to that rule that applies to the first line that starts with “#!” this line defines which interpreter to use. in this case we use bash.

You can use “echo” in order to print something in the terminal.

If you have understood everything in this part you can go to the next part where we will start making our backup program.


Bash scripting for beginners Part 2

A simple backup script

Now we will create the first version of this backup script. So what this script will do? It will create a compressed file in the backup folder from the folder that we store our scripts.

Let’s create the Bash script file as before, but now we will name that script backup.sh:

gedit ~/Desktop/bash/code/backup.sh

write the following to the gedit :

#!/bin/bash  
# Bash with allaboutlinux.eu
tar -zcvf ~/Desktop/bash/backup/backup.tar.gz ~/Desktop/bash/code/
echo "backup saved to ~/Desktop/bash/backup/backup.tar.gz"

save and close. Now make the file executable

chmod +x backup.sh

execute that file with

./backup.sh

Just a quick explanation of the script:

The first line is comment. So no comment  😀

The second line is actually the main program it compresses a folder to a file and it works like that:

“tar -zcvf” is the tool that we use here to compress the folder followed by “~/Desktop/bash/backup/backup.tar.gz” which is the location to store the backup file with the file name, and last by “~/Desktop/bash/code/” which is the folder that we want to backup. Maybe that looks a bit complicated but if you take a good look at the script you will see that is easier than you think.

The third line is just a message which you know from the previous part.

From now on I will not explain parts of the script that already have been explained so at any time you want to check something just turn back and take a look on the previous parts of this tutorial.

Now you have a script that can run and make a backup of the folder ~/Desktop/bash/code. If you are ready let’s proceed to the next part and make our script a bit better.

Bash scripting for beginners Part 3

stdout to file

Let’s make some improvements to the previous script. In this part I will explain to you how to keep a log file of the backup files into a text document. To do so let’s create a new script and name it backup2.sh

gedit ~/Desktop/bash/code/backup2.sh

write the following to the gedit :

#!/bin/bash  
# Bash with allaboutlinux.eu
tar -zcvf ~/Desktop/bash/backup/backup.tar.gz ~/Desktop/bash/code/ > ~/Desktop/bash/backup/backup.txt
echo "backup saved to ~/Desktop/bash/backup/backup.tar.gz and the results written to ~/Desktop/bash/backup/backup.txt"

save and close. Now make the file executable

chmod +x backup2.sh

execute that file with

./backup2.sh

Just a quick explanation of the script:

What we added here is the “> ~/Desktop/bash/backup/backup.txt” at the end of the second line. This will store the output of that command to a file with the name backup.txt

You can add “>” at the end of a command and then a filename.txt to write in that file the output of a command. For example:

ls > ls.txt

The “ls” command normally will print in the terminal the files and directories that are in the current directory. But instead of that the command:

ls > ls.txt

will print the same data in a text file with the name ls.txt.

In the third line I have just added some more information to print in the terminal before that script ends.

So now you have an improved backup program and also you know how to use stdout to file.

Let’s proceed to the next part.



Bash scripting for beginners Part 4

Variables

In this part we will not make an improvement of backup2.sh but we will use some variables to make our life easier. Variables will also make scripts shorter and easier to read. Maybe you will disagree with that at the begining but you will totally agree when you will make large and complicated scripts. To do so let’s create a new script and name it backup3.sh

gedit ~/Desktop/bash/code/backup3.sh

write the following to the gedit :

#!/bin/bash  
# Bash with allaboutlinux.eu
BKFILE=~/Desktop/bash/backup/backup.tar.gz # the desination and the filename of the compress backup folder
BKFOLDER=~/Desktop/bash/code/ #the folder that we want to backup
BKRESULT=~/Desktop/bash/backup/backup.txt # the destination and filename of the log file
tar -zcvf $BKFILE $BKFOLDER > $BKRESULT
echo backup saved to $BKFILE and the results written to $BKRESULT

save and close. Now make the file executable

chmod +x backup3.sh

execute that file with

./backup3.sh

Just a quick explanation of the script:

This is really self explained script so I will write a few words about variables. In order to create a variable you need a name followed by “=” and then the content of that variable. It is that simple. So you can create a string variable named “strvar” that will contain “Bash script for beginners” like that:

strvar=”Bash script for beginners”

Or you can create a numeric integer variable named “intvar” that will contain the number “10” like that

intvar=10

We will use more variables in the next part so you will understand that better.

In order to use a variable just put “$” in front of the name.

So if you use the “echo” command that you learned earlier followed by a variable, echo will print in the terminal the containing data of that variable. For example:

echo $strvar

it will print:

Bash script for beginners

but if you use:

echo strvar

it will print:

strvar

I hope that’s enough for variables. Let’s move on, to the next part.


Bash scripting for beginners Part 5

Date and Time

So far we have created a backup that works but there is a small problem everytime we run one of the previous scripts we created the backup in the same file. And what if we want to create separate backups that we can also have previous version of the backups? A good idea is to create file names that will contain the date and the time that this backup was created. In that way we will have a unique name every time we run the backup. To do so let’s create a new script and name it backup4.sh

gedit ~/Desktop/bash/code/backup4sh

write the following to the gedit :

#!/bin/bash
# Bash with allaboutlinux.eu
DATETIME="$(date +%d.%m.%Y_%H:%M:%S)"
BKFILE=~/Desktop/bash/backup/backup$DATETIME.tar.gz
BKFOLDER=~/Desktop/bash/code/
BKRESULT=~/Desktop/bash/backup/backup$DATETIME.txt
tar -zcvf $BKFILE $BKFOLDER > $BKRESULT
echo backup saved to $BKFILE and the results writtten to $BKRESULT$DATETIME

save and close. Now make the file executable

chmod +x backup4.sh

execute that file with

./backup4.sh

Just a quick explanation of the script:

This is just like the script from part 4 but I have added a new variable with the name DATETIME that is containing the current time and date when we execute the script. So every-time we execute the script that variable takes another value. A variable can also contain a previous defined variable just like in line 3 and line 5.

(date +%d.%m.%Y_%H:%M:%S) is used in order to get the current time and date. You can use only time or only date it’s up to you. For example if you just want the month and the year just use (date %m.%Y).

Note %m is for months but %M is for minutes. Most of the commands parameters are case sensitive. 


Bash scripting for beginners Part 6

Select and IF statement

Let’s try to add a simple “menu” in our script. Now we will give the option to the user to execute the backup or quit the program. So if you run that script and you decide that you don’t really want to run it, you will have the option to cancel that script. To do so let’s create a new script and name it backup5.sh

gedit ~/Desktop/bash/code/backup5.sh

write the following to the gedit :

#!/bin/bash 
# Bash with allaboutlinux.eu
DATETIME="$(date +%d.%m.%Y_%H:%M:%S)"
BKFILE=~/Desktop/bash/backup/backup$DATETIME.tar.gz
BKFOLDER=~/Desktop/bash/code/
BKRESULT=~/Desktop/bash/backup/backup$DATETIME.txt
LIST="Backup Quit"
select OPT in $LIST; do
if [ $OPT = "Backup" ]; then
tar -zcvf $BKFILE $BKFOLDER > $BKRESULT
clear
echo backup saved to:
echo $BKFILE
echo and the results writed to:
echo $BKRESULT
exit
elif [ $OPT = "Quit" ]; then
clear
echo Backup aborded by user
exit
else
clear
echo bad selection
exit
fi
done

 

save and close. Now make the file executable

chmod +x backup5.sh

execute that file with

./backup5.sh

Just a quick explanation of the script:

Here we have add a new variable named “LIST” that contains 2 words “Backup” and “Quit”. When you run:

“select OPT in $LIST; do”

That wiil create a list that you can choose between the words that were contained in variable “LIST” so in that case it will be something like:

1) Backup
2) Quit
#?

If you have more words in the “LIST” you will get more options. So the script is now waiting for your input. Type “1” and press enter in order to select backup of your folder or type “2” and press enter in order to Quit without backup. When you make your selection the variable “OPT” will get the value of the text that is next to the number of your selection. Now the script has to check your selection and decide what to do next. In order to do that we will need an “IF statement”. The “IF statement” is defined like the following example:

if [ condition1 ]; then
the code to execute if the condition 1 is true
elif [ condition2 ]; then
the code to execute if the condition 2 is true
else
the code to execute if  none of the conditions 1,2 are true
fi

So if you can understand the syntax above you will be able to understand the script backup5.sh. I have also added the command “clear” in this script that is just clearing the terminal from previous texts.

If you have any problems understanding this please leave a comment and I will try to answer.

Let’s go to the next part.


Bash scripting for beginners Part 7

Read

Sometimes it’s useful to let the user decide what’s the name of the backup that the script will create. So if you want to give a specific name to the backup you have to let the user type a name. So let’s create a script based on the previous one that will let the user select if he wants to add a specific name for the backup or just let the name with the date and time. To do so let’s create a new script and name it backup6.sh

gedit ~/Desktop/bash/code/backup6.sh

write the following to the gedit :

#!/bin/bash 
# Bash with allaboutlinux.eu
DATETIME="$(date +%d.%m.%Y_%H:%M:%S)"
BKFILE=~/Desktop/bash/backup/backup$DATETIME.tar.gz
BKFOLDER=~/Desktop/bash/code/
BKRESULT=~/Desktop/bash/backup/backup$DATETIME.txt
BKDESTINATION=~/Desktop/bash/backup/
LIST="Backup Quit"
LIST2="Date-Time User-Difine"
select OPT in $LIST; do
if [ $OPT = "Backup" ]; then
echo please chose the filename of the backup:
select OPT in $LIST2; do
if [ $OPT = "Date-Time" ]; then
tar -zcvf $BKFILE $BKFOLDER > $BKRESULT
clear
echo backup saved to:
echo $BKFILE
echo and the results writed to:
echo $BKRESULT
exit
elif [ $OPT = "User-Difine" ]; then
echo "Please specify a name for the backup"
read FILENAME
tar -zcvf $BKDESTINATION$FILENAME.tar.gz $BKFOLDER > $BKDESTINATION$FILENAME.txt
clear
echo backup saved to:
echo $BKDESTINATION$FILENAME.tar.gz
echo and the results writed to:
echo $BKDESTINATION$FILENAME.txt
exit
else
clear
echo bad selection
exit
fi
done
elif [ $OPT = "Quit" ]; then
clear
echo Backup aborded by user
exit
else
clear
echo bad selection
exit
fi
done

 

save and close. Now make the file executable

chmod +x backup6.sh

execute that file with

./backup6.sh

Just a quick explanation of the script:

Here we have added a new variable with the name “LIST2” in order to create a new “select”. You already know that from the previous part of this tutorial.

When we need an input from the user we have to use the “read” command. When this command executed by the script it will wait an input from the user. It’s useful to use an “echo” command before the “read” command in order to give a sort explanation to the user of what the script is waiting from him. You can see that in my script:

echo “Please specify a name for the backup”
read FILENAME

after the “read” you have to type a variable so the read command can store the input of the user. This variable can be used just like normal variables.

Take a close view of the script. I believe that you will not have a problem to understand that script now.

Let’s go to the next part.


Bash Scripting for beginners Part 8

While and Sleep

There are times, that you have a folder that the contents of that folder are changing really fast and you don’t really have time to execute the backup every let’s say 30 seconds. Or your boss comes over your head and said “You know, I want backup of a folder every 5 seconds for the next 8 hours”. You will say ok, but what if you have to do something else in the mean time? Think Bash! That is just a story that made up and the persons of that story is completely out of my mind and that never happened to me and my boss never yell over my head! (Ok Boss? Do I still have my job?). I can promise you that after that you will have plenty of time! To do so let’s create a new script and name it backup7.sh

gedit ~/Desktop/bash/code/backup7.sh

write the following to the gedit :

#!/bin/bash
# Bash with allaboutlinux.eu
DATETIME="$(date +%d.%m.%Y_%H:%M:%S)"
BKFILE=~/Desktop/bash/backup/backup$DATETIME.tar.gz
BKFOLDER=~/Desktop/bash/code/
BKRESULT=~/Desktop/bash/backup/backup$DATETIME.txt
BKDESTINATION=~/Desktop/bash/backup/
LIST="Backup Quit"
LIST2="Date-Time User-Difine"
LIST3="Run-Once Run-multiple"
select OPT in $LIST3; do
if [ $OPT = "Run-Once" ]; then
select OPT in $LIST; do
if [ $OPT = "Backup" ]; then
echo please chose the filename of the backup:
select OPT in $LIST2; do
if [ $OPT = "Date-Time" ]; then
tar -zcvf $BKFILE $BKFOLDER > $BKRESULT
clear
echo backup saved to:
echo $BKFILE
echo and the results writed to:
echo $BKRESULT
exit
elif [ $OPT = "User-Difine" ]; then
echo "Please specify a name for the backup"
read FILENAME
tar -zcvf $BKDESTINATION$FILENAME.tar.gz $BKFOLDER > $BKDESTINATION$FILENAME.txt
clear
echo backup saved to:
echo $BKDESTINATION$FILENAME.tar.gz
echo and the results writed to:
echo $BKDESTINATION$FILENAME.txt
exit
else
clear
echo bad selection
exit
fi
done
elif [ $OPT = "Quit" ]; then
clear
echo Backup aborded by user
exit
else
clear
echo bad selection
exit
fi
done
elif [ $OPT = "Run-multiple" ]; then
echo "How many times would you like to backup $BKFOLDER ?"
read TIMES
echo "What is the time gap between the backups(in seconds)"?
read DELAY
while [ $TIMES -ne 0 ]; do
tar -zcvf $BKFILE$TIMES $BKFOLDER > $BKRESULT$TIMES
clear
echo backup saved to:
echo $BKFILE
echo and the results writed to:
echo $BKRESULT
echo truew $TIMES
sleep $DELAY
let TIMES=$TIMES-1
done
exit
else
clear
echo bad selection
exit
fi
done

 

save and close. Now make the file executable

chmod +x backup7.sh

execute that file with

./backup7.sh

Just a quick explanation of the script:

Now I have to add 2 more “read” commands to let the user decide how many times and how often to create backups. You already know about read so let’s go on to “While” statement. So let’s take a look first on the syntax of the “while” statement

while [ condition ]; do
code to execute, as long as the "condition" is true
done

most of the time in the “condition” we have an arithmetic variable and we increase or decrease the value of that variable until the “condition” in not true. We use While for example when we want to execute a set of commands for more than one time in a raw. You can also use a variable in the condition inside the while statement. If that is too complicated with the following example everything will be cleared.

Create a new script and name it while.sh

gedit ~/Desktop/bash/code/while.sh

write the following to gedit :

#!/bin/bash
counter=10
while [ $counter -ne 0 ]; do
echo $counter". Allaboutlinux.eu"
let counter=counter-1
done

save and close. Now make the file executable

chmod +x while.sh

after the execution of that script you will get something like the following:

10. Allaboutlinux.eu

9. Allaboutlinux.eu

…..

1. Allaboutlinux.eu

And now I will explain to you how to create conditions. The condition in the previous example is:

[ $counter -ne 0 ]

$counter is the variable defined in the first line of that example and contains the number “10”, “-ne” stands for “Not Equal” and the “0” is just a number. So the while is checking if the variable “counter” is not equal to “0” and executes the code within “do-done”.

NOTE: It’s really important to increase or decrease the value of the variable that is compared in the condition inside the “loop”. (by loop I mean the code that is executed more than one time inside the “do” and the “done” commands of the previous example). Otherwise the loop will never end!

You can also use some other conditions like:

-eq = Equal to

-gt = Greater Than

-ge = Greater than or Equal to

-lt = Less Than

-le = Less than or Equal to.

I hope that in this point everything is clear. I know that this maybe a little confusing at the begining but try to read the script again and again and you will understand it.

The other new command that I use in that script is the “sleep” command. It actually does what is says it “sleeps” (freeze the script at that point) as long as you want.

So the command “sleep 5” will freeze the script for 5 seconds and after that the execution will continue normally.

But that’s enough for a tutorial named “Bash script for beginners” if I go any further I should change the Title.

Hope you enjoyed that. Please leave comments if something is not that clear, or if you want to create scripts that will help others to understand better this tutorial.