Send filename as variable in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send filename as variable in a shell script
# 1  
Old 03-16-2011
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:
Code:
 
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.

Last edited by vbe; 03-16-2011 at 02:17 PM.. Reason: use code tags please
# 2  
Old 03-16-2011
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:
Code:
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:

Code:
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.
# 3  
Old 03-16-2011
Code:
#!/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

?
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 03-16-2011
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.
# 5  
Old 03-16-2011
Quote:
Originally Posted by neva
The first tip provided gave me an error 'SQL*Loader-503: Error appending extension to file'.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

2. Shell Programming and Scripting

Fetch the latest filename shell script

Hi I want to fetch the latest file form the list example example= filename RATE_STATE_SETUPS.20151222.ccyymmdd.hhmmss.txt File pick which have latest ccyymmdd.hhmmss list of file in directory are RATE_STATE_SETUPS.20151222.20151222.170101.txt... (5 Replies)
Discussion started by: MOHANP12
5 Replies

3. Shell Programming and Scripting

How to sort the timestamp in the filename in shell script?

originally the shellscript #ln_file_name=`echo $ld_interface_date"_"${8}".csv"` #ln_file_name=`echo 201202011527_HL_HLTM1_B04A.csv` ln_file_name="*"`echo ${7}".csv"` get_file_list_1=$log_path"tm1_file_list.gfl1" cd ${source_path} echo "Try to find any file exist in the... (10 Replies)
Discussion started by: feilhk
10 Replies

4. Shell Programming and Scripting

Help shell script to list filename file_path

HI owner date and time and size of file in a row shell script to list filename file_path i have tried the below code present_dir=`pwd` dir=`dirname $0` list=`ls -lrt | awk {'print $9,$3,$6,$7,$8'}` size=`du -h` path=`cd $dir;pwd;` printf "$list" printf "$path" printf " $size" ... (4 Replies)
Discussion started by: abiram
4 Replies

5. Shell Programming and Scripting

Shell script if [[ -L <filename> ]]

Hi Please describe about following condition if ] in shell script. Also please provide the link related all the flags which are applicable for if condition in Shell (4 Replies)
Discussion started by: munna_dude
4 Replies

6. Shell Programming and Scripting

Call a awk script with variable and input filename

HI, MY question is a very simple one: if i want to call an awk script with the input file name and also pass a variable value , then how to do it. #>awk -f my_script.awk -v variable=value my_inputfile.txt I can't do it like this. throws error: awk: my_script.awk:18:... (0 Replies)
Discussion started by: Onkar Banerjee
0 Replies

7. Shell Programming and Scripting

Which shell script will call if i execute sh (without filename)?

Hi Friends, The below shell script is written by third party to create B2k_session_id.iam trying to execute this script.When i execute below script it is calling some other scripts.How to find which scripts is calling? . `execom commfunc.com` echo " $PRESENTATION_MODE " if then echo... (1 Reply)
Discussion started by: vadlamudy
1 Replies

8. UNIX for Dummies Questions & Answers

How to send e-mail from shell script ( C shell )?

Hi , How to send e-mail from shell script ( C shell ) . Mailx command is not working ( It didn't giving error also ). Please help me (2 Replies)
Discussion started by: arukuku
2 Replies

9. Shell Programming and Scripting

Shell script to use the last modified filename in a variable

Forgive me if this is a trivial question, but I haven't been able to find the answer to this. Basically I've got a list of files in a particular directory that have the general form t_*.dat. (I have other files in the same directory as well). Essentially what I want to do is obtain the name... (1 Reply)
Discussion started by: lost.identity
1 Replies

10. Shell Programming and Scripting

How to send filename as variable in a shell script

Can we can pass the filename as variable in the shell script. Sending the filename as a parameter file, the shell script takes the filename, needs to replace the string containing the filename with the variable in the shell script. EX: test1.sh is the shell script and takes file1.csv as... (6 Replies)
Discussion started by: gthokala9
6 Replies
Login or Register to Ask a Question