0

I'm trying to run a bit of perl inside a ksh file and not getting what I would expect from it. Rather than simply getting some sort of error if I was doing something syntactically incorrect, it finishes running and creates lots of .bak files but has not prepended $data onto every line of the file as I would have expected.

What I've got so far (contains is a function I declared earlier in the file but that part works fine):

if [[ $# != 3 ]];then
     echo "Please specify 1 folder containing data files as well as 1 from and 1 to system id."
else
    for file in `ls $1`;do
        filePath=$1$file
        fileName=`echo $filePath | awk -F'/' '{print$5}'`
        contains $fileName "ACCUMS" && (
            contains $fileName ".bak" || (
                echo "Loading file: "$fileName  
                date=echo $fileName | awk -F'_' '{print$5}' | sed 's/.txt//'   
                data=$fileName"|"$2"|"$3"|"$date"|"  
                echo $data  
                echo /usr/local/bin/perl -pi.bak -e 's/^/$data/' $filePath  
                logFile="log"$curFile".txt"  
                echo "Log file: "$logFile  
                echo "Done loading file: "$fileName  
                $ORACLE_BIN/sqlldr user/pass@server control=control_file.ctl data=$filePath log=$logFile  
                curFile=$curFile+1
            )
        ) || echo $fileName" not part of Accums.  Skipped."
    done
fi

To make it worse when I run the following straight up in PuTTY it works, so it appears to be the way that I'm trying to call the perl code (even though I've looked a several examples doing similar things) from the .ksh file:

perl -pi.bak -e 's/^/hi|/' /path/data/file_Im_working_with.txt

If it matters, the log files are also not quite doing what I want. Rather than making lots of them for each sqlldr call it just overwrites log0.txt every time. I've checked, the data files are in the correct location and everything so this may be part of my problem or something completely unrelated.

Thanks in advance!

EDIT

In addition to what ThisSuitIsBlackNot and dms said, I also didn't need to specify the location of perl on this system and doing so was actually messing it up, which is weird because I checked and perl stuff was there.

Community
  • 1
  • 1
dlkulp
  • 2,204
  • 5
  • 32
  • 46

2 Answers2

3

It looks like you are echoing the perl command but not actually running it.

dms
  • 797
  • 1
  • 5
  • 15
1

As dms pointed out, you are echoing the perl command, not executing it. Also, if you want the variable data to be interpolated, you need to use double quotes, not single quotes. Change this:

echo /usr/local/bin/perl -pi.bak -e 's/^/$data/' $filePath

to this:

/usr/local/bin/perl -pi.bak -e "s/^/$data/" $filePath

(the echo is not necessary to run the command)

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
  • hm, it's still not prepending $data even after removing the echo and switching to double quotes. Is it possible that it doesn't like having the file be in a different directory? – dlkulp Aug 09 '13 at 22:19
  • @dlkulp `echo $filePath` immediately before this line and make sure the path you're providing is an actual file that you have write permissions to. – ThisSuitIsBlackNot Aug 09 '13 at 22:24
  • ah, looks like I also didn't need to specify the location of perl on this system, so it works now! – dlkulp Aug 09 '13 at 22:26