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

📄 fto.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 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.57"><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"><TABLEWIDTH="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">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 7. Tests</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="comparison-ops.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="FTO">7.2. File test operators</A></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> and its use is	      discouraged.</P></DD><DT><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 (<ICLASS="EMPHASIS">for the	      user running the test</I>)</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>set-user-id (suid) flag set on file</P><P>A binary owned by <ICLASS="EMPHASIS">root</I>	      with <TTCLASS="REPLACEABLE"><I>set-user-id</I></TT> flag set	      runs with <ICLASS="EMPHASIS">root</I> privileges, even	      when an ordinary user invokes it.	      		<ANAME="AEN2654"HREF="#FTN.AEN2654">[1]</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="EMPHASIS">suid</I> flag, these binaries could not	      be invoked by a non-root user.	      <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>	      A file with the <TTCLASS="REPLACEABLE"><I>suid</I></TT> flag set shows	      an <ICLASS="EMPHASIS">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 <SPANCLASS="QUOTE">"sticky bit,"</SPAN> the	      <ICLASS="EMPHASIS">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="AEN2674"HREF="#FTN.AEN2674">[2]</A>	      If set on a directory, it restricts write permission.	      Setting the sticky bit adds a <ICLASS="EMPHASIS">t</I>	      to the permissions on the file or directory listing.	      <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>	    	      If a user does not own a directory that has the sticky	      bit set, but has write permission in that directory,	      he can only delete files in it that he owns. This keeps	      users from inadvertently overwriting or deleting each	      other's files in a publicly accessible directory, such	      as <TTCLASS="FILENAME">/tmp</TT>.	      (The owner of the directory or <ICLASS="EMPHASIS">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&nbsp;#!/bin/bash   2&nbsp;# broken-link.sh   3&nbsp;# Written by Lee bigelow &#60;ligelowbee@yahoo.com&#62;   4&nbsp;# Used with permission.   5&nbsp;   6&nbsp;#A pure shell script to find dead symlinks and output them quoted   7&nbsp;#so they can be fed to xargs and dealt with :)   8&nbsp;#eg. broken-link.sh /somedir /someotherdir|xargs rm   9&nbsp;#  10&nbsp;#This, however, is a better method:  11&nbsp;#  12&nbsp;#find "somedir" -type l -print0|\  13&nbsp;#xargs -r0 file|\  14&nbsp;#grep "broken symbolic"|  15&nbsp;#sed -e 's/^\|: *broken symbolic.*$/"/g'  16&nbsp;#  17&nbsp;#but that wouldn't be pure bash, now would it.  18&nbsp;#Caution: beware the /proc file system and any circular links!  19&nbsp;##############################################################  20&nbsp;  21&nbsp;  22&nbsp;#If no args are passed to the script set directorys to search   23&nbsp;#to current directory.  Otherwise set the directorys to search   24&nbsp;#to the agrs passed.  25&nbsp;####################  26&nbsp;[ $# -eq 0 ] &#38;&#38; directorys=`pwd` || directorys=$@  27&nbsp;  28&nbsp;#Setup the function linkchk to check the directory it is passed   29&nbsp;#for files that are links and don't exist, then print them quoted.  30&nbsp;#If one of the elements in the directory is a subdirectory then   31&nbsp;#send that send that subdirectory to the linkcheck function.  32&nbsp;##########  33&nbsp;linkchk () {  34&nbsp;    for element in $1/*; do  35&nbsp;    [ -h "$element" -a ! -e "$element" ] &#38;&#38; echo \"$element\"  36&nbsp;    [ -d "$element" ] &#38;&#38; linkchk $element  37&nbsp;    # Of course, '-h' tests for symbolic link, '-d' for directory.  38&nbsp;    done  39&nbsp;}  40&nbsp;  41&nbsp;#Send each arg that was passed to the script to the linkchk function  42&nbsp;#if it is a valid directoy.  If not, then print the error message  43&nbsp;#and usage info.  44&nbsp;################  45&nbsp;for directory in $directorys; do  46&nbsp;    if [ -d $directory ]  47&nbsp;	then linkchk $directory  48&nbsp;	else   49&nbsp;	    echo "$directory is not a directory"  50&nbsp;	    echo "Usage: $0 dir1 dir2 ..."  51&nbsp;    fi  52&nbsp;done  53&nbsp;  54&nbsp;exit 0</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.AEN2654"HREF="fto.html#AEN2654">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Be aware that <ICLASS="EMPHASIS">suid</I>		  binaries may open security holes. The		  <ICLASS="EMPHASIS">suid</I> flag has no effect on		  shell scripts.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2674"HREF="fto.html#AEN2674">[2]</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%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="tests.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="comparison-ops.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Tests</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="tests.html">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 + -