Creating Bash Scripts Easy Automation And Handy Script – Part 5
The
backup script we looked at previously can be further amended to incorporate choices,
user-interaction with regards to where the backup file will be copied to and so on.
Automating tasks is one of the main benefits of Bash scripting, a simple script can help
you out in many ways.
Entering line after line of commands to retrieve system
information, find a file or rename a batch of files? A script is a better answer.
Step 1
Let’s start
by creating a script to help display the Mint system
information; always a handy thing
to have.
Create a new script called sysinfo.sh and
enter the following
into Xed, or the text editor of your choice.
Step 2
We’ve
included a couple of extra commands in this script.
The first is the -e extension for echo, this means it’ll
enable echo interpretation of additional instances of a new
line, as well as other special characters. The proceeding ‘31;43m’
element enables colour for foreground and background.
Step 3
Each of the
sections runs a different Terminal command,
outputting the results under the appropriate
heading. You can include a lot more, such as the current aliases being
used in the system, the current time and date and so on. Plus, you
could also pipe all that information into a handy HTML file, ready
to be viewed in a browser.
Step 4
Although
there are simple Terminal commands to help you look
for a particular file or folder, it’s often more fun to
create a script to help you. Plus, you can use that script for other
non-technical users. Create a new script called look4.sh, entering the
content from the screenshot below.
Step 5
When executed
the script waits for input from the user, in this
case the file extension, such as jpg, mp4 and so
on. It’s not very friendly though. Let’s make it a little friendlier.
Add an echo, with: echo -n “Please enter the extension of
the file you’re looking for: “, just before the
read command.
Step 6
Here’s an
interesting, fun kind of script using the app espeak.
Install espeak with sudo apt-get install espeak,
then enter the text below into a new script called speak.sh.
As you can see it’s a rehash of the first greeting script we ran. Only
this time, it uses the variables in the espeak output.
Step 7
We briefly
looked at putting some colours in the output for
our scripts. Whilst it’s too long to dig a little deeper
into the colour options, here’s a script that outputs what’s
available. Create a new script called colours.sh and
enter the text (see
below) into it.
Step 8
The output
from colours.sh can, of course, be mixed
together, bringing different effects depending on
what you want to the output to say. For example, white text in
a red background flashing (or blinking). Sadly the blinking
effect doesn’t work on all Terminals, so you may need to change
to a different Terminal.
Step 9
Whilst we’re
on making fancy scripts, how about using Zenity
to output a graphical interface? Enter what
you see below into a new script, mmenu.sh.
Make it executable
and then run it. You should have a couple of dialogue boxes appear,
followed by a final message.
Step 10
While gaming
in a Bash script isn’t something that’s often
touched upon, it is entirely possible, albeit, a
little basic. If you fancy playing a game, enter wget http://bruxy.regnet.cz/linux/housenka/housenka.sh,
make the
script executable and run it. It’s in Polish, written by Martin
Bruchanov but we’re sure you can modify it. Hint: the title screen is in
Base64.
0 Response to "Creating Bash Scripts Easy Automation And Handy Script – Part 5"
Post a Comment