2

I'm quite new to bash scripting and i was having a look at permissions and i was wondering if there was a way to use a while loop to make it so that whilst the user is opening say

"file.txt" it becomes read only to everyone else until that file has been closed by the user again.

Is this possible in linux?

Robert Mason
  • 181
  • 3
  • 15
  • Why do ask? Who do that locking in a shell script, and not inside the executable accessible that file? Do you want mandatory or advisory locking? – Basile Starynkevitch Oct 20 '12 at 12:48
  • 2
    Are you talking about [file locking](http://en.wikipedia.org/wiki/File_locking)? If so, then yes, Linux supports it. To do it from within a shell script, have a look at [flock(1)](http://linux.die.net/man/1/flock). Its availability will depend on the particular Linux distribution you're using. – ghoti Oct 20 '12 at 12:53
  • 1
    Though, this sounds like an [XY Problem](http://mywiki.wooledge.org/XyProblem). What are you REALLY trying to do? Describe the scenario, and we may be able to suggest a better way to achieve what you want. – ghoti Oct 20 '12 at 12:54
  • Ok so it is a CVS type system. So if user1 edits it and breaks it for example then it will be clear that it was him because when he last opened it as only one person can open the file at once. – Robert Mason Oct 20 '12 at 13:15
  • Boy, CVS is so old and completely obsolete. Learn quickly to use `git` or at least `svn` !!! You are losing your time to circumvent CVS deficiencies (there are too many of them). – Basile Starynkevitch Oct 20 '12 at 15:13

1 Answers1

1

You might want file locking. See this page, the lockfile(1) man page, this question (which is very similar).

But I am not sure that you are right to reason at the shell script level. I'll do it inside the application, probably with lockf(3) or flock(2). You should avoid mandatory locking.

Be aware that editors might do their own file locking, or do other things than just writing files.

Some editors or programs might write a temporary file, then rename(2) it

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547