Re: Syntax Checker Script



On Tuesday 30 October 2007 01:56, nishant.lokre@xxxxxxxxx wrote:
Hello,

Hello,

I want a script which is going to check if the numbers specified in
the config file is the same for each of the types of lines in the
seeding files.

The script would read each line to check if the no of columns is
correct as proposed in a config file.

If it is not correct, out put an entire list of lines which were not
correct with line number in OUTPUT file and exit with an error.

If all the lines were correct, exit normally.

Config.ini
ROUTER,22
IF,19
QOSINTERFACE,18

Seed.csv file
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4
IF,BusPhonePrefix,BusAreaCode,BusPhoneLast4,BusAreaCode
IF,BusPhonePrefix,BusAreaCode,BusPhoneLast4
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4
ROUTER,BusPhonePrefix,BusAreaCode,BusPhoneLast4,BusAreaCode,
QOSINTERFACE,BusPhonePrefix,BusAreaCode,BusPhoneLast4,BusAreaCode
ROUTER,BusAreaCode,BusPhonePrefix,BusPhoneLast4,BusPhonePrefix,

Below is the code for this particular requirement but right now
inputs are hard coded
I want that this script read from Config.ini file.
Can anybody help me out in this?

Yes, you should probably store the data from the file in a hash:

my $config_file = 'Config.ini';
open my $ch, '<', $config_file or die "Cannot open '$config_file' $!";
my %config;
while ( <$ch> ) {
chomp;
my ( $key, $value ) = split /,/;
$config{ $key } = $value;
}
close $ch;



[ Code reformatted for legibility. ]

#!/usr/bin/perl

The next two lines should be:

use warnings;
use strict;

so that perl will help you find mistakes in your code.


$outcome = validateFile();
if($outcome == 1) {print("file is not proper, Please check the
file\n");}
else {print("Everything is fine in a file\n");}


sub validateFile
{
my $state=0;
$ARGV[0]=ROUTER;
$ARGV[1]=5;
my $first=$ARGV[0];
my $second=$ARGV[1];
my $line_num = '';
my $fname="seed.csv";


open(OF, $fname) or die "Can't open user database file!\n";

You should include the $! variable and probably $fname in the error
message so you know *why* it failed.


$. = 0;

Why are you doing this? Hint: it has *no* effect on the value of $.
inside the loop.


LINE: while ($temp_line = <OF>) {

You only have one loop so you don't really need the label.


chomp $temp_line;

@array = split(/,/,$temp_line,-1);

#print "CURRENT LINE $temp_line\n";

if ($first eq $array[0])
{
#print("$first is found\n");
$arraylength=@array;
# check particular line
if($second ne $arraylength)

You are comparing two numbers so you should use a numerical compare.
Also you could use the array directly:

if ( $second != @array )


Another thing you could do is instead of chomping and splitting up the
line is to just count the number of commas plus one:

if ( $second != 1 + $temp_line =~ tr/,// )


{
print "CURRENT LINE $temp_line\n";

$line_num = $.;
print "LINE NUMBER $line_num\n";

Why not just:

print "LINE NUMBER $.\n";


print("Lenght of Current Line is $arraylength which

'Lenght' is usually spelt 'Length'.


is not matching with $second\n");
$state=1;
#last;
}

}
next LINE;

}
close($fname);
return ($state);
}




John
--
use Perl;
program
fulfillment

.



Relevant Pages

  • Re: Analysis Services Command line
    ... You will see that there is a switch to supply a config file. ... variable and then have the DDL task take the script from the variable. ... I am learning to use the DTEXEC. ... >> Within an IS package there is a new task called Execute Analysis Services ...
    (microsoft.public.sqlserver.olap)
  • Re: Why not XML based configurationfiles?
    ... >>I still have yet to see your working shell script that can parse XML ... Sure one might need to parse a config file in a shell script "for ... the XML file directly or make heavy use of cfg-tool. ...
    (comp.os.linux.misc)
  • Re: Whereis Kernel? & Why Debian "KDE" freezing?
    ... > There is no config file in. ... Thats not a Script, thats the kernel. ... your KDE is unstable. ...
    (alt.linux)
  • Re: Bash/Shell scripting practices/conventions
    ... I just finished my first bigger bash script and I want to upload it to ... To check whether a command (here 'fold') is executable, ... If a config file exists at the default position, ...
    (comp.unix.shell)
  • loadtimes script
    ... Here is the script and a sample config file. ... # parts of the process the transition was between. ... TransitionFromProcessRef (VARCHAR) "%s" ...
    (comp.programming)