Operating System - HP-UX
1833871 Members
1656 Online
110063 Solutions
New Discussion

Re: Converting Kshell to C language

 
Henry Chua
Super Advisor

Converting Kshell to C language

I have gotten this written in korn shell, can anyone give me some guides onto how to covert this to C language. I am particularly interested in how to move files efficiently using C under shell environment. :

#!/bin/ksh

# -- Date initialisation --
YY=`date +%Y`
MM=`date +%m`
DD=`date +%d`
HH=`date +%H`
MI=`date +%M`
SS=`date +%S`

# -- Files & Directories Initialisation --
DIR=/users/tester/temp
SCP=$DIR/T1
OCP=$DIR/Temp
FIL=$DIR/file.txt
OFL=$DIR/file_old.txt
NFL=$DIR/file_new.txt
LOG=$DIR/events.txt

TM1=$DIR/tmp1.txt
TM2=$DIR/tmp2.txt

#Check file modification status
ls -lt $FIL > $NFL
diff $NFL $OFL > $TM1
wc -l $TM1 > $TM2
No=`awk '{ print $1 }' $TM2`
cp $NFL $OFL
#NOW=`date`
if [ $No -gt 0 ]
then
#-- Log event --
echo "Change in Tester Deskew file detected, program ran on $YY$MM$DD at $HH$MI$SS""hrs" >> $LOG
echo "file modified on $NOW"
cp $SCP/* $OCP
rm -f $SCP/*

# -- Housekeeping --

if [ -f $TM1 ]
then
rm $TM1
fi

if [ -f $TM2 ]
then
rm $TM2
fi

if [ -f $NFL ]
then
rm $NFL
fi
1 REPLY 1
Biswajit Tripathy
Honored Contributor

Re: Converting Kshell to C language

To me, it appears that the job you want to do is
best handled by using shell script. But if you must
write a C program for this, here are the library
routines you should be looking at:

Date initialization: see library routines
gettimeofday(2), time(2), ctime(3C) and related
functions (all listed in the manpage of ctime(3C)).

File modification status: Should use fstat(2) call. See
the manpage for more details. Structure stat
(defined in sys/stat.h) stores the access details of
the files. Use difftime(3C) routine for time
difference.

Log event: If you want to log to /var/adm/syslog,
then use syslog(3C).

Housekeeping: Use link(2), unlink(2) to copy and
remove files.

Does that answer what you are looking for?

- Biswajit
:-)