Re: How to open, lock, write and close a file in ksh
- From: Bill Marcum <bmarcum@xxxxxxxxx>
- Date: Sun, 22 Jan 2006 15:25:35 -0500
On 21 Jan 2006 19:40:16 -0800, Herbert
<kang_uni@xxxxxxxxxxx> wrote:
> I am a beginner in shell scripting.
> I have a question regarding file handling in shell script.
> There is a perl script as follows:
>
> open (WRITEFILE, ">>/user/test.html");
> flock WRITEFILE, LOCK_EX;
> print (WRITEFILE "test");
> ....
> print (WRITEFILE "<BR>");
> close WRITEFILE
>
> If I want to change this to ksh script, how can I do it?
>
> open(WRITEFILE, '/user/test.html')
"open" and "close" commands are usually not needed in shell scripts,
but when they are needed, files are opened and closed with redirection
commands like "exec >/usr/test.html" or "exec >&-"
> flock WRITEFILE, LOCK_EX
There is a flock command, but it is rarely included in Unix/Linux
distributions. In shell scripts, one usually uses a lock file or
directory, with the assumption that other scripts will test that before
writing the file.
until mkdir lock_dir; do sleep 1; done
> I am not sure of write something into file
Depending on the application:
echo something > $WRITEFILE
echo something >> $WRITEFILE
or
sed 's/oldstring/newstring/' $WRITEFILE > tempfile
mv tempfile $WRITEFILE
--
<james> abuse me. I'm so lame I sent a bug report to
debian-devel-changes
.
- Follow-Ups:
- Re: How to open, lock, write and close a file in ksh
- From: Herbert
- Re: How to open, lock, write and close a file in ksh
- References:
- How to open, lock, write and close a file in ksh
- From: Herbert
- How to open, lock, write and close a file in ksh
- Prev by Date: Skip writing to a named pipe if the reading process is not running
- Next by Date: Re: modifying case of dirs and files name
- Previous by thread: How to open, lock, write and close a file in ksh
- Next by thread: Re: How to open, lock, write and close a file in ksh
- Index(es):
Relevant Pages
|