Send filename as variable in a shell script

I am having some trouble with a shell script I am writing. In the program I pass the path of where certain file names exist. I then want to loop through each file name and pass it as a DATA variable when calling sqlldr for a control file. This is what I have:

 
ls $FILEPATH/${FILENAME}*.csv
 
ALLFILES='ls $FILEPATH/${FILENAME}*.csv'

if [ $? -ne 0 ]
then
  echo "\n    No inbound files to process"
  exit 1
fi
## Loop through each file and load to staging table 
for EACHFILE in $ALLFILES
do
TEMP_FILE=${EACHFILE}a
if [ $? -ne 0 ]
then
  echo "\n    Error"
else
  echo "\nCalling Control File"
sqlldr "$FCP_LOGIN" silent=feedback silent=header silent=partitions control=$CONTROLFILE data=$TEMP_FILE 
 

This does NOT send the file name to the DATA variable and I receive an error message stating no file was found to process. Any ideas on what I'm doing wrong? Any help is greatly appreciated.

Try echo "$TEMP_FILE" to make sure it's really the value you think it is.

Put $TEMP_FILE in quotes like above, because otherwise the shell will split it apart when it contains spaces.

This won't do what you think it does:

ALLFILES='ls $FILEPATH/${FILENAME}*.csv'

This sets it to the literal string "ls $FILEPATH/${FILENAME}*.csv". I think you meant to use backticks instead.

Which would be a combo useless use of ls and useless use of backticks anyway. You don't need to use ls or backticks here, the shell's flexible enough to use its own globbing directly:

for EACHFILE in $FILEPATH/${FILENAME}*.csv
do
...
done

I think that was the root problem here. You were feeding the string "ls $FILEPATH/${FILENAME}*.csv" into the program instead of looping over each file.

#!/usr/bin/ksh

FILEPATH=/choose/whatever/you/need
FILENAME=whatevername
FCP_LOGIN=whateverlogin_you_need
CONTROLFILE=whatever_cntrl_you_need

find $FILEPATH -type f -name "${FILENAME}*.csv" | while read a
do
sqlldr "$FCP_LOGIN" silent=feedback silent=header silent=partitions control=$CONTROLFILE data=$a
done

?

1 Like

The first tip provided gave me an error 'SQL*Loader-503: Error appending extension to file'.

However the second tip fixed the issue.

find $FILEPATH -type f -name "${FILENAME}*.csv" | while read a
do
sqlldr "$FCP_LOGIN" silent=feedback silent=header silent=partitions control=$CONTROLFILE data=$a
done

My program is now working perfectly! Thanks for the help.

Without seeing the complete code you had, I can't tell you why. Both solutions should work but there may have been a minor slip up somewhere.