Creating Bash Scripts Choices And Loops – Part 4
You’ve
encountered user interaction with your scripts, asking what the user’s name is and so
on. You’ve also looked at creating loops within the script to either count or simply do
something several times. Let’s combine and expand some more.
Let’s
bring in another command, CHOICE, along with some nested IF and ELSE
statements. Start by creating a new script called mychoice.sh.
Step
1
The
mychoice.sh script is beginning
to look a lot more
complex. What we have here
is a list of four
choices, with three
possible options. The options:
Mint, Is, and Awesome
will be displayed if
the user presses the
correct option key.
If not, then the menu will
reappear, the fourth
choice.
Step 2
If you follow
the script through you soon get the hang of what’s
going on, based on what we’ve already
covered. WHILE, IF, and ELSE, with the FI closing loop statement
will run through the options and bring you back to the start if you
pick the wrong option.
Step 3
You can, of
course, increase the number of choices but you need
to make sure that you match the number of
choices to the number of IF statements. The script can quickly
become a very busy screen to look at. This lengthy script is another
way of displaying a menu, this time with a fancy colour scheme too.
Step 4
You can use
the arrow keys
and Enter in the menu
setup in the script. Each choice is an
external command that feeds back
various information. Play around with
the commands and choices, and
see what you can come up with. It’s
a bit beyond what we’ve looked at but
it gives a good idea of what can be
achieved.
Creating A Backup Task Script
One of the most well used examples of Bash scripting is the
creation of a backup routine, one that automates the task as well as adding some customisations along the way.
Step 1
A very basic
backup script would look something along the
lines of: #!/bin/bash, then,
tar cvfz
~/backups/my-backup.tgz ~/Documents/. This will create a
compressed file backup of the ~/Documents folder, with
everything in it, and put it in a folder called /backups with the name
my-backup.tgz.
Step 2
While
perfectly fine, we can make the simple script a lot more
interactive. Let’s begin with defining
some
variables. Enter the text in the screenshot into a new backup. sh script.
Notice that we’ve misspelt ‘source’ as ‘sauce’, this is because there’s
already a built-in command called ‘source’ hence the different
spelling on our part.
Step 3
The previous
script entries allowed you to create a Time Stamp,
so you know when the backup was
taken. You
also created a ‘dest’ variable, which is the folder where the backup
file will be created (~/backups). You can now add a section of
code to first check if the ~/backups folder exits, if not, then it
creates one.
Step 4
Once the
~/backups folder is created, we can now create a new
subfolder within it based on the
Time Stamp
variables you set up at the beginning. Add mkdir -p $dest/”$day
$month $year”. It’s in here that you put the backup file
relevant to that day/month/year.
Step 5
With
everything in place, you can now enter the actual backup
routine, based on the Tar command from Step 5.
Combined with the variables, you have: tar cvfz $dest/”$day
$month $year”/DocumentsBackup.tgz $sauce.
In the screenshot, we added a handy “Now backing up...” echo command.
Step 6
Finally, you
can add a friendly message: echo “Backup
complete. All done...”. The completed
script isn’t too over-complex and it can be easily customised to
include any folder within your Home area, as well as the entire
Home area itself.
Step 3
ReplyDeleteScript is a scan in a to low resolution and to small font. It's impossible to read. Here on the website and also in the magazine from BDM's i-Tech Special "Coding for Beginners" Volume 31.
Even increasing font to maximum in Firefox brings no improvement