> > I am new to shell scripting, 
> > I would like to create a script in bash shell.
> > that would do the following
> > 
> > 1) find out the latest file ( text file)in a directory
> > ( there are bunch of files in a dir)

This will work...
    cd {directory}
    ls -t | head -1
or
    ls -t $HOME | head -1

Using ksh (on Unix), this next line works to only accept files
    x=`ls -t | head -1`; if [[ -f $x ]]; then echo $x ;fi
except that I could not get this to work with bash as
    x=`ls -t | head -1`; if [ -f $x ]; then echo $x ;fi
since the '-f $x' seems to fail the test. Anyone know why ?

> > 2)open that file (text file) and parse the
> > values seperated by comma and store them in variables.

Perl script follows at bottom to do what you want.
It assumes you have a file (say xxx), with format like

1,2,3,4,5
abc,def,ghi,jkl,mno
joe,smith,[EMAIL PROTECTED],555-1212,Canada

The script will accept a directory name for parameter

Thanks... Dan.


#!/usr/bin/perl
#
# Dan Woods
# 4Loops Internet Services
# http://www.4loops.com
# August 3, 2000 - GPL license
#

use strict;

my $p1 = $ARGV[0];
my $dir = defined $p1 ? $p1 : $ENV{HOME};
my $x = `/bin/ls -t $dir | /usr/bin/head -1`;
chop $x;
# print "x, $x\n";

exit if (! (defined $x && -T $x));      # do nothing in script if not text file

open F, "<$x" or die "Unable to open file $x, $?";

### You can use an array, or individual variables.
# my @items;
my ($fname, $lname, $email, $phone, $origin, $junk);

while (<F>)
{
  chop;
  # for each line in file
  # @items = split /,/;
  ($fname, $lname, $email, $phone, $origin, $junk) = split /,/;

  ### do whatever you want here

  print "$fname : $lname : $email : $phone : $origin \n";
  # print "$items[0] : $items[1] : $items[2] : $items[3] : $items[4] \n";
}

close F or die "Unable to open file $x, $?";


Reply via email to