Hello,
First time on this forum. Hope to be back here soon...
I'am doing simple scripts in bash. I use bash because it is the only language I kind of know.
I have 2 directories,called /input and /output. The first one /input is régularly updated with new files from an ftp. I need to copy the file from /input to /output if a value, the $ValueF, is smaller in the newer file. In order to do this I've created a text file called FileDatabase.txt in /output directory where I store the filename and the corresponding ValueF. My script check the ValueF in the FileDatabase.txt and then copy in /output if needed. My problem is that for some reasons I have hard time updating the ValueF in FileDatabase.txt. I'am using a sed command. But this command only works when I am pasting it in the terminal but not inside my script
Really !
Here is my script I have simplify for clarity.
Code:
#!/bin/bash
# List all the files in INPUT directory. At each file correspond an ValueF. If the Value F is smaller
# in the INPUT directory, cp file from input directory to OUTPUT directory
partern=pattern
#Directories parameters
input_dir=/home/Data/INPUT
output_dir=/home/Data/OUTPUT
# On va d'abord looper sur tout les fichiers.
find ${input_dir} -name "$pattern" | sort -r > ${input_dir}/Input_filelist.txt
while read input_file;
do
# Extract filename
basefilename=`basename "$input_file"`
echo "$basefilename"
#Extract ValueF
Input_ValueF=`grep ValueF "$basefilename" | cut -d"=" -f2`
# remove leading 0
((Input_ValueF=10#${Input_ValueF}))
# Output file (same name as input file, different directory
output_file=${output_dir}/$basefilename
if [ ! -f ${output_file} ]; then
# First time, just copy to output dir
echo "First Time"
cp ${input_file} ${output_file}
# File up database
echo "${output_file} ${Input_ValueF}" >> ${output_dir}/FileDatabase.txt
sort -o ${output_dir}/FileDatabase.esv ${output_dir}/FileDatabase.txt
else
echo "Check FValue"
# Output file already exists.
# Check ValueF reading FileDatabase.txt
ValueF=`grep ${output_file} ${output_dir}/FileDatabase.txt | cut -d" " -f2`
echo "$Input_ValueF vs $ValueF "
# Compare both Value F
if [[ "$Input_ValueF" -lt "$ValueF" ]]; then
echo "better one"
# Overwrite old file with new one
cp ${input_file} ${output_file}
# Update database using sed command
sed '/${output_file}/ s/ ${ValueF} / ${ValueF} /' ${output_dir}/FileDatabase.txt > new
#wait ${!}
mv new ${output_dir}/FileDatabase.esv
fi
fi
echo
done < ${input_dir}/Input_filelist.txt
What is wrong with my sed command ??
If you have a better way and more reliable way to do this kind of thing with file, I'll be pleased to learn. ( shall I use a database ? how does it work ? )
THanks in advance
RENOO