Scripting in Linux

Using scripts in Linux to simplify tasks

Okay. So, officially, this is my second post. Today, I will show you how to use scripts in a Linux environment to simplify (and do more) with many repetitive tasks.
As a first thing, I’d like to tell what scripts are.
Scripts are small programs written for automating tasks which could alternatively be performed by a human in a step by step manner.
Enough with the formalities. So, where can you use scripts? Well, let’s see. The other day I needed to download a couple of pictures (more like 500 pictures) from a certain website. The website supported online viewing of the images but they did not support downloading the images. Of course you could save the images one by one but you wouldn’t want that now, would you? So, what do I do? That’s right. I use a script for pulling the images off the website’s server 🙂
Similarly, you can have a situation where you have a folder full of files whose names start with ‘Copy of’. Such as – ‘Copy of xyz.mp3’. Now, that’s not pleasing to the eye at all. Here also, you can use a bit of advanced scripting to rename all files such that the ‘Copy of’ part is removed.
Okay. Now to work. We’ll start with an example and work out the steps as we move along. Make a file. Call it script.sh and then type in the following content in it.

#!/bin/bash
echo "Hello World!"

Now comes the analysis part. The first line is what is called a shebang. Now don’t laugh. That’s what it is called. This lines tells Linux where to look for the program that executes this script. The next line, as you would’ve guessed by now, prints to the screen.
Note: It is possible to write this output to a file rather than your screen since Linux considers the screen also as a file. More on this later
Now change the permission of the file so that it becomes executable. For this, open the terminal (Ctrl+alt+t) and navigate to the folder containing the file and enter:

chmod 755 script.sh

This sets the permission to read-write-execute for the user and to read-execute for all other users. Alternatively, you could right-click the file and go to properties and change the permission to make it executable.
Now, navigate to the folder containing the file and in the terminal enter:

./script.sh

You should see a hello world message!
That’s it for the time being. Come back later as we tackle something more complex. Go here for part 2.

Leave a comment