Re: [ILUG] sed challenge (really a shell question)

From: Brian Foster (blf at domain utvinternet.ie)
Date: Fri 05 Apr 2002 - 15:04:14 IST


  | From: Stephen_Reilly at domain dell.com
  | Date: Fri, 5 Apr 2002 07:33:52 -0600
  |[ ... ]
  | $ sed -e '1a\
  | > line1\
  | > line2' /etc/pam.d/su | cat > /etc/pam.d/su
  |
  | With the append option. It's unfortunate to use cat but otherwise the file
  | will overwrite itself. A gold star for anyone who can remove it without
  | writing to another file ... sed output redirection has often caused me
  | headaches in the past. Except with certain emulaters that just run things
  | sequentially ...

 I would _never_ use the above!
 the file is truncated to zero-length (due to the zap)
 before (on most systems) sed ever even has a chance
 to open it, much less read all of the contents.

 if you want to modify the file in-place --- note my
 sed solution did NOT try to do this! --- then sed(1)
 is simply the wrong tool. use ed(1):

 $ ed file
 <gibberish from ed>
 1a
 lineA
 lineB
 .
 w
 <more gibberish from ed>
 q
 $

 this is still not ideal, as there is still a race with
 any concurrent readers (or, worse, writers).

 b.t.w., the only time I've ever had problems with
 output redirection (from sed or anything else) is when
 I inadvertently assume it's not buffered (including
 assuming stdout and stderr appear in program order).

cheers!
        -blf-

--
 Innovative, very experienced, Unix and      | Brian Foster    Dublin, Ireland
 Chorus (embedded RTOS) kernel internals     | e-mail: blf at domain utvinternet.ie
 expert looking for a new position ...       | mobile: (+353 or 0)86 854 9268
  For a resume, contact me, or see my website  http://www.blf.utvinternet.ie


This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:15:51 GMT