Search

Practice Scripts

Try out the following scripts


Lets try writing different scripts using what we have learnt till now.

Create a file named are "file1" with the following contents

"The command line is very powerful and fast.Once you get used to the using the
commands in commmand line, a lot of things become very easy to do.
The graphical user interface is slow as well as very restrivtive in its options.
 Learn the commmand line it will help in automating a number of tasks.The
command line also requires lesser amount of memory resource as compared to  
the GUI"


Questions:
1. Write a script to that will ask the user for a string and count the number of lines that have the string in it.

2. Write a script to that will ask the user for a string and count the number of times the string appears in file1.

3. Write a script that will ask the user for a string and output all the lines that do not have the string in them.

4. write a script that will take a number as the input and output only that many lines from the file1, from the top.



Solutions:

1.
#!/bin/bash
# A script to count the number of lines that have a certain word.

read string
echo "Searching for $string"
count=$(grep -c  $string file1)
echo "There are $count lines with the word $string"

2.
#!/bin/bash
# A script to count the number of times a string appears.

read string
echo "Searching for $string"
count=$(grep -o $string | grep -c)
echo "There are $count occurences of $string in file1"

3.
#!/bin/bash
# A script to output the lines that do not contain a certain string

echo "Enter the string"
read string

grep -v $string file1

4.
 #!/bin/bash
# A script that will output specific number of lines of the file1

echo "Enter the number of lines needed in the ouput"
read number

head $number file1

What is Operating System

This series of articles aim at helping the non technical people make sense of the commonly used Technical terms.

Antie is a character with no technical knowledge , who will be exploring the technical world.
Tiny is the techie who antie turns to for all the technical help.

Antie:  Hi Tiny, one of my friend confused me yesterday completely. 
Tiny :  What happened Antie ? What did you get confused on  ? 
Antie: When I told them that I will be buying a laptop, he asked me which Operating system was I getting with it? 
Tiny: OK, so what did you say? 
Anite:  I did not know, so I said I had no idea and he was like, don't take Vista, take windows 7 blah blah...
Tiny (Smiling) : Ok, I got what you are trying to say. Let me first tell you what an operating system or generally called as OS is. 
Anite:(Sitting with a mug of coffee) That would help. 
Tiny: In the company that you work for, who takes all the decisions? 
Anite:  What decisions ?
Tiny:  Any important ones, like how many new people to be recruited, who gets to do what work etc etc. 
Antie: Well all the big decisions come from the CEO. 
Tiny: So can we say CEO controls the your office ? 
Antie:(Nodding) Yes you can say so. 
Tiny: That is exactly what an operating system does, it controls every thing in your laptop. 
Antie: Oh ok. 
Tiny: Its the OS that decides what should be done and how. For example when you put a movie CD into the laptop, its the OS that finds out what is there in the CD and how should it be played.
Anite: Ah, now I get it. I thought it was some kind of magic that the computer knew exactly what to do every time.
Tiny: No Anite, there is no magic, its just the OS.
Antie: So what is all this about Vista, Windows 7 etc?
Tiny: You have options to choose who will be the boss in your laptop. You must have heard of the company windows and Bill gates.
Antie: Yes the rich guy, yes.
Tiny: He runs a company called Microsoft which make one of the most used OS.  Windows 7 is the latest one the company has launched, Vista was the one before it.
Anite: So we need to buy one of these OS to make our laptops work , right ?
Tiny: If you want any of the windows OS yes you need to buy.
Antie: What do you mean, is there any other company too ?
Tiny:  There is another OS called as Linux, that comes for free. Its as good as windows is and any one can use it for free.
Antie: Then why should any one pay for Windows.
Tiny: Linux was not always so easy to use as it is today, so people are still scared to try it. But sooner than later people more people will move to it.
Antie:(smiling) Cool then, I too will try out Linux it self. Thanks Tiny for saving me some money, and here are your strawberry filled chocolates. 

‘cout’ was not declared in this scope

After installing g++ if you get the error
‘cout’ was not declared in this scope 
or similar for "cin"
Then the work around is to add using namespace std; after the "#include"

Memory hierarchy



Memory Hierarchy




Memory Hierarchy gives the relationship between the speed,size and cost with respect to the distance from the Processor.
In the diagram above the peak of the pyramid represents the processor. The Register lie with in the processor it self hence are the closest to the processor and work the fastest. But the number of registers that can be included in a processor are limited, as it would lead to increase in processor size, increase in manufacturing cost etc. Thus the register memory is restricted to minimal.

A level below the register is the L1 cache or the first level cache. In the processors of today, L1 cache also lies on the processor chip itself, though it might lie outside too.
The cache memory works at a very fast speed but is also extremely expensive as compared to the other memories available. This high cost is one of the major restriction why we can not use lots of cache in a computer even thought it is faster.
The general thumb rule is, higher the cache memory faster would be the working of the processor.

After the L1 or first level of cache, modern systems have an L2 or the second level of cache.This is introduced mainly because we can not include a high amount of L1 cache when we are making L1 a part of the chip as it would occupy precious space on the chip.
L2 cache is mostly off the chip. But it is of the same kind as the L1 cache and hence is extremely fast in its operation. Together L1 and L2 help in making the system run fast. Some of the modern computers also have a third level or L3 cache.
Cost factor again restricts the amount of second level cache can be included.

All the cache though does make the system work faster but is of very limited in size, which is not really enough for the computer to function.

The next level of memory is the RAM. RAM basically stores all the data that the computer needs while it is running. RAM is also fast in its operation but slower than the cache, and less expensive. Thus we can afford to have a relatively large amount of RAM.  The higher the RAM, faster is the system, but again price is a main limiting factor with regards to how much RAM can be included in the computer.

All the memories until the level of RAM are volatile, i.e. they loose the data stored in them as soon as power is switched off. This obviously is not a very desirable property as you would loose all your work.
Thus to store data even when power is not there secondary memory is made use of. The secondary memory is the slowest but the size is a lot bigger than the RAM or the cache because price of the secondary memory is a lot lesser than compared to RAM and cache.

Let us take a real life example to understand the above hierarchy .
Consider the laptop SL400c by lenovo.
The configuration is :
Intel T5870 Processor.
L1 cache: 32KB
L2 Cache : 2048KB
RAM: 2GB
Secondary Memory: 160GB

The above data depicts the memory hierarchy clearly, L1 cache being the least and the secondary memory being the largest.

Scripting -3 Variables

Aim: Introduction to the usage of variables in shell scripting.

 The concept of variables is common in the programming world. A variable is basically used to hold or store a value, which can change dynamically while the program or the script is running.
The value that is stored in the variable can later be used any where in the script or program by referring the variable.
The syntax of assigning a value to a variable in bash scripting is as follows.

Variable_Name=Value


There should not be any space between the variable name and "=" and also no space between "=" and value.

The value on the left is the name of the variable which has to follow the following rules.
1. It must start with a letter or an Under score.
2. Subsequent characters(After the first one) can be letters,numbers or underscore.
3. Spaces can NOT be used in variable names

Eg :
Valid Variable names : num1, _num1, n1n1.
Invalid  Variable names: 1num, num 1.

The variables are case sensitive, i.e. num1 is not same as NUM1.


In bash scripting to use a variable, once a value is assigned to it, we need the append the name of the variable with a "$" symbol i.e. $variable_name.

Echo can also be used to print the value of a variable

Let't try writing a script using a variable.

#!/bin/sh
var1=10
echo "The value of the variable is"
echo $var1


Type the above lines into a file and save it under the name script3.sh
Use chmod to give execute permission to it.

$ ./script3.sh 
output:
The value of the variable is 
10


The value "10" was saved in the variable "var1", which was later accessed by the echo command by perpending a "$" before the name i.e. $var1. 




A variable can hold any values i.e not only numbers you can assign words, sentences etc to a variable. 


For eg: 


#! /bin/bash
var1="harry" 
echo "His name is $var1"


Write the above script into a file and save it as script4.sh
Run the script after giving execute permission to it.

$ ./script4.sh
Output:
His name is harry


From the above example we see that the variable can be used along with any text of the echo command, only thing we need to make sure is that the name is perpended with a "$" and the whole sentence is inside double quotes.

A script can also be assigned sentences as shown in the example below.

#!/bin/bash 
var1="His name is harry"
echo $var1


Write the above script into a file and save it as script5.sh
Run the script after giving execute permission to it.

$ ./script5.sh 
Output:
His name is harry


Now try this script


#!/bin/bash 
var1=His name is harry
echo $var1


Save the above script as script6.sh.
What do you expect the output to be?

$./script6.sh 
Output:
script6.sh: 2: name: not found


Can you guess why is this ?

The assignment to variable does not take spaces if you remove the double quotes. So "var1" gets assigned "His".  The script then treats the string next to his i.e. "name" as a command, but there is no command called as "name", hence we get the error.

You can also assign commands to variables as shown in the script below.

#!/bin/bash 
list=ls
$list


Save the above script as script7.sh. On executing the script you should see the same output as you would if you executed the command "ls" in your current directory.
The variable "list" get assigned with the value "ls". When the variable by it self is used in the next line, its same as using a command. With the $ perpended before "list"  $list becomes "ls", which in turn gets executed as usual.

You can also club multiple commands into one variable as shown in the script below.

#!/bin/bash
store="ls > file"
$store

Save the above script as script8.sh, guess what would be the output.

De-assigning a Variable


In case you want to remove the value the you have assigned to a variable, you can use the command "unset"

syntax:
$ unset variable_name


For eg:
$ var1=10
$ echo $var1
10
$ unset $var1
$ echo $var1


$

In the above sequence of commands, we first assign the value of 10 to the variable "var1", which we display using the command "echo".
Then using the "unset" command on the variable "var1",  it gets de-assigned.
After the "unset" we try using he "echo" command again on "var1" but it does not display any value thus proving that the value has been de-assigned.


Taking Input from the user 


Until now we have seen scripts that set the value of the variable in the script it self. But a variable's real use is when we can allow it to be modified by the user.
In a number of situations we would need the user to provide us with certain data with which we need to work in the script. For situations like this, when we need to take the input from the user, we can use the command "read".

syntax:
read variable_name


The read commands basically waits for the user to enter a value, which in turn gets assigned to the "variable" passed to the read command.
For eg:

#!/bin/bash
echo "What is your name?"
read name
echo "My name is $name"

Save the above script as script8.sh and execute it.

$ ./script8.sh 
Output:
what is your name 
( The script waits here with a blinking cursor, as long as you don't  enter a value. The value entered in turn gets assigned to "name" )
Harry (Assume we entered this)
My name is Harry

Note:  "echo" by default inserts a new line at the end, in case you don't want the new line and want to continue in the same line use the option     "-n" with echo, i.e. "echo -n"




More scripts 


Write a script that will ask the user for a filename, and output the content of the file.
Note: Assume that the user enters a file that exists in the current directory, we will see how we can do a check whether the file is existing or not a little later. 


#!/bin/bash
echo "Enter the filename" 
read file
cat $file


Save it as script9.sh, and run it to check whether you get the desired output.


Write a script that will take the name,phone number and email-id of a user one by one and later display it all together.

#!/bin/bash 
echo -n "Hello, enter your name: " 
read name
echo -n "Enter your phone number :" 
read number 
echo -n  "Enter your email id: " 
read email 
echo "Thank you $name, your phone number is $number and your email is
           $email"



Save the script as script10.sh, and run it.

Output:

$ ./script10.sh 
Hello, enter your name: Harry
Enter your phone number : 9999
Enter your email id: abc@abc.com

Thank you Harry, your phone number is 9999 and your email is abc@abc.com

Notice that in the above script we have used the "-n" option with echo, thus the input prompt stays on the same line after the echo. 
Also notice that there are not limits on how many times you can use the read command in a script, neither is there are limit of where you can use the command.

Ways to reference Variables : 

There are three ways you can reference a variable and access its value.
If var1 is the name of the variable

1. $var1
2. ${var1}
3. {$var1}

In all the three methods we will get the value stored in the variable.

For eg:

#!/bin/bash 
var1=10
echo $var1
echo {$var1}
echo ${var1} 

output:
10
{10}
10

Quotes:


While scripting you will come across three kinds of quotes.Each of these quotes have their own meaning.

1. " "The double quote
       With in double quotes every special character holds its special meaning.
        For eg:
        "$" is used to before a variable to print its value, with in double quotes the "$"  
         will still function the same.
          For Eg:
          #!/bin/bash
           var1=10
          echo "Value is $var1" 
           
          Output
          10


2. ' Single quote
      Any special symbol placed between the single quotes looses its special meaning.
          #!/bin/bash
           var1=10
          echo "Value is $var1" 
           
          Output 
          $var1

3. ` Backtick
     A backtick is also called as the command substitution. Only a command or a script can be  placed between the backticks. The script or the command gets executed and the value gets replaced at the place.
      For eg:

#!/bin/bash

          echo `cat file1` 
           
          Output 
          "contents of file1"

In the above script we placed the command "cat file1" between the backticks, thus instead of printing the string "cat file1" the command "cat file1" got executed, whose contents in turn were passed to echo, thus echo put out the contents of file1 on to the screen. 







Processor and its Speed

Clock Speed


Antie: Hi, tiny how are you today.
Tiny: Doing greaty Antie, so did you buy the laptop.
Antie: Oh no, not untill I understand every bit of the terms that they have on their pamplets.
Tiny:(laughing) You are always the curious one arn't you.
Antie: Well sure I am.Ok Tiny, you told me what RAM is all about and why it is better to have more RAM than less.
       Now there is another term that I have seen a lot, processor and attached with it is always a number, some thing like 2GHz etc.
Tiny: A processor is like the brain Antie. It is what that controls every thing with in your computer. Just as every thing that we think is using our brain, every thing that a computer is controlled by the processor.
Anite: That is very interesting, I always wondered how is that the computers are so smart and seem to do every thing by themselves. Now I know who does the job.
Tiny: Yes Antie, with out a processor a computer would just be a dumb box.
Antie: Hmmm, then what is number attached to it.
Tiny: We can say it is like the speed of thinking. The faster we think the faster we can work isn't it.
Anite: Yes, I always used to be the first to solve the Maths problems in class.
Tiny: You were always a smart one Antie. The number attached with the processor is called as the clock speed. The higher the clock speed the faster the   processor can do its job. The faster your computer will work.
Anite: Ah, so if we want a computer that works fast we should by one with a higher speed atacehd with the processor.
Tiny: Yes, Antie. But just the processor speed will not help. Remeber the RAM? Even if you can think fast but there is no space to keep your files on the table then what is the use. Same way even if you have a processor with high speed but there is no RAM to give it data quickly, your computer will still    work slowly.
Anti: Yes, that makes sense. So we need to have a high speed processor and a big size of RAM too to make the compute work fast.
Tiny: Exactly Antie.

Scripting-2 The first Script

Aim : To introduce the reader to writing shell scripts

The shebang: 


If you have seen shell scripts in the past. You would have seen that most professionally written scripts have some thing like #!/bin/sh or #!/bin/bash. This is called as the shebang.
A shebang basically tells the which shell interpreter should be used to interpret the script. A standard linux system would generally have all the shell installed in it, though it might use one of them as its default shell.
Let us assume that user A writes a script using the "bash" shell and gives it to user B whose linux system uses a C shell. The script would obviously fail to run in user B's system even if the system has bash shell installed. Because user B is using the C shell by default. Easiest way to get around this problem is using the shebang.
shebang basically points to the location where the executable of the interpreter  is located. So irrespective of which default shell your system uses, if the path in the shebang is correct the script will be able to run correctly as it will know which interpreter should be used.

What is a script: 


A script is nothing but commands put into  a file and executed together. The commands could be any of the commands available in Linux.

Assume you have to create 100 files in a directory. The command to create a file is touch "filename". 
Let us say you are creating the files by the name file1, fil2.. etc. If you have to do manually on the command line you would have to execute this command a hundred times. But in a script its just a matter of 3 lines

#!/bin/bash

for((i=0;i<100;i++))
do
touch file$i
done

Write the above lines into a file and save it as magic.sh in the directory where you want to create the 100 files.

Open the terminal and "cd" into the directory and run the script as follows
bash magic.sh


Do "ls" and you will a 100 files created in your directory.

Don't worry about how the script works, we will learn that as we go.

We can see that a script can save a lot of time, if you are going to do some work repeatedly.

A script is also helpful in automating work, as multiple commands can be run together in a script.


First Script: 


Lets start by writing a simple script. using the commands "echo". 


#!/bin/sh 
echo "Hello world"


Type the above lines into a file using "vi" or any editor that you are comfortable with and save the script as script1.sh

Open a terminal and "cd" to the folder where you have saved the script.

There  are two ways you can run a script.

1. You could pass your script file to the interpreter which are either "sh" for bourne shell or "bash" for bourne again shell, "csh" for cshell.
   i.e.  sh "scriptname"
          bash "scriptname"
           csh "scriptname".

Note: It is a good practice to save the script as "filename".sh  though the script would work even if not saved with ".sh" extension.

2. The other way of running the script is giving the execute permission to the script using the "chmod" command. i.e.
chmod 777 "filename".sh  

and then to run the script

./filename.sh 


For our first script let's try doing both.
First step before doing that would be to find out which shell are you running currently. The path to your default shell interpreter is stored in an environment variable SHELL.


echo $SHELL
/bin/bash


The above output shows that the shell is a bash shell. Your output might defer depending on what shell is being used.

Note: We will come to what variables and environment variables are a little later so don't worry if you did not understand what SHELL variable is all about. 


You can run the script1 now (assuming a bash shell) as follows.

Note: "$" will be used from here on to indicate the shell prompt. 


$ bash script1.sh
output: 
Hello world


The second way of running the script is to give execute permission to it.

$ chmod 777 script1.sh


Once the execute permission is given, we can run the script as follows.


$ ./script1.sh
Ouput:
Hello world 


Second Script
#!/bin/sh 
echo "Hello world" > file1
cat file1


Type the above lines into a file using the "vi" editor and save it as script2.sh

To run the script, open a terminal and "cd"  to the folder in which you have saved the script.  Run the script using the command

sh script2.sh 

What do you expect the output to be ?

We know that echo will send "Hello world" to the output, which has been redirected using ">" to a new file  called as file1.
The "cat" command sends the contents of the file1 to the output i.e the terminal.

So the output should be the contents of the file file1, which is "Hello World". So you should see "Hello world" printed on the screen when you run this script.



The only difference between the two ways of running the scripts are, in the former a sub shell is created in which the script gets executed, in the later method no sub shell is created.


Try it out:  
1. The command "uname" with various options gives information about your system.
For eg:  If you run the command "uname" with no options, it will display the current operating system.
"uname -m" will display what architecture your computer belongs to i.e i386 or i686 etc.
Write a script that will give the following output

Hello, the operating system in this computer is 
Linux
It belongs to the family 
i686

The second and fourth line of the output might vary depending on what is installed in your computer and what kind of hardware is present in the computer.

2. The command "who" gives the list of users currently logged in to your system.
Write a script that will give the following output

The users who are working now in my system are 
user1  tty7 2010-6-12 11:23 (:0)  


The second line of the output depends on your sour log in name, terminal etc and will vary from one system to another.


Following a good book to start learning shell scripting

Beginning Shell Scripting (Programmer to Programmer)

What is RAM ?

This series of articles aim at helping the non technical people make sense of the commonly used Technical terms.
Antie is a character with no technical knowledge , who will be exploring the technical world.
Tiny is the techie who antie turns to for all the technical help.
RAM: 


Antie:  Hi tiny, was wondering about buying a laptop
Tiny: Cool Antie, which one?
Antie: I don't know, yesterday I went to the showroom and my head started spining.
Tiny: (laughing)  Well Antie, I understand there are just so many options.
Antie:  No Tiny its not the options in laptops, its the different technical terms that were using. I just could not make head and tail of it.
Tiny: Oh, let see then. Tell me one by one and I will tell you what they mean.
Antie:(smiling). ok Lets start with some thing called as  RAM. Every one kept asking how much RAM are you looking for? I would have rammed them on their heads if they had asked once more.
Tiny:(laughing) Lucky guys they got spared. Well let me explain to you what RAM is all about and why is it important to look at RAM when buying a laptop or a desktop.
In your office you have so many files, each file having different data in it.
 Where do you keep all the files?
Anite: I have a shelf to store all files, where I arrange them.
Tiny: At any given time you would only work with one or two files isn't it?
Antie: Yes.
Tiny: Where do you keep those files ?
Antie: On my desk. I pick which ever file I want from the shelf and keep them on the desk while  I work.
Tiny: Yes, obviously because you don't want to keep moving from the shelf to your table to look at the files. Now suppose that you need 10 files at the same time for your work. But your table can hold only 5 at time. Then what would you do ?
Antie: Hmmm that is problem I do face at times. Then I just work with the 5 on my table and keep shifting the files i finish work with back to the shelf and get the new ones on the table.
Tiny: A bigger table would obviously make life a lot easier for you won't it.
Anite: Yes if I could afford one, now how is any of this related to RAM?
Tiny:That is what I am coming to.  The shelf where you keep all the files is similar to the memory in your laptop where all the data gets stored. The data lies in that memory when not being used just as your files that lie in the shelf when not used. This memory is called as the secondary memory in computer terms.
But when you need some data out of this memory, the data is moved into a temporary storage where you can work on the data, just like you move the file on to your table. This temporary storage where data is moved to while working on it is termed as the RAM. Which stands for Random access memory.  As you said if you had a bigger table you could have kept more files on the table and saved the time that you spend moving from shelf to the table, same way the bigger the RAM in your laptop more the data that can be stored in it while working. If the RAM becomes small the system has to repeatedly keep swapping the data from the secondary memory to the RAM just as you swap files from the desk to shelf.
Antie: Oh, Ok now that explains some things all right. But what is it that they use to measure the size of this RAM. I have seen some thing like RAM of 2GB,1GB etc. What does that mean?
Tiny: That is the unit that is used to measure the data in any computer. For eg: we use grams,pounds to measure weights or Meter,Centimeter to measure distance. Similarly a unit called bytes is used to measure the data in the computer. 1GB is 1 Giga byte that is 10 to the power of 6 bytes. The bigger this number the more the storage capacity of the RAM. Say your table is 1 square meter, and you buy a new table of 4square meter obviously you can keep more files on the new table. Similarly the higher the bytes, more the storage capacity of the RAM but also higher will the cost of your computer.
Antie: Ah, now I get it. That does make things a lot clearer. Though there are a lot more terms that they kept throwing at me. This is enough for today. I will come back again tomorrow.
Tiny: Any time Antie, would be glad to tell you about it, as long as you keep giving me your home made chocolates.
Antie:(Giving Tiny a box). Here they are special strawberry filled chocolates. Try them out.
Walking  away.