An Amateur’s guide to Bash scripting

An Amateur’s guide to Bash scripting

Bash scripts are one of the easiest linux programming languages to learn. Bash scripts are handy when you want to automate tasks like a server backup or email the IP address of your Raspberry Pi server on boot.

In this post you will see how to write a simple bash script and then proceed to some intermediate bash scripting examples.

Let’s get started with a simple script to tell will tell you the disk usage in the current directory

First we need to reate a file called “disk_usage” and add the following code.

[user@linux-vps ~]$vim disk_usage

#!/bin/bash
#Author Leo G

du -sh *

In Linux in order to run scripts the execut bit or execute permissions must be set.

chmod +x disk_usage

./disk_usage

learn bash scripting

Explanation of the disk usage script.

Let’s take each line of the script and understand how it work’s.

#!/bin/bash : This line indicates that the bash interpreter should be used to compile the script. In simple words our script will be translated to machine code by a translator called BASH.

#Author Leo G : This “#” symbol in our script indicates a comment. The exception to this is the first line “#!/bin/bash”.

du -sh * : This line as you might have guessed contains the disk usage command for the current directories.

That’s it, you can replace “du -sh *” with any command you like, You can even see how the script works by adding ” -x” to “#!/bin/bash” .

[user@linux-vps ~]$ vim disk_usage
#!/bin/bash -x
#Author Leo G

du -sh *

You use “-x” when you want to debug any bash script.

Running multiple commands

You can run multiple commands by adding them one after the other on the next line

#!/bin/bash
#Author Leo G

du -sh *
ls -l
echo " This script is done executing and you now know how to write a basic bash script"

You need to ensure that add them on the next line, else you will need to use a semi colon after each command on the same line as this indicates to the BASH interpeter the end of the command.

The “echo” bash command

The command “echo” will allow you to print messagea to the screen, ensure you enclose words with spaces in double quotes.

Bash Script to read user input

Before we go into user input, we need to learn about variables.

What are variables in bash?
Well variables are there in any programming language and they are used to store data.

If you need to find the disk usage of multiple files in different directories, rather than running our script in each of those directories you simply input the path of the file and get it’s disk usage

Let’s see how it works,

 #!/bin/bash
#Author Leo G

echo " Enter your directory: "
read x
du -sh "$x"

This script will prompt you for an input, enter the path to a file in the format \path\file and type enter.

In this script the “read” command get’s your data and store’s the same in x. We want to find the disk usage of the value stored inside x and not of x, hence we prefix x with a dollar sign.

learn bash scripting

Here’s a little homework, figure out how you can create a variable without using “read”.

I am going to end this post with another practical Bash script example .

Backup Bash Script.

We want to achieve two things with this bash script, one take daily backup’s and two send out an email when the backup’s are complete.


[user@linux-vps ~]$vim backup.sh 

#!/bin/bash
#Author Leo G

rsync -avz  -e "ssh " /path/to/yourfile user@backupserver.com:/backup/

echo "backup for $(date) "| mail -s "backup complete" user@youremail.com

Replace the path with your path and server and run the script.

Next, add the script to a cronjob which will run daily.

[user@linux-vps ~]$crontab -e

00 1 * * * /pathtp/backup.sh

This script will run “rsync” first and then send an email to user@youremail.com everyday at 1 am.

Note: There is no error check in the script and I know a common question that may have come to your mind, What if rsync fails?. We will tackle this issue with error codes in my next post.

I like to keep posts simple, with less content and more practice. So do the homework I told you and feel free to ask any questions or doubts in the comments.

Read Part 2

A detailed guide on bash scripting can be found at http://bash.cyberciti.biz/guide/Main_Page.

Digiprove sealCopyright secured by Digiprove © 2014 Leo G
Follow me

Leo G

Is a Team Leader, Linux administrator and programmer. He has over 8 years experience in administering Linux servers and loves to share his experience and knowledge via his blogs.
Follow me

Similar Posts
Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting part 2
Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting part 2
Bash scripts are one of the easiest linux programming languages to learn. Bash scripts are handy when you want to...
Here is How I Built my First RPM
Here is How I Built my First RPM
Bash scripts are one of the easiest linux programming languages to learn. Bash scripts are handy when you want to...
What Does echo 1>&2 mean
Bash scripts are one of the easiest linux programming languages to learn. Bash scripts are handy when you want to...

1 Comment

Leave a Reply


Name (required)

Email (required)

Website

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>