Open file function

Hello all, just a quick little part of code i'm writing to check if the file i'm writing too in my automatic process is not being written too manually.

 #!/bin/bash

FUSER=$(/sbin/fuser -s /toto.tmp >/dev/null 2>&1)
LSOF=$(/usr/sbin/lsof | grep -q "toto.tmp")
PGREP=$(pgrep -f "toto.tmp" > /dev/null )

# Manage error

error_log () {

printf "$(date '+%b %e %T') [ $1 ]: %-30s\n" "$2" | tee -a toto.log
}  

check_if_open () {

echo FUSER: $FUSER
echo LSOF: $LSOF
echo PGREP: $PGREP

if [[ $FUSER -eq '0' ]] || [[ $LSOF -eq '0' ]] || [[ "X$PGREP" != "X" ]]; then
  error_log "ERROR" "File toto.tmp is already in write mode"
else
  echo nothing
fi
}

echo Start
check_if_open
echo END

The above code doesn't work and I know i could use something like this bellow but i'm sure there is an easier (cleaner) way.

pgrep -f "toto.tmp" > /dev/null

if [[ $? -eq '0' ]]; then
error_log blabla
fi

/usr/sbin/lsof | grep -q "toto.tmp"
if [[ $? -eq '0' ]]; then
  error_log blablabla
fi
fuser...
if [[ $? -eq '0' ]]; then
  error_log blablabla
fi

Thanks.

Could you please format your post and make it readable, your carriage returns seem to have vanished? Thanks...

Browser is firefox at work and seems to hate my code.

Had to fix with phone :frowning:

First thing I notice is the closing braces of the functions. They should either be on a new line or preceded by a semi-colon.

The syntax of my code is ok on my screen but trying to fix it on my phone is a teadious task. I will try to find a different workstation with a good browser. Thanks for the help through.

I'm guessing only that you want to use the commands' exit codes in your first script. Then you should also print them using e.g. echo $? in the assignment statements. What you could do to clean up/compress your code is trying out

fuser -s /toto.tmp || pgrep -f /toto.tmp || lsof  /toto.tmp && echo open || echo closed 

If any of those command succeeds, that means the file is open (NOT necessarily for writing!). I think that using three of those commands might be overkill...

Found a decent browser....

Which would you think would be best? Wanted to make absolutly sure that they (the production) can't not second guess me on that issue (maybe you know how it is in big businesses).

The purpose is of course that the automated process stop/wait until the file is free of anthing.