Scripting in Linux – Writing scripts

In the previous two posts, I covered the basics of shell scripting. Here are the links:

1. Scripting in Linux
2. Scripting in Linux – Part 2

Today, we’ll use that knowledge to write a few scripts, understand their operation, and, most importantly, get you started with writing scripts on your own.

Script 1: Downloading files from a server
In this example, we’ll write a script that reduces your work when you have to download multiple files from a server. There are some prerequisites though:

  • You must have a direct download link to the file.
  • There must be some kind of a sequence in the remaining download links.

These conditions are usually satisfied. As an example, the other day I had to download a manga. The images hosted on the website’s server were something like:

http://c.mfcdn.net/store/manga/106/65-668.0/compressed/u001.jpg

The number coloured in red changed for every page and for every page it corresponded to the page number. Now for the script:

#!/bin/bash
echo "At what page number do you wish to begin?"
read firstpage

echo ""
echo "At what page number do you wish to finish? "
read lastpage

cd ~/Desktop/manga/

for(( i=firstpage; i<=lastpage; i++))
    do
    if [ "$i" -lt 10 ]
    then
        url="http://c.mfcdn.net/store/manga/106/65-668.0/compressed/u00"$a".jpg"
    else
        url="http://c.mfcdn.net/store/manga/106/65-668.0/compressed/u0"$a".jpg"
    fi
        wget -U Mozilla $url
    done

echo ""
echo "Complete!"

Explanation:
The first couple of lines just get the starting and beginning page numbers. In the line number 9, we change our current directory to the folder in which we want to keep our files. Then follows a loop. The loop itself just loops over numbers starting from firstpage and ending at lastpage.

The condition inside the loop is for a different reason. If you look at the url closely, you’ll see that if the page number is less than 10, we have to put the page number by adding a zero in front of the page number. The condition checks whether the page number is less than 10, and, if it is, then, a url with the ‘extra’ zero is saved into the variable called url. In case the number is greater than 10, then there is no need for that extra zero.

Then, in line 19, we have used the inbuilt wget command. The options -U Mozilla ‘disguises’ the wget utility as Mozilla Firefox to the website’s server. And voila! You’re done.

I’ll keep adding more scripts here whenever I come across something useful. So, you can come back and check this page periodically. Also, please share this post on facebook if possible.

Scripting in Linux – Part 2

In my last post we discussed what scripts are and how they make your life easier. We also saw a how to get a script up and running within no time.

Today, we’ll use that knowledge to tackle something more than a ‘Hello World’ example 🙂

We’ll start with variables in bash (the terminal). Note that the shell need not be bash. Anyway, first are the system variables. They are named in all capital alphabets. Examples would be PATH, BASH, HOME, USERNAME etc. Go here for a full list. These variables are maintained by Linux itself.

To access the value of any one of them, open the terminal (ctrl+alt+t) and type, for example, echo $PATH and hit the return key. You’ll see something like this:

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

These are the paths in your root directory that the OS looks in when you open a certain program.

Anyway, moving on to user defined variables. These variables are defined and initialized by you – the user. The general format is as follows:

var_name=value

Note that there are no spaces on either side of the = sign. That’s required. Unlike most of the programming languages that allow spaces, shell doesn’t. Also, there are some naming conventions. Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character.

Now, you can define variables. Now, we’ll move on to accepting input from the user. For this, the following syntax is used:

read var_name

So, for example, if you want the user to enter a number, you could use:

echo "Enter Number"
read number

Now we can move on to conditions and looping.
Conditions
The if condition:
Syntax

if condition
then
    statement1
    statement2
    ....
fi

Note: Unlike programming languages where a non negative value evaluates to true and a 0 value evaluates to false, in shell, it’s just the opposite.

Boolean Value Programming languages Linux Shell
True Non – Zero 0
False 0 Non – Zero

The conditions are a bit different compared to programming languages.
For comparing integers:

Expression in programming lang Usage in shell Example
== -eq if [ 5 -eq 6 ]
!= -ne if [ 5 -ne 6 ]
< -lt if [ 5 -lt 6 ]
> -gt if [ 5 -gt 6 ]
<= -le if [ 5 -le 6 ]
>= -ge if [ 5 -ge 6 ]

For strings, we have following expressions:

Condition Usage in shell Example
Strings are equal = if [ “$str1” = “$srt2” ]
Strings are unequal != if [ “$str1” != “$srt2” ]

And yes, that’s a single equals sign, not a ‘==’.
Similarly, we can have if-then-else style conditions:
Syntax

if condition
then
    statement1
    statement2
    ....
else
    statement3
    statement4
    ....
fi

Also, we can have multiple conditions checked using elif in between then and else blocks. See here
Also, note that the condition used here is provided in square brackets and the variables are put in double quotes
Now, we’ll move on to loops.

Loops:
The for loop:
Syntax:
Fortunately, the for loops executes just as a for loop in C/C++

for (( initialization; condition; counter ))
do
.....
.....
repeat all statements between do and done until condition is TRUE
done

Here, the condition is also the of the same type as used in C/C++ and Java etc.
Also, there’s another type of loop that can be used which is more python oriented:
Syntax:

for {variable name} in {list}
do
.....
.....
done

As an example

for i in {0..10..2}
do
.....
.....
done

Here the list is constructed similar to the range function in python. In this example, the list will be { 0, 2, 4, 6, 8 }.

The while loop:
Syntax

while [ condition ]
do
statement1
statement2
statement3
....
....
done

Note:Here the condition used is similar to that used with the ‘if’ condition.
As an example, to print numbers from 1 to 10:

count=1
while [ "$count" -le 10 ]
do
echo -e "$count \n"
count=$[count + 1]
done

The echo -e represents that the echo command will interpret the ‘\n’ as a new line character.
In the fifth line, where we have performed the increment in counter. To know more about shell arithmetic, see this page.

Okay. So, that will be it for today. In the next part, we’ll see some real world examples of using scripts. Go here for the next part.

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.