shell script - search a file and perform some action

hi,

i have a service on unix platform, it will generate traces in a particular folder
i want to check using shell script if traces exist, then perform some action else continue to be in loop.
filename is service.tra

can you please help?

thanks

What did you try so far? Where did you get stuck?

two=2
trace=service.tra
while [ $two != 0 ]
do
cd /home/linus
find . -name "service.tra"
size=$(du -sk $trace |awk '{print $1}')
if [ $size -gt 3000 ]
   then
      cat $trace > temp1
      mv temp1 /home/linus/gaurav
       rm $trace
       $one = `expr $one + 1`
    else
      echo "size is less than 3 MB"
fi
done

this is what i try to achieve. I want to run this script infinitely, check if the trace file is generated, if it is generated, calculate its size and then move it to other folder and remove it.

this trace file will be generated regularly and if we do not remove it then its size will increase

problem i am facing is that i am not able to check if file is present in that folder

The script could look like:

#!/bin/bash

FILENAME=service.tra
S_PATH=/path/to/your/trace
D_PATH=/home/linus/gaurav

while :; do
   FOUND=$(find $S_PATH -type f -name "$FILENAME" -size +3000c -print)
   if [[ -n $FOUND ]]; then
      echo "File $FILENAME is larger than 3k - moving it."
      mv $FOUND $D_PATH
   fi
   sleep 3
done

exit 0

For infinite loops maybe put a sleep in there - else it might push your box to 100% CPU usage. In this example I use "find" but when the destination of the mv is also below this path, you'll get a problem, just as a site note. You might to use -purge in the find or check with ls -l or stat for the size of the file instead and check with -e if the file exists before that.

$one = `expr $one + 1`

Will not work since in a shell script there may be no spaces on the left or right of the equal sign. Also when declaring a variable there may be no $ in the name of the variable.

if [ -f service.tra ] 
then
  DO something
fi