📄 fto.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>File test operators</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Tests"HREF="tests.html"><LINKREL="PREVIOUS"TITLE="Tests"HREF="tests.html"><LINKREL="NEXT"TITLE="Other Comparison Operators"HREF="comparison-ops.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="tests.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 7. Tests</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="comparison-ops.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="FTO"></A>7.2. File test operators</H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="RTIF"></A>Returns true if...</B></P><DL><DT><SPANCLASS="TOKEN">-e</SPAN></DT><DD><P>file exists</P></DD><DT><SPANCLASS="TOKEN">-a</SPAN></DT><DD><P>file exists</P><P>This is identical in effect to <SPANCLASS="TOKEN">-e</SPAN>. It has been <SPANCLASS="QUOTE">"deprecated,"</SPAN> <ANAME="AEN2915"HREF="#FTN.AEN2915">[1]</A> and its use is discouraged.</P></DD><DT><ANAME="REGULARFILE"></A><SPANCLASS="TOKEN">-f</SPAN></DT><DD><P>file is a <TTCLASS="REPLACEABLE"><I>regular</I></TT> file (not a directory or device file)</P></DD><DT><SPANCLASS="TOKEN">-s</SPAN></DT><DD><P>file is not zero size</P></DD><DT><SPANCLASS="TOKEN">-d</SPAN></DT><DD><P>file is a directory</P></DD><DT><SPANCLASS="TOKEN">-b</SPAN></DT><DD><P>file is a block device (floppy, cdrom, etc.) </P></DD><DT><SPANCLASS="TOKEN">-c</SPAN></DT><DD><P>file is a character device (keyboard, modem, sound card, etc.)</P></DD><DT><SPANCLASS="TOKEN">-p</SPAN></DT><DD><P>file is a <AHREF="special-chars.html#PIPEREF">pipe</A></P></DD><DT><SPANCLASS="TOKEN">-h</SPAN></DT><DD><P>file is a <AHREF="external.html#SYMLINKREF">symbolic link</A></P></DD><DT><SPANCLASS="TOKEN">-L</SPAN></DT><DD><P>file is a symbolic link</P></DD><DT><SPANCLASS="TOKEN">-S</SPAN></DT><DD><P>file is a <AHREF="devproc.html#SOCKETREF">socket</A></P></DD><DT><SPANCLASS="TOKEN">-t</SPAN></DT><DD><P>file (<AHREF="io-redirection.html#FDREF">descriptor</A>) is associated with a terminal device</P><P>This test option may be used to check whether the <TTCLASS="FILENAME">stdin</TT> (<TTCLASS="USERINPUT"><B>[ -t 0 ]</B></TT>) or <TTCLASS="FILENAME">stdout</TT> (<TTCLASS="USERINPUT"><B>[ -t 1 ]</B></TT>) in a given script is a terminal.</P></DD><DT><SPANCLASS="TOKEN">-r</SPAN></DT><DD><P>file has read permission (<SPANCLASS="emphasis"><ICLASS="EMPHASIS">for the user running the test</I></SPAN>)</P></DD><DT><SPANCLASS="TOKEN">-w</SPAN></DT><DD><P>file has write permission (for the user running the test)</P></DD><DT><SPANCLASS="TOKEN">-x</SPAN></DT><DD><P>file has execute permission (for the user running the test)</P></DD><DT><SPANCLASS="TOKEN">-g</SPAN></DT><DD><P>set-group-id (sgid) flag set on file or directory</P><P>If a directory has the <TTCLASS="REPLACEABLE"><I>sgid</I></TT> flag set, then a file created within that directory belongs to the group that owns the directory, not necessarily to the group of the user who created the file. This may be useful for a directory shared by a workgroup.</P></DD><DT><SPANCLASS="TOKEN">-u</SPAN></DT><DD><P><ANAME="SUIDREF"></A></P><P>set-user-id (suid) flag set on file</P><P>A binary owned by <ICLASS="FIRSTTERM">root</I> with <TTCLASS="REPLACEABLE"><I>set-user-id</I></TT> flag set runs with <ICLASS="FIRSTTERM">root</I> privileges, even when an ordinary user invokes it. <ANAME="AEN3014"HREF="#FTN.AEN3014">[2]</A> This is useful for executables (such as <BCLASS="COMMAND">pppd</B> and <BCLASS="COMMAND">cdrecord</B>) that need to access system hardware. Lacking the <ICLASS="FIRSTTERM">suid</I> flag, these binaries could not be invoked by a <ICLASS="FIRSTTERM">non-root</I> user.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">-rwsr-xr-t 1 root 178236 Oct 2 2000 /usr/sbin/pppd</TT> </PRE></TD></TR></TABLE> </P><P>A file with the <TTCLASS="REPLACEABLE"><I>suid</I></TT> flag set shows an <ICLASS="FIRSTTERM">s</I> in its permissions.</P></DD><DT><SPANCLASS="TOKEN">-k</SPAN></DT><DD><P><TTCLASS="REPLACEABLE"><I>sticky bit</I></TT> set</P><P>Commonly known as the <ICLASS="FIRSTTERM">sticky bit,</I> the <ICLASS="FIRSTTERM">save-text-mode</I> flag is a special type of file permission. If a file has this flag set, that file will be kept in cache memory, for quicker access. <ANAME="AEN3037"HREF="#FTN.AEN3037">[3]</A> If set on a directory, it restricts write permission. Setting the sticky bit adds a <ICLASS="FIRSTTERM">t</I> to the permissions on the file or directory listing.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">drwxrwxrwt 7 root 1024 May 19 21:26 tmp/</TT> </PRE></TD></TR></TABLE> </P><P>If a user does not own a directory that has the sticky bit set, but has write permission in that directory, she can only delete those files that she owns in it. This keeps users from inadvertently overwriting or deleting each other's files in a publicly accessible directory, such as <TTCLASS="FILENAME">/tmp</TT>. (The <ICLASS="FIRSTTERM">owner</I> of the directory or <ICLASS="FIRSTTERM">root</I> can, of course, delete or rename files there.)</P></DD><DT><SPANCLASS="TOKEN">-O</SPAN></DT><DD><P>you are owner of file</P></DD><DT><SPANCLASS="TOKEN">-G</SPAN></DT><DD><P>group-id of file same as yours</P></DD><DT><SPANCLASS="TOKEN">-N</SPAN></DT><DD><P>file modified since it was last read</P></DD><DT><SPANCLASS="TOKEN">f1 -nt f2</SPAN></DT><DD><P>file <TTCLASS="REPLACEABLE"><I>f1</I></TT> is newer than <TTCLASS="REPLACEABLE"><I>f2</I></TT></P></DD><DT><SPANCLASS="TOKEN">f1 -ot f2</SPAN></DT><DD><P>file <TTCLASS="REPLACEABLE"><I>f1</I></TT> is older than <TTCLASS="REPLACEABLE"><I>f2</I></TT></P></DD><DT><SPANCLASS="TOKEN">f1 -ef f2</SPAN></DT><DD><P>files <TTCLASS="REPLACEABLE"><I>f1</I></TT> and <TTCLASS="REPLACEABLE"><I>f2</I></TT> are hard links to the same file</P></DD><DT><SPANCLASS="TOKEN">!</SPAN></DT><DD><P><SPANCLASS="QUOTE">"not"</SPAN> -- reverses the sense of the tests above (returns true if condition absent).</P></DD></DL></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="BROKENLINK"></A><P><B>Example 7-4. Testing for broken links</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # broken-link.sh 3 # Written by Lee bigelow <ligelowbee@yahoo.com> 4 # Used in ABS Guide with permission. 5 6 # A pure shell script to find dead symlinks and output them quoted 7 #+ so they can be fed to xargs and dealt with :) 8 #+ eg. sh broken-link.sh /somedir /someotherdir|xargs rm 9 # 10 # This, however, is a better method: 11 # 12 # find "somedir" -type l -print0|\ 13 # xargs -r0 file|\ 14 # grep "broken symbolic"| 15 # sed -e 's/^\|: *broken symbolic.*$/"/g' 16 # 17 #+ but that wouldn't be pure Bash, now would it. 18 # Caution: beware the /proc file system and any circular links! 19 ################################################################ 20 21 22 # If no args are passed to the script set directories-to-search 23 #+ to current directory. Otherwise set the directories-to-search 24 #+ to the args passed. 25 ###################### 26 27 [ $# -eq 0 ] && directorys=`pwd` || directorys=$@ 28 29 30 # Setup the function linkchk to check the directory it is passed 31 #+ for files that are links and don't exist, then print them quoted. 32 # If one of the elements in the directory is a subdirectory then 33 #+ send that subdirectory to the linkcheck function. 34 ########## 35 36 linkchk () { 37 for element in $1/*; do 38 [ -h "$element" -a ! -e "$element" ] && echo \"$element\" 39 [ -d "$element" ] && linkchk $element 40 # Of course, '-h' tests for symbolic link, '-d' for directory. 41 done 42 } 43 44 # Send each arg that was passed to the script to the linkchk() function 45 #+ if it is a valid directoy. If not, then print the error message 46 #+ and usage info. 47 ################## 48 for directory in $directorys; do 49 if [ -d $directory ] 50 then linkchk $directory 51 else 52 echo "$directory is not a directory" 53 echo "Usage: $0 dir1 dir2 ..." 54 fi 55 done 56 57 exit $?</PRE></TD></TR></TABLE><HR></DIV><P><AHREF="zeros.html#COOKIES">Example 28-1</A>, <AHREF="loops.html#BINGREP">Example 10-7</A>, <AHREF="loops.html#FILEINFO">Example 10-3</A>, <AHREF="zeros.html#RAMDISK">Example 28-3</A>, and <AHREF="contributed-scripts.html#MAILFORMAT">Example A-1</A> also illustrate uses of the file test operators.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2915"HREF="fto.html#AEN2915">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P> Per the 1913 edition of <SPANCLASS="emphasis"><ICLASS="EMPHASIS">Webster's Dictionary</I></SPAN>: <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 Deprecate 2 ... 3 4 To pray against, as an evil; 5 to seek to avert by prayer; 6 to desire the removal of; 7 to seek deliverance from; 8 to express deep regret for; 9 to disapprove of strongly.</PRE></TD></TR></TABLE> </P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN3014"HREF="fto.html#AEN3014">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Be aware that <ICLASS="FIRSTTERM">suid</I> binaries may open security holes. The <ICLASS="FIRSTTERM">suid</I> flag has no effect on shell scripts.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN3037"HREF="fto.html#AEN3037">[3]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>On modern UNIX systems, the sticky bit is no longer used for files, only on directories.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="tests.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="comparison-ops.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Tests</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="tests.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Other Comparison Operators</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -