script to remove and recreate a lock file

Hi all,
i have a small script to remove locks for the prevous day and create new lock for processing in the path on my server
i have made something like this

#!/bin/sh
#lock_remover
#script to remove regular lockfiles and hang
curr_month=`date "+%b"`
2day=`date "+%_d"`
cd /rbs/batchProcs/locks
curr_status=`ls -ltr`
for i in $curr_status
do
if [$6 !=$curr_month && $7!=$2day] then
locked_file=$9
rm -f $locked_file
touch $locked_file
fi
done
exit

it returning this error
lock_remover: line 6: 2day=: command not found
lock_remover: line 15: syntax error near unexpected token `fi'
lock_remover: line 15: `fi'

thanks

Several errors:
variable names cannot start with a number in most modern shells. 2day has to be something else.

This line :

if [$6 !=$curr_month && $7!=$2day] then

needs to be :

if [ $6 !=$curr_month && $7!=$2day ]; then

Lose the $2day variable name, I did not change it. the [ and ] characters have to be
separated by spaces, you need a semicolon between ] and the word "then"

Lastly, the $6 $7 and $9 variables are not defined anywhere, you are confusing awk syntax with shell syntax.

What shell are you using? What OS?
Please post the output of:

echo $SHELL
uname -a

On second thought: You probably want something like this

  1. create a dummy file with a date of today, the first second of today
  2. use find to get files older than the dummy file
  3. touch a new copy of the file
#!/bin/ksh
# touch a file to set date to first second of today
 touch -t `date +%Y%m%d0000` dummy
 #find older files in directory
 find /rbs/batchProcs/locks -type f ! -newer dummy |
 while read fname
 do
      rm $fname
      touch $fname
      echo "`date` $fname changed"
 done  > logfile

Please post a sample of the format of a directory listing of these files which makes it clear what the time and date format looks like and makes it clear whether there are subdirectories under the lock directory:

cd /rbs/batchProcs/locks
ls -lar
1 Like

Good point, methyl. What he is saying 'the find command will clobber files in subdirectories that may not be lock files'