Need a help in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a help in shell scripting
# 1  
Old 04-29-2009
Need a help in shell scripting

Hi folks,

My code goes here:


Code:
1.  #!/bin/ksh

2.  export LIBPATH=/ediprod6.1/gentran/lib:/usr/local/lib

3.  cd /ediprod6.1/gentran/comm_EUROCSV/
4.  infiles=`ls *csv 2>/dev/null`    ## List all input files
5.  echo $infiles
6.  if test -n $infiles 2>/dev/null
7.  then
8.    ls *csv|while read i        ## Read each input files
9.    do
10.       dttm=`date +%y%m%d%H%M%S`
11.       var1=`head -1 $i | cut -d"," -f2`
12.       var2=`head -1 $i | cut -d"," -f3`
13.       var=$var2"_"$var1
14.       echo $var
15.       ./new.$var.sh
16.       sleep 5000
17.   done
18. else
19.    echo "NoGAMDE1_KRW71 files today" |mail -s "GAMDE1_KRW71 FILES ${dttm}" xyz@abc.com
20. fi


In line 4,I have list all the csv files in a working directory.
In line 11,12,13 , I will read the 2nd and 3rd column data and store in varibale called "var" and execute the script.

Note:each csv file may contain different data in 2nd and 3rd column.depending upon the data,it will execute corresponding script.
All the files and corresponding scripts are in the same directory

I want to insert a delay where it should complete the script execution for the first csv filesand read the next csv file data and execute the second corresponding script.


Can anyone help me out where to insert a delay command and how much.the csv files size keeps on changing.sometimes it may contains less data and sometimes more data.

Last edited by Franklin52; 04-29-2009 at 06:44 AM.. Reason: adding code tags
# 2  
Old 04-29-2009
I think it will be best if you could check the return code of the script and if successful, then proceed with the rest of the file processing else warn and exit.

or in a better way, implement a function and pass the csv file name as a parameter and have the function return a code upon successful execution.

cheers,
Devaraj Takhellambam
# 3  
Old 04-29-2009
Can you help out by written code for 2nd solution
# 4  
Old 04-29-2009
Quote:
Originally Posted by chandrakala
Can you help out by written code for 2nd solution
You will need to explain what does the script does and see if that can be implemented in a function instead.


cheers,
Devaraj Takhellambam
# 5  
Old 04-29-2009
are u talking about this script(./new.$var.sh) which acts on csv file.
# 6  
Old 04-29-2009
Quote:
Originally Posted by chandrakala
are u talking about this script(./new.$var.sh) which acts on csv file.
Yes. You will need to explain what does that script does on the specified csv file.
# 7  
Old 04-30-2009
Hi,
Basically I work on EDI file.
The script will convert each record(row) in the CSV file to sterling format.
The number of records keeps varying.

The scripts goes here:


#!/bin/ksh

export LIBPATH=/ediprod6.1/gentran/lib:/usr/local/lib

cd /ediprod6.1/gentran/comm_EUROCSV/

infiles=`ls *csv 2>/dev/null` ## List all input files
if test -n $infiles 2>/dev/null
then
ls *csv|while read i ## Read each input files
do

echo "FILE NAME:" >notify.$i.mail
echo "$i" >>notify.$i.mail ## ls in.$i >>notify.$i.mail
###echo "FILE DATA:" >>notify.$i.mail
###cat in.$i >>notify.$i.mail

########### FILE EDITING PROCESS ######################################


sed s/
//g <$i >$i.tmp
mv $i.tmp $i
## Declare unique variable as DateTime(dttm)
dttm=`date +%y%m%d%H%M%S`


########### END OF FILE EDITING PROCESS ###############################

############## FILE TRANSLATION PROCESS ###############################
ERROR=0
rm /ediprod6.1/gentran/comm_EUROCSV/temp/dtlLog.err
rm /ediprod6.1/gentran/comm_EUROCSV/temp/xlcntl.err
touch /ediprod6.1/gentran/comm_EUROCSV/temp/dtlLog.err
touch /ediprod6.1/gentran/comm_EUROCSV/temp/xlcntl.err

## Translate file
lftran $i "-ofT[TMN91_SPICER]" transport.$i.out.$dttm -cp "/ediprod6.1/gentran"

res=$?
if [ $res -ne 0 ] ## Check if there is ERROR
then
##### Generate ERROR DETAILS USING xlcntl.err and dtlLog.err ####
C=0
File_xln=/ediprod6.1/gentran/comm_EUROCSV/temp/xlcntl.err
File_dtl=/ediprod6.1/gentran/comm_EUROCSV/temp/dtlLog.err

echo "########## ERROR DETAILS FOR $i ##########" >ErrorDetail_of_$i.txt ## Generate Error details txt File

grep "in.*.app" $File_xln >app_lin.tmp
awk 'BEGIN {FS = "/"} {print $NF} ' app_lin.tmp >app_name.tmp

grep "Record count" $File_xln >rec_cnt.tmp
awk 'BEGIN {FS = ":"} {print $NF } ' rec_cnt.tmp >rec_name.tmp

grep "Translation error count" $File_xln >err_cnt.tmp
awk 'BEGIN {FS = ":"} {print $NF} ' err_cnt.tmp >err_name.tmp

C=`cat app_name.tmp|wc -l`
###C=`expr $C + 1`

Line=1
while [ $Line -le $C ];
do
err_num=0
app_name=`tail +$Line app_name.tmp|head -1`
rec_name=`tail +$Line rec_name.tmp|head -1`
err_num=`tail +$Line err_name.tmp|head -1`

echo "Record Number :$rec_name" >>ErrorDetail_of_$i.txt
grep -n $app_name $File_dtl >file_name.tmp
line_number=0
line_number=`awk 'BEGIN {FS = ":"} {print $1 } ' file_name.tmp`
err_num=`expr $err_num + 1`

L=1
while [ $L -lt $err_num ];
do

####Get Decription
echo "Error Number : $L" >>ErrorDetail_of_$i.txt

if [ $L -eq 1 ]; then
line_number=`expr $line_number + 23`
fi

if [ $L -gt 1 ]; then
line_number=`expr $line_number + 5`
fi

tail +$line_number $File_dtl|head -1 > err_desc.tmp
awk 'BEGIN {FS = ">"} {print $2 } ' err_desc.tmp >err_desc1.tmp
err_desc=`awk 'BEGIN {FS = "<"} {print $1 } ' err_desc1.tmp`
echo "Error Description : $err_desc" >>ErrorDetail_of_$i.txt

####Get Element Name
line_number=`expr $line_number + 19`
tail +$line_number $File_dtl|head -1 > elm_name.tmp
awk 'BEGIN {FS = ">"} {print $2 } ' elm_name.tmp >elm_name1.tmp
elm_name=`awk 'BEGIN {FS = "<"} {print $1 } ' elm_name1.tmp`
echo "Element Name : $elm_name" >>ErrorDetail_of_$i.txt

####Get Value
line_number=`expr $line_number + 4`
tail +$line_number $File_dtl|head -1 > elm_value.tmp
awk 'BEGIN {FS = ">"} {print $2 } ' elm_value.tmp >elm_value1.tmp
elm_value=`awk 'BEGIN {FS = "<"} {print $1 } ' elm_value1.tmp`
echo "Element Value : $elm_value" >>ErrorDetail_of_$i.txt

L=`expr $L + 1`
done

echo "---" >>ErrorDetail_of_$i.txt
Line=`expr $Line + 1`
done

rm *.tmp 2>/dev/null
########## END of Generate ERROR#########

## Send ERROR notification via email
uuencode ErrorDetail_of_$i.txt ErrorDetail_of_$i.txt |mailx -s "PROBLEM TRANSLATING TMN91 SPICER FILE named $i. FILE MOVED TO ERROR" xyz@abc.com
## mv in.$i.$dttm /ediprod6.1/gentran/error
ERROR=1

rm $i 2>/dev/null
rm transport.$i.out.$dttm 2>/dev/null
rm notify.$i.mail 2>/dev/null
fi

if [ $ERROR -ne 1 ] ## If there is no ERROR
then
echo " " >>notify.$i.mail
echo "TRL COUNT AFTER TRANSLATION PROCESS:" >>notify.$i.mail
grep ^TRL transport.$i.out.$dttm | wc -l >>notify.$i.mail

## Send Notification email
cat notify.$i.mail |mail -s "TMN91 Spicer FILES ${dttm}" xyz@abc.com

## Script to remove multiple HDR TRL (HT)

## trl=0
## inFile=transport.$i.out.$dttm
## resultFile=resultFile.tmp
## head -1 $inFile >>$resultFile
## trl=`grep ^TRL $inFile |wc -l`
## sed -e /^HDR/d $inFile >> tempFile1
## sed -e /^TRL/d tempFile1 >> $resultFile
## rm tempFile1
## tail=$trl
## echo "TRL|"`expr $trl`>>$resultFile

## Script Separate
FILE=transport.$i.out.$dttm
C=`cat $FILE|wc -l`
C=`expr $C + 1`
Line=1
F_Count=1
while [ $Line -lt $C ];
do
touch $F_Count.tmp
temp_line=`tail +$Line $FILE|head -1`
start=`echo $temp_line|cut -c 1-3`
if [ $start != "TRL" ]; then
echo $temp_line>>$F_Count.tmp
else
echo $temp_line>>$F_Count.tmp
F_Count=`expr $F_Count + 1`
fi
Line=`expr $Line + 1`
done

## INV Manipulator
File_C=1
while [ $File_C -lt $F_Count ];
do
INV=`grep ^INV $File_C.tmp` # INV = INV line of current file
if [ "$INV" != "" ]; then
INV1=`cat $File_C.tmp|grep ^INV|cut -d "|" -f2`
INV3=`cat $File_C.tmp|grep ^INV|cut -d "|" -f4`
INV_O=`echo $INV3` ## INV3 original
cat $File_C.tmp|grep ^SHP|cut -d "|" -f16 > SHP_F15.tmp
F=`expr $File_C + 1`
while [ $F -lt $F_Count ] ;
do
INV_F1=`cat $F.tmp|grep ^INV|cut -d "|" -f2`
if [ "$INV_F1" = "$INV1" ];then
SHP_F15=`cat $F.tmp|grep ^SHP|cut -d "|" -f16`
echo $SHP_F15>>SHP_F15.tmp
## Delete INV line from $F.tmp ##
touch temp.tmp
grep -v "^INV" $F.tmp > temp.tmp
cat temp.tmp>$F.tmp
rm temp.tmp
fi
F=`expr $F + 1`
done
## Replace INV3 ##
awk 'BEGIN{AMT=0}{
AT=$1
AMT=AMT + AT;}
END{printf("%.2f",AMT)}' SHP_F15.tmp> INV3.tmp
INV3=`cat INV3.tmp`
echo $INV>INV.tmp
sed s/"|$INV_O|"/"|$INV3|"/ INV.tmp>temp.tmp
cat temp.tmp>INV.tmp
rm temp.tmp
touch temp.tmp
INV_New=`cat INV.tmp`
sed s/"$INV"/"$INV_New"/ $File_C.tmp > temp.tmp
cat temp.tmp>$File_C.tmp
rm temp.tmp
touch temp.tmp
fi
File_C=`expr $File_C + 1`
done

## Script Merge
File_C=1
rm temp.tmp 2>/dev/null
touch temp.tmp
while [ $File_C -lt $F_Count ];
do
cat $File_C.tmp>>temp.tmp 2>/dev/null
File_C=`expr $File_C + 1`
done
cat temp.tmp>FILE_merge.tmp
rm temp.tmp 2>/dev/null

### removing Multiple HRD TRL
rm resultFile.tmp tempFile.tmp 2>/dev/null
touch resultFile.tmp tempFile.tmp

head -1 FILE_merge.tmp >>resultFile.tmp
tail=`grep ^HDR FILE_merge.tmp |wc -l`
sed -e /^HDR/d FILE_merge.tmp >> tempFile.tmp
sed -e /^TRL/d tempFile.tmp >> resultFile.tmp
echo "TRL|"`expr $tail`>>resultFile.tmp
#//


cat resultFile.tmp>transport_transport_110.SPICER.$dttm ## Create Output file
rm $resultFile
rm *.tmp 2>/dev/null
########## END OF TRANSLATION PROCESS ##################

## Move Translated output file to FTP
###mv transport_transport_110.100998.* /ediprod6.1/gentran/ftp_trans_ran 2>/dev/null
mv transport_transport_110.SPICER.* /ediprod6.1/gentran/comm_EUROCSV/test 2>/dev/null

## Update in Legal( infile and notification ) directory
cat $i >/ediprod6.1/gentran/legal/FEDXSMART/TMN91.in.$dttm 2>/dev/null
cat notify.$i.mail >/ediprod6.1/gentran/legal/FEDXSMART/TMN91.email.$dttm 2>/dev/null

## move infile in "Legal/CLIENT" directory
mv $i /ediprod6.1/gentran/legal/EURO/TMN91/in.dane1.$dttm 2>/dev/null

fi
done

## Remove all temp files
rm notify* 2>/dev/null
rm in* 2>/dev/null
rm *out 2>/dev/null
rm out* 2>/dev/null
rm transport* 2>/dev/null
rm *.csv 2>/dev/null

else
echo "No TMN91 Spicer files today" |mail -s "TMN91 Spicer ${dttm}" xyz@abc.com

fi


Thanks for your time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Shell Programming and Scripting

help me in Shell Scripting

Hi there please have a look at the code..i want to create Using a named pipe. Run a find in the background starting in the working directory While this is happening wait for input from the user to ask him which file to find. If the user does not enter any data in 10 seconds ask the user again.... (1 Reply)
Discussion started by: kattak1511
1 Replies

3. Shell Programming and Scripting

Shell scripting

Hi, if in a network there are lots of PCs connected with either windows or linux as operating system.Then what will be the shell script for the same and also if the PC has linux in it then we have to find if it is occupied or unoccupied. If the PC has windows in it then we have to find if it is... (6 Replies)
Discussion started by: akansha singh
6 Replies

4. UNIX for Dummies Questions & Answers

Shell Scripting

Hey I have a data in the file named as outputFile.txt. The data is in the format 123456,12345678912345,400,09/09/09,INACTIVE. I want this output without commas ie 12345612345678912345400090909INACTIVE. Please tell me what to do and clear explain all the terms, as I am new to it. (6 Replies)
Discussion started by: sampandey31
6 Replies

5. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

6. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

7. Android

Android Scripting Environment: Shell Scripting and Android

I just upgraded to Android 2.2 from 2.1. The GPS issue that was troublesome in 2.1 seems to have been fixed. Some of web browsing seems faster, but it could just be my connection is better today ;) Flash works in some browsers but not very good and it is too slow for Flash apps designed for... (0 Replies)
Discussion started by: Neo
0 Replies

8. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

9. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question