I am using the below if condition to delete files .
if [ "$LINKTRIGGER" != "" ]
then
if [ ! -f ${LANDINGDIR}/${LINKTRIGGER}.* ]; then
log_err "Trigger File ${LINKTRIGGER} does not exist!"
fi
log_msg "Deleting the Linktrigger"
rm -v ${LANDINGDIR}/${LINKTRIGGER}.*
else
if [ ! -f ${LANDINGDIR}/${TRGFILE}.* ]; then
log_err "Trigger File ${TRGFILE} does not exist!"
echo nofile
fi
log_msg "Deleting the individual trigger"
rm -v ${LANDINGDIR}/${TRGFILE}.*
fi
This is working fine however this piece of code is not working
if [ ! -f ${LANDINGDIR}/${LINKTRIGGER}.* ]; then
log_err "Trigger File ${LINKTRIGGER} does not exist!"
fi
It was a little mess with place of else . Following may help.
if [ "$LINKTRIGGER" != "" ]
then
if [ ! -f ${LANDINGDIR}/${LINKTRIGGER}.* ]; then
log_err "Trigger File ${LINKTRIGGER} does not exist!"
else
log_msg "Deleting the Linktrigger"
rm -v ${LANDINGDIR}/${LINKTRIGGER}.*
fi
if [ ! -f ${LANDINGDIR}/${TRGFILE}.* ]; then
log_err "Trigger File ${TRGFILE} does not exist!"
echo nofile
else
log_msg "Deleting the individual trigger"
rm -v ${LANDINGDIR}/${TRGFILE}.*
fi
fi
Yes it is working but how do i use this in if condition as i need to use this in IF condition . My requirement is to check for test and delete all files like test1 or test2
fileexist=0
ls -d "$LANDINGDIR"/"$LINKTRIGGER."* |\
while read file ; do
if [ -f "$file" ] ; then
fileexist=1
fi
done
if [ $fileexist -eq 1 ] ; then
log_err "At least one Trigger File ${LINKTRIGGER} does exist!"
else
log_err "Trigger File ${LINKTRIGGER} does not exist!"
fi
The else / if syntax is something basic you can look for on the internet with a simple search looking for bash syntax.
If you want to delete more than one file you need it to be recursive, with the -r option for rm command.
Bash expands all the coincidences in that directory but it is only supported with [[ ]] syntax.
Maybe you are looking for something like this.
if [ "$LINKTRIGGER" != "" ]
then
if [[ ! ${LANDINGDIR}/${LINKTRIGGER}.* ]]
then
log_err "Trigger File ${LINKTRIGGER} does not exist!"
else
log_msg "Deleting the Linktrigger"
rm -rv ${LANDINGDIR}/${LINKTRIGGER}.*
fi
if [[ ! ${LANDINGDIR}/${TRGFILE}.* ]]
then
log_err "Trigger File ${TRGFILE} does not exist!"
echo nofile
else
log_msg "Deleting the individual trigger"
rm -rv ${LANDINGDIR}/${TRGFILE}.*
fi
fi