⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch45_10.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] 45.10 Removing a File Once It's Opened&nbsp;- for Security and Easy Cleanup</TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:54:31Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"><LINKREL="prev"HREF="ch45_09.htm"TITLE='45.9 The Unappreciated Bourne Shell  ":&quot; Operator'><LINKREL="next"HREF="ch45_11.htm"TITLE="45.11 The Multipurpose jot Command "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_09.htm"TITLE='45.9 The Unappreciated Bourne Shell  ":&quot; Operator'><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 45.9 The Unappreciated Bourne Shell  ":&quot; Operator'BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 45<BR>Shell Programming for the Initiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_11.htm"TITLE="45.11 The Multipurpose jot Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.11 The Multipurpose jot Command "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2060">45.10 Removing a File Once It's Opened&nbsp;- for Security and Easy Cleanup</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-52816"></A><ACLASS="indexterm"NAME="AUTOID-52819"></A><ACLASS="indexterm"NAME="AUTOID-52822"></A><ACLASS="indexterm"NAME="AUTOID-52825"></A>Once a process has<SPANCLASS="link">opened a file (<ACLASS="linkend"HREF="ch45_20.htm"TITLE="Overview: Open Files and File Descriptors ">45.20</A>)</SPAN>,UNIX won't delete the fileuntil the process closes it.(The <EMCLASS="emphasis">rm</EM> command only removes a link to the file from a directory,not the file itself.)</P><PCLASS="para">I've heard<SPANCLASS="link">arguments (<ACLASS="linkend"HREF="ch24_03.htm"TITLE="Unlinking Open Files Isn't a Good Idea ">24.3</A>)</SPAN>about whether removing a file while it's open is a goodidea.If you want to run a set of commands from a file, but not let anyoneelse read the list of commands you're using, you can write a shellscript that removes itself before doing anything else.(You should be aware that if you use a filesystem mounted by<SPANCLASS="link">NFS (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,NFS will just rename the &quot;removed&quot; file to a<SPANCLASS="link">hidden filename (<ACLASS="linkend"HREF="ch16_11.htm"TITLE="Showing Hidden Files with ls -A and -a ">16.11</A>)</SPAN>like <EMCLASS="emphasis">.nfsXXXXX</EM>.)</P><PCLASS="para">Here's a simple self-removing shell script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat doit</B></CODE>rm doit   # by now, shell has opened this file; we can remove itls doitmake bigprog   ...% <CODECLASS="userinput"><B>sh doit</B></CODE>ls: doit not foundcc   -target sun4 -c  routine.c   ...</PRE></BLOCKQUOTE></P><PCLASS="para">Here's a more typical script that opens and removes a file in<SPANCLASS="link"><EMCLASS="emphasis">/tmp</EM> (<ACLASS="linkend"HREF="ch21_03.htm"TITLE="Unique Names for Temporary Files ">21.3</A>)</SPAN>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch45_07.htm"TITLE="45.7 The exec Command ">exec</A> <ACLASS="co"HREF="ch45_21.htm"TITLE="45.21 n&gt;&amp;m: Swap Standard Output and Standard Error ">&lt;&amp;-</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat delme</B></CODE>#!/bin/shtemp=/tmp/delme$$              # file in /tmp (could be anywhere)echo &quot;This is line1.This is line2.This is line3.&quot; &gt; $temp        # put three lines in $templs -l $temp; wc $temp          # ls and count lines in the file<ACLASS="indexterm"NAME="AUTOID-52846"></A>exec &lt; $temp                   # take standard input from $tempread line; echo $line          # read and echo line 1 from $temprm $temp; echo rm returned $?  # remove $temp link; show statusls -l $temp; wc $temp          # the file is gone...?read line; echo $line          # but file is still open!read line; echo $lineexec &lt;&amp;-                       # close standard input (and file)% <CODECLASS="userinput"><B>delme</B></CODE>-rw-rw-r--   1 jerry    45 Sep 16 12:31 /tmp/delme22743      3      9     45 /tmp/delme22743This is line1.rm returned 0ls: /tmp/delme22743: No such file or directorywc: cannot open /tmp/delme22743This is line2.This is line3.</PRE></TD></TR></TABLE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_09.htm"TITLE='45.9 The Unappreciated Bourne Shell  ":&quot; Operator'><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 45.9 The Unappreciated Bourne Shell  ":&quot; Operator'BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_11.htm"TITLE="45.11 The Multipurpose jot Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.11 The Multipurpose jot Command "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.9 The Unappreciated Bourne Shell  ":&quot; Operator</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">45.11 The Multipurpose jot Command </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -