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

📄 sha-bang.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
>sh &#60;scriptname</B></TT>,	since this effectively disables reading from	<AHREF="ioredirintro.html#STDINOUTDEF"><TTCLASS="FILENAME">stdin</TT></A>	within the script.) Much more convenient is to make	the script itself directly executable with a <AHREF="external.html#CHMODREF">chmod</A>.	<DIVCLASS="VARIABLELIST"><DL><DT>Either:</DT><DD><P><TTCLASS="USERINPUT"><B>chmod 555 scriptname</B></TT> (gives	      everyone read/execute permission)	        <ANAME="AEN303"HREF="#FTN.AEN303">[9]</A>	      </P></DD><DT>or</DT><DD><P><TTCLASS="USERINPUT"><B>chmod +rx scriptname</B></TT> (gives	      everyone read/execute permission)</P><P><TTCLASS="USERINPUT"><B>chmod	      u+rx scriptname</B></TT> (gives only the		script owner read/execute permission)</P></DD></DL></DIV>      </P><P>Having made the script executable, you may now test it by	<TTCLASS="USERINPUT"><B>./scriptname</B></TT>.		  <ANAME="AEN315"HREF="#FTN.AEN315">[10]</A>	If it begins with a <SPANCLASS="QUOTE">"sha-bang"</SPAN> line, invoking the	script calls the correct command interpreter to run it.</P><P>As a final step, after testing and debugging,	you would likely want to move it to <TTCLASS="FILENAME">/usr/local/bin</TT> (as	<ICLASS="FIRSTTERM">root</I>, of course), to make the script	available to yourself and all other users as a systemwide	executable.  The script could then be invoked by simply typing	<BCLASS="COMMAND">scriptname</B> <BCLASS="KEYCAP">[ENTER]</B> from the	command line.</P></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN192"HREF="sha-bang.html#AEN192">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Also seen in the literature as	  <ICLASS="FIRSTTERM">she-bang</I> or <ICLASS="FIRSTTERM">sh-bang</I>.	  This derives from the concatenation of the tokens	  <ICLASS="FIRSTTERM">sharp</I> (<SPANCLASS="TOKEN">#</SPAN>) and	  <ICLASS="FIRSTTERM">bang</I> (<SPANCLASS="TOKEN">!</SPAN>).</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN202"HREF="sha-bang.html#AEN202">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Some flavors of UNIX (those based on 4.2 BSD)          allegedly take a four-byte magic number, requiring          a blank after the <SPANCLASS="TOKEN">!</SPAN> --	  <TTCLASS="USERINPUT"><B>#! /bin/sh</B></TT>. <AHREF="http://www.in-ulm.de/~mascheck/various/shebang/#details"TARGET="_top">	  According to Sven Mascheck</A> this is probably a myth.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN214"HREF="sha-bang.html#AEN214">[3]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The <SPANCLASS="TOKEN">#!</SPAN> line in a shell script	      will be the first thing the command interpreter	      (<BCLASS="COMMAND">sh</B> or <BCLASS="COMMAND">bash</B>)	      sees. Since this line begins with a <SPANCLASS="TOKEN">#</SPAN>,	      it will be correctly interpreted as a comment when the	      command interpreter finally executes the script. The	      line has already served its purpose - calling the command	      interpreter.</P><P>If, in fact, the script includes an	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">extra</I></SPAN> <SPANCLASS="TOKEN">#!</SPAN> line, then	      <BCLASS="COMMAND">bash</B> will interpret it as a comment.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;echo "Part 1 of script."   4&nbsp;a=1   5&nbsp;   6&nbsp;#!/bin/bash   7&nbsp;# This does *not* launch a new script.   8&nbsp;   9&nbsp;echo "Part 2 of script."  10&nbsp;echo $a  # Value of $a stays at 1.</PRE></TD></TR></TABLE></P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN230"HREF="sha-bang.html#AEN230">[4]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>This allows some cute tricks.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/rm   2&nbsp;# Self-deleting script.   3&nbsp;   4&nbsp;# Nothing much seems to happen when you run this... except that the file disappears.   5&nbsp;   6&nbsp;WHATEVER=65   7&nbsp;   8&nbsp;echo "This line will never print (betcha!)."   9&nbsp;  10&nbsp;exit $WHATEVER  # Doesn't matter. The script will not exit here.  11&nbsp;                # Try an echo $? after script termination.  12&nbsp;                # You'll get a 0, not a 65.</PRE></TD></TR></TABLE></P><P>Also, try starting a <TTCLASS="FILENAME">README</TT> file with a        <TTCLASS="USERINPUT"><B>#!/bin/more</B></TT>, and making it executable.        The result is a self-listing documentation file. (A <AHREF="here-docs.html#HEREDOCREF">here document</A> using	<AHREF="external.html#CATREF">cat</A> is possibly a better alternative	-- see <AHREF="here-docs.html#EX71">Example 18-3</A>).</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN244"HREF="sha-bang.html#AEN244">[5]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="POSIX2REF"></A><SPANCLASS="strong"><BCLASS="EMPHASIS">P</B></SPAN>ortable	 <SPANCLASS="strong"><BCLASS="EMPHASIS">O</B></SPAN>perating	 <SPANCLASS="strong"><BCLASS="EMPHASIS">S</B></SPAN>ystem <SPANCLASS="bold"><BCLASS="EMPHASIS">I</B></SPAN>nterface, an attempt to	 standardize UNI<SPANCLASS="strong"><BCLASS="EMPHASIS">X</B></SPAN>-like	 OSes. The POSIX specifications are listed on the <AHREF="http://www.opengroup.org/onlinepubs/007904975/toc.htm"TARGET="_top">Open	 Group site</A>.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN257"HREF="sha-bang.html#AEN257">[6]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>To avoid this possibility, a script may begin	  with a <AHREF="system.html#ENVV2REF">#!/bin/env bash</A>	  <ICLASS="FIRSTTERM">sha-bang</I> line. This may be	  useful on UNIX machines where <ICLASS="FIRSTTERM">bash</I>	  is not located in <TTCLASS="FILENAME">/bin</TT></P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN267"HREF="sha-bang.html#AEN267">[7]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>If <ICLASS="FIRSTTERM">Bash</I> is your default	shell, then the <SPANCLASS="TOKEN">#!</SPAN> isn't necessary at the	beginning of a script.	However, if launching a script from	a different shell, such as <ICLASS="FIRSTTERM">tcsh</I>,	then you <SPANCLASS="emphasis"><ICLASS="EMPHASIS">will</I></SPAN> need the	<SPANCLASS="TOKEN">#!</SPAN>.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN288"HREF="sha-bang.html#AEN288">[8]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Caution: invoking a <ICLASS="FIRSTTERM">Bash</I>	  script by <TTCLASS="USERINPUT"><B>sh scriptname</B></TT> turns off	  Bash-specific extensions, and the script may therefore fail	  to execute.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN303"HREF="sha-bang.html#AEN303">[9]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>A script needs <ICLASS="FIRSTTERM">read</I>, as		well as execute permission for it to run, since the shell		needs to be able to read it.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN315"HREF="sha-bang.html#AEN315">[10]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Why not simply invoke the script with	  <TTCLASS="USERINPUT"><B>scriptname</B></TT>? If the directory you	  are in (<AHREF="variables2.html#PWDREF">$PWD</A>) is where	  <TTCLASS="FILENAME">scriptname</TT> is located, why doesn't	  this work? This fails because, for security reasons, the	  current directory (<TTCLASS="FILENAME">./</TT>)	  is not by default included in a user's <AHREF="variables2.html#PATHREF">$PATH</A>. It is therefore necessary to	  explicitly invoke the script in the current directory with	  a <TTCLASS="USERINPUT"><B>./scriptname</B></TT>.</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="why-shell.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="prelimexer.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Why Shell Programming?</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part1.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Preliminary Exercises</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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