How to open, lock, write and close a file in ksh
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > How to open, lock, write and close a file in ksh




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    How to open, lock, write and close a file in ksh  
Herbert


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-22-06 11:10 PM

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')
flock WRITEFILE, LOCK_EX
I am not sure of write something into file
close WRITEFILE.

Can anyone help me with this?

Thanks






[ Post a follow-up to this message ]



    Re: How to open, lock, write and close a file in ksh  
Bill Marcum


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-22-06 11:10 PM

On 21 Jan 2006 19:40:16 -0800, Herbert
<kang_uni@hotmail.com> 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





[ Post a follow-up to this message ]



    Re: How to open, lock, write and close a file in ksh  
Herbert


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-06 01:58 AM

Bill Marcum
Jan 22, 4:25 pm   show options

Newsgroups: comp.unix.shell
From: Bill Marcum <bmar...@iglou.com> - Find messages by this author
Date: Sun, 22 Jan 2006 15:25:35 -0500
Local: Sun, Jan 22 2006 4:25 pm
Subject: Re: How to open, lock, write and close a file in ksh
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

On 21 Jan 2006 19:40:16 -0800, Herbert


<kang_...@hotmail.com> 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

-----------
Thanks for the reply but I don't understand the file lock stuff in your
reply.

until mkdir lock_dir; do sleep 1; done

How do I lock '/user/test.html' file? Can you please explain it again?






[ Post a follow-up to this message ]



    Re: How to open, lock, write and close a file in ksh  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-06 07:55 AM

2006-01-21, 19:40(-08), Herbert:
> 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')
> flock WRITEFILE, LOCK_EX
> I am not sure of write something into file
> close WRITEFILE.
>
> Can anyone help me with this?
[...]

You don't want to do it like that. A shell is not a programming
language, you have to think differently, you can't translate
word to word from another language.

--
Stéphane





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:35 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register