Thursday, March 3, 2011

The power of the bash

I am going to show you a script that was found useful to me for testing a piece of code I wrote for a particular job.
Actually, that job, takes some data and outputs a single result. It is not necessary for you to know what the program does, because it doesn't affect what I am going to show you.
In addition this article shows the power of the command 'grep'! You can find about grep by typing 'man grep' on your terminal.

Well, the piece of code I wrote it was nothing new. It actually exists on linux. So to test it I should get some data, give it to the linux program and to my program and compare the results. The data might be numbers, characters or whatever. So I need a program from linux that each time is called it outputs different data. That program is actually a game and is called 'fortune'.

The code I wanted to test was written in java so the bash script I wrote was:

0:   #!/bin/bash
1:   for ((i=1;i<=10;i++)); do
2:   data=`fortune`
3:   myprogram=`echo "$data" | java myJavaProgram`
4:   linuxprogram=`echo "$data" | name-of-linux-program`
5:   similarity=`echo "$linuxprogram" | tr [:space:] \n | grep "$myprogram"`
6:   if [ -z "$similarity" ]; then
7:    echo "Program is wrong"
8:   else echo "Program is right"
9:   fi
10: echo $myprogram   $linuxprogram
11: done

*lines was put for discription only
Line 0: defining that the code will be executed with bash
Line 1: a for loop which will be executed 10 times because this is the number of times I
want to test my program.
Line 2: the variable 'data' will curry the output of fortune
Line 3: the variable 'myprogram' will curry the data of my programs output which will process the data from variable 'data'
Line 4: the same with line 3 but this time with the finished linux program
Line 5: 'linuxprogram' data is split with tr, and then grep is looking if my program result  matches linux program result. Remember, pipe ('|') sends the result of a program to the next as its input.

Line 6-9: the if that checks if there is a similarity. if -z $similarity is true, it means that the similarity is empty, so my program's output is different than the linux one's
Line 10: outputs the result of both programs, just to be sure that the script does the right thing

I could add a couple lines of code and have a summarise of my results: i.e. In the end it would tell me program errors: 3, or no error found. 
That would need a variable initialised to zero, and every time it finds an error it will be increased.
And the script should be like that:
for ((i=1;i<=10;i++)); do
myprogram=`echo "$data" | java myJavaProgram`
linuxprogram=`echo "$data" | name-of-linux-program`
similarity=`echo "$linuxprogram" | tr [:space:] \n | grep "$myprogram"`
if [ -z "$similarity" ]; then
 echo "Program is wrong"
 let errors+=1
else echo "Program is right"
echo $myprogram   $linuxprogram
if [ $errors -eq 0 ]; then
 echo "No errors found"
 echo "Found $errors errors"

1 comment:

  1. What You Need to Know About Casino Game Night - JTM Hub
    Whether you want a chance to play the most exciting online slot 이천 출장마사지 game night 진주 출장마사지 or a 강원도 출장마사지 fun opportunity to 부산광역 출장마사지 win a lot, we have the allure of 포천 출장안마 having fun