Friday 27 February 2015

Sample SHELL program to illustrate file read and file write operations

if [ $# -lt 2 ]
then
  echo "Please provide 2 file names";
  exit
fi

while read LINE
do
  echo $LINE >> $2
done < $1

Sample PERL program to illustrate file read and file write operations

#!/usr/bin/perl

use strict;
use warnings;

if(@ARGV < 2)
{
  print "Please provide 2 file names\n";
  exit;
}

open my $IFILE, "<", $ARGV[0];
open my $OFILE, ">", $ARGV[1];

while(<$IFILE>)
{
  print $OFILE $_;
}

Sample C++ program to illustrate file read and file write operations

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char * argv[])
{
  if(argc < 3)
  {
    cout<< "Please pass 2 file names"<<endl;
    return 0;
  }
  else
  {
    string line;

    fstream ifile, ofile;
    ifile.open(argv[1], ios::in);
    ofile.open(argv[2], ios::out);

    while(getline(ifile, line))
    {
      ofile << line << endl ;
    }

    return 1;
  }
}

Wednesday 25 February 2015

How to customize the date to get required format in shell programming.

Get name of the day:
date +"%A"

Get only day without month and year:
date +"%d"

Get hour of the day (00..23):
date +"%H"

Get hour of the day (01..12):
date +"%I"

Get day of the year:
date +"%j"

Get name of the month:
date +"%B"

Get only month:
date +"%m"

Get current century:
date +"%C"

Get only year:
date +"%Y"

Get full date in yyyy-mm-dd format:
date +"%F" or date +"%Y-%m-%d"

Get minute of the hour:
date +"%M"

Get second of the minute:
date +"%S"

Get time in hh:mm:ss
date +"%T" or date +"%H:%M:%S"

Get the timestamp:
date +"%s"

Friday 20 February 2015

Sample SHELL program to illustrate - 1) Declaring variables 2) Writing to STDOUT, 3) reading user input from STDIN, 4) Using IF statement 5) Using FOR and WHILE loops

echo -n "What is your name ? "
read name
echo "Hi $name"

echo -n "Enter a number: "
read num

echo -n "Even numbers below $num: "
for((i = 1; i <= $num; i++))
do
  if [ `expr $i % 2` == 0 ]
  then
    echo -n "$i ";
  fi
done
echo -e ""

echo -n "Odd numbers below $num: "
j=1
while [ $j -le $num ]
do
  if [ `expr $j % 2` != 0 ]
  then
    echo -n "$j "
  fi
  j=`expr $j + 1`
done
echo -e ""

Sample PERL program to illustrate - 1) Declaring variables 2) Writing to STDOUT, 3) reading user input from STDIN, 4) Using IF statement 5) Using FOR and WHILE loops

#!/usr/local/bin/perl

use strict;
use warnings;

print "What is your name ? ";
my $name = <STDIN>;
print "Hi $name";

print "Enter a number: ";
my $num = <STDIN>;
chomp($num);

print "Even numbers below $num: ";
for(my $i = 1; $i < $num; $i++)
{
  if($i%2 == 0)
  {
    print "$i ";
  }
}
print "\n";

print "Odd numbers below $num: ";
my $j = 1;
while($j < $num)
{
  if($j%2 != 0)
  {
    print "$j ";
  }
  $j++;
}
print "\n";

Sample C++ program to illustrate - 1) Declaring variables 2) Writing to STDOUT, 3) reading user input from STDIN, 4) Using IF statement 5) Using FOR and WHILE loops

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string name;
  cout << "What is your name ?";
  cin >> name;
  cout << "Hi " << name << "\n";

  int num;
  cout << "Enter a number: ";
  cin >> num;

  cout << "Even numbers below" << num << ": ";
  for(int i = 1; i < num; i++)
  {
    if(i%2 == 0)
    {
      cout << i << " ";
    }
  }
  cout << "\n";

  cout << "Odd numbers below" << num << ": ";
  int j = 1;
  while(j < num)
  {
    if(j%2 != 0)
    {
      cout << j << " ";
    }
    j++;
  }
  cout << "\n";

  return 1;
}

Monday 16 February 2015

warning: conflicting types for built-in function â__atomic_compare_exchangeâ static inline int __atomic_compare_exchange(

 Cause: 

 A built-in function in the gcc compiler seems conflicting with the Berkley db function because of the same function name.


 Explaination:

 Few __atomic routines are added in gcc-4.7.0 and above. So one of them is __atomic_compare_exchange and this function also exists in Berkley db.
 That's why it is conflicting.


 Fix:

 Modify the function "__atomic_compare_exchange" in dbinc/atomic.h file in Berkley db to "__atomic_compare_exchange_db"

Wednesday 11 February 2015

error: ‘f’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] note: ‘int f(int)’ declared here, later in the translation unit

Cause: 

This error is because of a bug fix in gcc 4.7 or later.



Explaination: 


Compilers earlier than gcc 4.7 accept a function usage in a template function even prior to its declaration. Actually it's a bug in these compilers. Now it got fixed in gcc 4.7.  


For example, code shown below


-------------------------------

template<typename T>
int t(T i)
{ return f(i); }

int f(int i)
{ return i; }

int main()
{
  return t(1);
}
------------------------------
compile with gcc < 4.7 but will result in the following error if gcc >= 4.7:

--------------

In instantiation of ‘int t(T) [with T = int]’ required from here
error: ‘f’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
note: ‘int f(int)’ declared here, later in the translation unit
--------------


Fix:

To fix, make sure the function f in the code above is declared before first use in function t. Like so:

--------------------

int f(int i)
{ return i; }

template<typename T>
int t(T i)
{ return f(i); }

int main()
{
  return t(1);
}
---------------------
Work around:
While compiling specify the flag -fpermissive.
Eg: gcc -fpermissive test.cpp