Open file function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Open file function
# 1  
Old 06-11-2014
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.

Code:
 #!/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.

Code:
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.

Last edited by maverick72; 06-11-2014 at 05:27 AM..
# 2  
Old 06-11-2014
Could you please format your post and make it readable, your carriage returns seem to have vanished? Thanks...
# 3  
Old 06-11-2014
Browser is firefox at work and seems to hate my code.

Had to fix with phone :-(
# 4  
Old 06-11-2014
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.
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-11-2014
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.
# 6  
Old 06-11-2014
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
Code:
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...
This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-11-2014
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Programming

Function open() sets errno

I am opening a text file using open() system call in O_RDONLY mode. open() returns me a valid handler but also sets errno to 13 i.e. EACCES(Permission denied). Question is when open() is returning a valid handler then why does it sets the errno? Should not errno be set only in case of error... (10 Replies)
Discussion started by: rupeshkp728
10 Replies

3. Shell Programming and Scripting

Help !! perl open function

Help Please perl Gurus, I am trying to add ungrouped passengers in a group and I creating a script however it fails on first step only I tried all the options it returns following error. syntax error at junki line 4, near "open " Execution of junki aborted due to compilation errors. ... (2 Replies)
Discussion started by: dynamax
2 Replies

4. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

5. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

6. Red Hat

cannot set user id: Resource temporarily unavailable (not open file/open process related)

First post, sorry to be a bother but this one has been dogging me. I have a process user (java application server) that trips a resource limit every couple weeks and need help finding what limit we're hitting. First, this is what's running: This is the error when jobs are run or the... (0 Replies)
Discussion started by: Katahdin
0 Replies

7. Programming

Open function of sys/stat.h

If a process already has the entire file locked for read and write using newstruct.l_type = F_WRLCK; what would happen if another process would try to open it in read only mode using open(filename, O_RDONLY); ? I want to check if the file exists and I want it to work even if another process has... (4 Replies)
Discussion started by: cyler
4 Replies

8. Programming

Some how the open(file,flag, acc) returns 0 and write to the screen, instead of the file ???

I am out of idea what to do to resolve the problem! I need to use the open(file, for.., access) function to write a file. Never have the situation like that: it is return 0 - zero. As a result all write(..) going to the screen! What the problem it could be? I do not even know... (2 Replies)
Discussion started by: alex_5161
2 Replies

9. UNIX and Linux Applications

Sybase help: Open client, bcp function

To begin: I use Linux The Problem: I need bcp functionality for scripts. Perl modules, such as Sybase:xfer, require ctlib which comes with Sybase Open Client. Talking with Sybase sales reps is an exercise in futility and hate. They know absolutely nothing about their own products and will... (0 Replies)
Discussion started by: Bubnoff
0 Replies

10. Shell Programming and Scripting

function to test if file is open

I need to write a function that will work in sh/ksh shell that will test to see if a file has already been opened for writting by another user has anyone written something like this? (3 Replies)
Discussion started by: johnsonbryce
3 Replies
Login or Register to Ask a Question