Archive for the 'Scripting' Category

Perl: Format date strings

Wednesday, December 7th, 2011

The other day I had a requirement to fill in a date value while creating an entity in ClearQuest. The field that was required was a date and was formatted as “mm/dd/yyyy hh:MM::SS AM|PM”. I used the localtime function to return the date and used sprintf to format the date to the proper output.
The reason I used sprintf was that for hours, months and days less 10 the output was 1 digit instead of two and the input was not allowed. The %02d states that the digits used will be a minumum of 2 in this case.

Here is the sample program I wrote to test the expected output:

 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;

# formatted just by concatenating output
my $datestring = $mon . "/" . $mday . "/" . $year . "  " . $hour . ":" . $min.
":" . $sec  ;

# formatted using sprintf to ensure proper digit counts are output
my $datestring2 = sprintf("%02d/%02d/%04d %d:%02d:%02d %s",$mon,$mday,1900 +$yea
r,$hour,$min,$sec, $hour >= 12? "PM" : "AM");

 print $datestring;
  print $datestring2;

Tags: ,

DOS: Check for empty environment variable

Wednesday, November 16th, 2011

I was writing a batch file the other day and needed to change the argumets to a command based on if the user had specified his password on the command line.

SET PASS=somepassword
: or
:SET PASS=
if x%PASS%= X (
  SET PASSARG=--password somepassword
) else
(
  SET PASSARG=
)

commandtorun %PASSARG%

Now if the user comments out his password, the script will make the PASSARG environment variable be empty or if they set a password the passarg variable will be filled in.

Tags: ,

Java: Command line argument processing

Sunday, November 6th, 2011

In each language that I have learned I have found it useful to know how to process command line arguments and here is a simple framework that I use for Java. I usually wrap the java command line in a bat or shell file to make it easier to run so that the user does not have to worry about classpath or path issues.

The following is the basic code to handle the command line arguments.

class CmdArgs
{

  public static void main(String[] args)
  {
      int argsize = args.length;
      String curarg;

      if (argsize > 0 )
      {
       
      }
      
      for(int i = 0; i < argsize; i++)
      {
          if (args[i].equals("--host"))
          {
             i++;
             host = args[i];
           }
          else if (args[i].equals("--debug"))
          {
               debug = 1;
          }
          else
          {
              System.out.println("Unknown option: " + args[i]);
              System.exit(1);
          }
       }

   }
}

The bat file for Dos/Windows might look like this:

@echo off

java -cp c:javabin;somejar.jar  com.etechtips.CmdArgs %*

The shell based file might look like:

java -cp /home/ecdown/javabin:somejar.jar com.etechtips.CmdArgs $*

Tags: ,

Linux:Script to use find to search contents of files

Thursday, November 3rd, 2011

I use find and grep quite a bit. I found that I was regularly searching for strings in file and was typing commands like:

find . -exec grep "somestring" {} ; --print

This find command will search from the current directory and for each file will run grep on it and if it finds a matching string, it will print the string and then the filename. So I took this and moved it into a script.

#!/bin/sh

if [ $# -lt 1 ]; then
  echo 1>&2 Usage: $0 ""
  exit 127
fi
icase=
search=

while [ $# -ge 1 ]; do
  case $1 in
     -i) icase=$1;;
      *) search=$1 ;;
  esac
  shift
done

find . -type f -print0 | xargs -0 grep $icase $search

This allows me to run a command such as

ffind -i Super

This command will search all file names starting from the current directory and recurse directories and the “-i” will tell it to ignore case.

Tags:

Perl: Open and read file line by line

Wednesday, September 28th, 2011

I regularly have to process Comma Seperated Value or CSV files, usually for user lists. So the following perl code is a framework for processing a file line by line.

#!/usr/bin/perl
use strict;
my $line;

open(FILEIN, "filename.txt");

while(<FILEIN>)
{
  chomp();
  $line = $_;
  # Process $line for data
  # Example seperating line by commas: 
  my ($username,$firstname,$lastname,$email) = split(/,/,$line);
}

close(FILEIN);

Tags: ,

Perl: Hashes

Wednesday, September 7th, 2011

Here are some basics about Perl hashes that can be helpful to be used when an you need to associate a label with some value such as for names and telephone numbers or account numbers with amounts owed.

Create a hash variable.

my %hashvar;

Create a hash reference

my $refhash = {};

Add a value to a hash variable.

$hashvar{'somekey'} = 'someval';

Add a value to a hash reference.

$hashref->{'somekey'} = 'someotherval';

Access a value in a hash.

print $hashvar{'somekey'};

Access a value in a hash reference.

print $hashref->{'somekey}';

Access all Keys in a hash variable.
foreach my $k (keys(%hashvar))
{
print $hashvar{$k};
}

Access all Keys in a hash reference.

foreach my $k (keys(%$hashref))
{
  print $k . "n";
}

Access all Keys and values in a hash.

while (my ($key,$value) = each %hashvar)
{
  print "Key: " . $key . " has value:" . $value . "n";
}

Access all Keys and values in a hash.

while (my ($key,$value) = each %$refhash)
{
  print "Key: " . $key . " has value:" . $value . "n";
}

Assigning multiple values to a hash

%hasvar = (
  'test2' => 'myval',
  'test3' => 'myval3'
);

Note: This will clear the existing hash.

Delete a value from a hash

delete $hashvar{'key1'};

Delete a value from a hash reference.

delete $hashref->{'key1'};

Tags: , , ,

Perl: Arrays

Saturday, September 3rd, 2011

I use perl arrays in some of my programs, and just started to look at the regular things that I do with these arrays and thought it would be useful to keep a list of frequently used items.

Create a variable specified as an array.

my @vararray

Add an item to the beginning of an array

unshift (@vararray, "File2");

Add an item to the end of an array

push (@vararray, "File1");

Remove an item from the beginning of an array

my $var = shift(@array);

Remove an item from the end of an array

pop(@array)

Clear an array

@vararray = ();

Address an item in an array

print $vararray[0] ; 

Get the size of an array

my $arraysize = @array;

Remove an item based on the index

delete @vararray[1];

Iterate over an array

foreach my $var (@vararay)
{
  print "$var n";
}
 
# or 
my $size = @vararray;
my $i;
for ($i = 0 ; $i < @size; $i++)
{
  print "$vararray[$i]n";
}

Tags: , ,

Linux System utilization tools

Friday, September 2nd, 2011

I was asked about system utilization on Linux systems. I found thesystatset of tools which consist of: sar, sadf, mpstat, iostat, nfsiostat, cifsiostat, pidstat and sa tools.

I will talk about the iostat tool in this article and follow up about some of the other tools.

On Ubuntu or debian systems you can install this toolset with:

sudo apt-get install sysstat

Then you will have access to the iostat command. The command output from executing with no options is below:

# iostat
Linux 2.6.32-33-generic (sumo)  09/01/2011      _i686_  (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.11    0.00    0.47    0.01    0.00   99.41

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               0.54         1.85         8.91    1611385    7740024

This displays the avg-cpu utilization and a device usage list with basic utilization.
Some options you can use are

Option Explanation
-c Display CPU utilization only
-d Display Device utilization only
-k Display information in kilobytes
-m Display information in megabytes
-t Display time for each report
-x Display extended statistics

One more way to use the iostat tool is to specify an interval and count to the command

iostat -d -x 10 5

This will display the output 5 more times with 10 seconds between each run.

Tags: , ,

How to exit python script

Tuesday, August 23rd, 2011

When I was trying to terminate a python script after a failure, I tried to find the nicest way to exit. The sys.exit() command provided by importing the sys library was what I found.

The function can take an optional numeric argument, usually in the range of 0-127, and with no argument it returns 0. The return code is passed back to the Operating System.

import sys
sys.exit(1)

When exit returns a non-zero value, it is considered an error.

Tags:

Bash: test if file exists

Friday, August 19th, 2011

When you need to do some processing in bash and display if a file exists, the following is a simple script that takes the filename in the $testfile variable and reports if it exists or not.

if [ -f $testfile ]
then
  echo $testfile exists!
else
  echo $testfile does not exist
fi