Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

touch question 1

Status
Not open for further replies.

ns705

Programmer
Feb 4, 2004
29
GB
Hi,

can you please help me with one question?

I need to create a file if it does not exist.
Something about:

while [[ -f "$file" ]]; do sleep 1; done
touch $file || echo "can not create $file"

But there is a little possibility that such file is created in the time gap between while and touch.
And to my regret I faced it.
touch does not return bad code if the file exists.

Does any way exist to handle it?

Thank you in advance.

Anta

 
set -o noclobber
>$file || echo "can not create $file"
 
Thank you a lot, jad!

It really helps.

Anta
 
I'd like to ask one more question.
Could you please help me with it?

It works great in command line.
But it does not work in script:

#!/bin/ksh
filel=/dir1/dir2/filelname
set -o noclobber > $filelname || {
echo "$filelname not created"
return 8
}
return 0

I can see that the file is created during each run.

Anta
Anta
 
Code:
#!/bin/ksh
set -o noclobber
filel=/dir1/dir2/filelname

> $filel
if [ $? -eq 0 ] ; then
    echo "$filel not created"
    return 8
else
    return 0
fi

maybe :)
 
Thank you a lot, jad.

It really works.

I realize I'm becoming impudent and drawing you away your work but maybe you will find a couple minutes to answer one more question please.

The problem is nobody here can answer.

Does this noclobber option means that all directions to all the creating files will be under its action?
I mean if in one part of script I set noclobber, all the files will not be created till the moment the command
set +o noclobber
will run in the other part of script.
Is it so?

Thanks in advance

Anta
 
yes ...

i believe that you can do it without set by forcing (eg. 'rm -f') or '>|' to work without clobbering.

Jon
 
Anta,

Are you trying to implement a kind of locking system ?
If so, use a directory as lock because the mkdir command is unsplitable (no "test then create") on many unix systems and return the succes / failure.

Code:
lockdir=/tmp/$(basename $0)-lockdir

#Wait for the lock
while true; do
 if mkdir "$lockdir" 2>/dev/null; then
   # Ok we just created the lock dir so exit the waiting loop
   break
 fi
 # We failed to create the lock dir so wait and try again
 echo waiting for lock "$lockdir"
 sleep 1;
done

# Here we have the lock

# Do our work

for i in 1 2 3 4 5 6 7 8 9; do
   echo working ...
   sleep 1;
done

# Now release the lock
rmdir $lockdir

--------------------

Denis
 
Thank you, Denis!

Yes, it is so.
The problem was I had to use files, not directories.

But in future I could use the technique you suggested.

Thank you a lot,

Anta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top