I need to read a trigger file whose name can be:
ABC!DEF@2014.txt
or
ABC!DEF@2014,2015.txt
and then carry out functions on those inputs.
Currently I am doing:
YEAREXPORT {
FILE= xyz.txt
ls ABC* -l > ${FILE}
if [ ! -f ${FILE} ]; then
log_err "Trigger File ABC* does not exist!"
fi
while read LINE
do
log_msg "Read line ${LINE}"
#Check if Two years have entered
COUNTCOMMA =$(echo "${LINE}" | grep -o "," | wc -l)
if [${COUNTCOMMA} == 0]; then
log_msg "Its zero"
else
log_msg "Its not zero"
fi
done < ${FILE}
}
Now while the Read line shows up in the log. The countcomma part is not executing, I just get this:
Read line -rwx------+ 1 piruser Domain Users 0 2014-06-13 19:33 ABC!DEF@2014,2015.txt
YearExport:21: 1 not found
YEAREXPORT {
FILE= xyz.txt
ls ABC* -l > ${FILE}
if [ ! -f ${FILE} ]; then
log_err "Trigger File ABC* does not exist!"
fi
while read LINE
do
log_msg "Read line ${LINE}"
#Check if Two years have entered
COUNTCOMMA=`echo "${LINE}" | grep -o "," | wc -l)`
if [${COUNTCOMMA}=0]; then
log_msg "Its zero"
else
log_msg "Its not zero"
fi
done < ${FILE}
}
So while the COUNTCOMMA now works, I keep getting bad pattern error for the if condition.
what is your shell and os ?
and can u write full code ?
your code must be like these
YEAREXPORT {
FILE= xyz.txt
ls ABC* -l > ${FILE}
if [ ! -f ${FILE} ]; then
log_err "Trigger File ABC* does not exist!"
fi
while read LINE
do
log_msg "Read line ${LINE}"
#Check if Two years have entered
COUNTCOMMA=`echo "${LINE}" | grep -o "," | wc -l`
if [ ${COUNTCOMMA}=0 ]; then
log_msg "Its zero"
else
log_msg "Its not zero"
fi
done < ${FILE}
}
Opening parenthesis missing. Also there is no need to put those unless you have used $(...) but in that case you dont need `...`
if [${COUNTCOMMA}=0]; then
You have not put spaces around the opening and closing square bracket as ygemici suggested. Also, Make sure put spaces around = or == whatever you use.