understanding a ksh script

January 7, 2010 at 10:27:14
Specs: HP Itanim ia64, 4GHz/16GB
Hi

I have a ksh script which is fetching data from an Infiormix db on a remote server using ARC files and creating CSV files for that data. I'm trying to understand how to specify the start and end dates manually in the script and then run it as I need to collect data for a certain period.

# Get the current time as the stop time.
#
stoptime=`date +"%Y-%m-%d %H:00"`
if test $? -ne 0
then
echo "Failed to get the date"
rm -f $1/.optpamo.pid
exit 4
fi

#
# Read the lasttime file to get the start time
#
if test -f $1/optlasttime
then
starttime=`cat $1/optlasttime`
# if the length of the chain is zero
# (lasttime is empty) It is updated properly
# and I wait for the following hour
if test -z "$starttime"
then
echo "Empty file lasttime"
echo $stoptime > $1/optlasttime
rm -f $1/.optpamo.pid
exit 5
fi
else
# If lasttime does not exist I create, it with the present date
# and I wait for the following hour
echo "File lasttime does not exist"
echo $stoptime > $1/optlasttime
rm -f $1/.optpamo.pid
exit 6
fi

Any help appreciated


See More: understanding a ksh script

Report •

#1
January 7, 2010 at 21:29:59
I'm not exactly certain what your problem is. The way the script is written you have to wait an hour to see a difference between the start and the stop time. Is that what you want?

BTW, your script works, but modern ksh scripts use [ and ] (or [[ and ]] for specific ksh) instead of test. See my addition for testing whether arg 1 was passed. Let me know if you have any other question:

#!/bin/ksh

if [ -z $1 ]
then
   echo "arg 1 undefined"
   exit 1
fi

# Get the current time as the stop time.
#
stoptime=`date +"%Y-%m-%d %H:00" 2> /dev/null`
if test $? -ne 0
then
   echo "Failed to get the date"
   rm -f $1/.optpamo.pid
   exit 4
fi

#
# Read the lasttime file to get the start time
#
if test -f $1/optlasttime
then
   echo "cat"
   starttime=`cat $1/optlasttime`
   # if the length of the chain is zero
   # (lasttime is empty) It is updated properly
   # and I wait for the following hour
   if test -z "$starttime"
   then
      echo "Empty file lasttime"
      echo $stoptime > $1/optlasttime
      rm -f $1/.optpamo.pid
      exit 5
   fi
else
   # If lasttime does not exist I create, it with the present date
   # and I wait for the following hour
   echo "File lasttime does not exist"
   echo $stoptime > $1/optlasttime
   rm -f $1/.optpamo.pid
   exit 6
fi

echo "starttime is: $starttime"
echo "stoptime is: $stoptime"


Report •

#2
January 18, 2010 at 05:09:09
Thanks for the reply. What I meant to say is:
That the starttime and stoptime in the script is variable. I want to specify them as specific datetimes. Can I write:

# Get the current time as the stop time.
#
stoptime=2010-01-03 15:00
#if test $? -ne 0
#then
#echo "Failed to get the date"
#rm -f $1/.optpamo.pid
#exit 4
#fi

#
# Read the lasttime file to get the start time
#
#if test -f $1/optlasttime
#then
starttime=2010-01-03 15:00
# if the length of the chain is zero
# (lasttime is empty) It is updated properly
# and I wait for the following hour
#if test -z "$starttime"
#then
#echo "Empty file lasttime"
#echo $stoptime > $1/optlasttime
#rm -f $1/.optpamo.pid
#exit 5
#fi
#else
# If lasttime does not exist I create, it with the present date
# and I wait for the following hour
#echo "File lasttime does not exist"
#echo $stoptime > $1/optlasttime
#rm -f $1/.optpamo.pid
#exit 6
#fi

The datetime format used is as it is found in the file 'optlasttime'.


Report •

#3
January 18, 2010 at 10:39:35
Any string containing white space - i.e. spaces - must be surrounded by quotes:

stoptime=2010-01-03 15:00

must be:

stoptime="2010-01-03 15:00"


Report •
Related Solutions


Ask Question