0 pts.
 Unix shell script help ?
I wrote a shell script and defined the function in the begining, but calling function somewhere in the middle of the code. The problem is function is getting executed before the function call, immmediatly when i execute the script from the command. Please help any body.....
here is my code

function create_dat_file
{
filename="$fname"
# FUNC-STEP1
mv $filename $filename.formatted
newfile="$filename".formatted
#echo "here newfile: $newfile"
# FUNC-STEP2
filename=`echo $line | nawk '{ print substr($7,1,index($7,".")-1)}'`
#echo $filename
# SUB-STEP3
if [ -s "$newfile" ]; then
tr -d '015'<$newfile >"$newfile"_1
else echo "File not found for the TR command"
fi
# FUNC-STEP4
if [ -s "$newfile"_1 ]; then
sed 's/ $//' "$newfile"_1 > "$newfile"_2;
else echo "File not found for the SED command"
fi
# FUNC-STEP5
if [ -s "$newfile"_2 ]; then
grep -v "^$" "$newfile"_2 > "$newfile"_3;
else "file not found for the GREP command"
fi
# FUNC-STEP6

if [ -s "$newfile"_3 ]; then
nawk -v dt="$filedate" -v fnm="$fname" -v dnm="$current_dir" '
BEGIN { fil = FILENAME }
{ if( $4 ~ /NYSID:/ ) { idtyp= 1; personid= substr($5,1,length($5) -1) }
if( $4 ~ /WPR:/ ) { idtyp= 2; personid= substr($5,1,length($5) -1) }
if( $1 ~ /insert/ )
{ stmnt = getSQLStatement()
print dnm"|"fnm"|"dt"|"idtyp"|"personid"|"getTableName(stmnt)"|"stmnt
}
if( $1 ~ /delete/ )
{ stmnt = getSQLStatement()
print dnm"|"fnm"|"dt"|"idtyp"|"personid"|"getTableName(stmnt)"|"stmnt
}
}
function getSQLStatement()
{
sqlStmnt = $0;
if (!match($0, ";$"))
{
do
{
if (getline == 0) break;
sqlStmnt = sqlStmnt " " $0;

} while(!match($0, ";$"));
}
return sqlStmnt;
}

function getTableName(stmnt)
{
split(stmnt, stWords, " ");
tableName = stWords[3];
if (index(tableName, "(") > 0)
{
tableName = substr(tableName, 1, index(tableName,"(")-1);
}
return tableName;
}
' "$newfile"_3 > "$filename".dat
else "file not found for nawk "
fi

# FUNC-STEP7

if [ -s "$filename".dat ]; then
#echo " file found"

batch="BATCH4"
#echo "here1"
`sqlplus -silent rkumar/rkumar@ds2tcch << END >/home/rkumar/"$filename".log
set pagesize 0 verify off heading off echo off
define filename=${filename}.dat
define fdate=${filedate}
define fbatch=${batch}

insert into conv_batchfile_metadata
values ((select decode(max(file_number),null,0,max(file_number)) +1 from conv_batchfile_metadata),
'&filename',
to_date('&fdate','mon-dd-yy-hh24:mi'),
'NOT',
'&fbatch');

commit;
exit;
END`

# FUNC-SUB-STEP1

#echo "here 2"
if [ -s /home/rkumar/"$filename".log ]; then
#echo "file found"
chk=`grep "row" /home/rkumar/"$filename".log | awk '{print $1}'`
#echo "$chk"
if [ "$chk" = 0 ]; then
echo "here 3"
echo "could not insert file details into conv_batch_metadata"
exit 0
fi
else
echo "file not found"
fi
else
echo ".dat file not found"
fi

echo "complete processing "$fname" at:"`date '+%I:%M:%S'`

# FUNC-STEP8
mv $fname.done $fname
rm "$newfile"_3 "$newfile"_2 "$newfile"_1

}
######################################Main###############################

# STEP1
echo "start processing "$filename" at:"`date '+%I:%M:%S'`

current_dir=`pwd`;
#echo "$current_dir"

currentyear=`date '+%y'`;

# STEP2

#echo "$currentyear"

rm "$current_dir"/listfile

# STEP3

`ls -ogltr *.sql |grep -v "total" > "$current_dir"/listfile`;
# STEP4
echo " STEP4"

exec 4<&0 <"$current_dir"/listfile
echo "here 1"
while read line;
do
#echo $line
echo "here 2"

# SUB-STEP1

fname=`echo $line |nawk '{ print $7}' `
#echo "$fname"
echo " here 3"

# SUB-STEP2
v_date_month=`echo $line | nawk '{print $4"-"$5"-" }' `
v_time=`echo $line | nawk '{print $6 }' `
filedate=$v_date_month$currentyear"-"$v_time
#echo $filedate

if [ -s $fname ];
then
if [ ! -f "$fname".load ];
then
sleep 2
if [ ! -f "$fname".load ];
then
echo ".Load file not found, skipping the $fname file from Processing"
error_code=2
echo "errorcode $error_code"
break
else
########## Call to function ##############
create_dat_file
fi
else
########## Call to function ##############
create_dat_file
fi
else
echo "$fname is zero bytes in size...exiting processing "
error_code=1
echo "errorcode $error_code"
break
fi

done
exec 0<&4 4<&-
echo "here 4"

if [ $error_code -eq 1 ]; then
echo "stop processing coz of filesize "
exit
elif [ $error_code -eq 2 ]; then
echo "Done file not found"
exit
else
echo " file processed"
fi
ASKED: Sep 20, 2004  2:47 PM GMT
UPDATED: September 21, 2004  1:37:40 PM GMT
0 pts.

Answer Wiki:
Which shell is executing this script? Could it be getting executed by a different shell from what you expect? You might want to put
#!/bin/ksh
or whatever at the front of the script.

Is this a problem with mismatched quotes or braces within the embedded nawk scripts? Does the problem still occur if you replace the nawk code by do-nothing code?
Last Wiki Answer Submitted:  Sep 21, 2004  10:58 AM (GMT)  by  Bertilak   0 pts.
To see other answers submitted to the Answer Wiki View Answer History.
Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _




 

In addition to the complexity to which Bertilak alludes, you should realize that “UNIX” is not necessarilly “Unix”
Here are some of the most common implementations of unix.
BSD: Berkeley, BSD
SunOS: Sun Microsystems, BSD/sys 5
Solaris: Sun Microsystems, Sys 5/BSD
Ultrix: Digital Equipment Corporation, BSD
OSF 1: Digital Equipment Corporation, BSD/sys 5
HPUX: Hewlett-Packard, Sys 5
AIX: IBM, Sys 5 / BSD
IRIX: Silicon Graphics, Sys 5
GNU/Linux: GNU, BSD/Posix
Source: http://www.iu.hio.no/~mark/unix/unix.html
The unix programming environment,
Edition 2.2,August 2001,
Mark Burgess,
University College Oslo,
Faculty of Engineering,
Oslo, Norway

 0 pts.

 

Guys Thanks for your input! it is working now.
I got it working by wierd solution, I deleted all lines from the function and added to it line by line, same lines and it works fine now!
problem could have been coz of some invisible character, i guess.
Thanks again for your time.

Rajkum

 0 pts.