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

📄 sha-bang.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Starting Off With a Sha-Bang</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Introduction"HREF="part1.html"><LINKREL="PREVIOUS"TITLE="Why Shell Programming?"HREF="why-shell.html"><LINKREL="NEXT"TITLE="Preliminary Exercises"HREF="prelimexer.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="CHAPTER"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="why-shell.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="prelimexer.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="SHA-BANG"></A>Chapter 2. Starting Off With a Sha-Bang</H1><TABLEBORDER="0"WIDTH="100%"CELLSPACING="0"CELLPADDING="0"CLASS="EPIGRAPH"><TR><TDWIDTH="45%">&nbsp;</TD><TDWIDTH="45%"ALIGN="LEFT"VALIGN="TOP"><I><P><I>Shell programming is a 1950s juke box . . .</I></P><P><I>--Larry Wall</I></P></I></TD></TR></TABLE><P>In the simplest case, a script is nothing more than a list of system      commands stored in a file. At the very least, this saves the      effort of retyping that particular sequence of commands each time      it is invoked.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX1"></A><P><B>Example 2-1. <ICLASS="FIRSTTERM">cleanup</I>: A script to clean up the log      files in /var/log </B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Cleanup   2&nbsp;# Run as root, of course.   3&nbsp;   4&nbsp;cd /var/log   5&nbsp;cat /dev/null &#62; messages   6&nbsp;cat /dev/null &#62; wtmp   7&nbsp;echo "Logs cleaned up."</PRE></TD></TR></TABLE><HR></DIV><P>There is nothing unusual here, only a set of commands that      could just as easily be invoked one by one from the command line on      the console or in an <ICLASS="FIRSTTERM">xterm</I>. The advantages of      placing the commands in a script go beyond not having to retype them      time and again. The script becomes a <SPANCLASS="emphasis"><ICLASS="EMPHASIS">tool</I></SPAN>,      and can easily be modified or customized for a particular      application.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX1A"></A><P><B>Example 2-2. <ICLASS="FIRSTTERM">cleanup</I>: An improved clean-up      script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Proper header for a Bash script.   3&nbsp;   4&nbsp;# Cleanup, version 2   5&nbsp;   6&nbsp;# Run as root, of course.   7&nbsp;# Insert code here to print error message and exit if not root.   8&nbsp;   9&nbsp;LOG_DIR=/var/log  10&nbsp;# Variables are better than hard-coded values.  11&nbsp;cd $LOG_DIR  12&nbsp;  13&nbsp;cat /dev/null &#62; messages  14&nbsp;cat /dev/null &#62; wtmp  15&nbsp;  16&nbsp;  17&nbsp;echo "Logs cleaned up."  18&nbsp;  19&nbsp;exit # The right and proper method of "exiting" from a script.</PRE></TD></TR></TABLE><HR></DIV><P>Now that's beginning to look like a real script. But we can      go even farther . . .</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX2"></A><P><B>Example 2-3. <ICLASS="FIRSTTERM">cleanup</I>: An enhanced      and generalized version of above scripts.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Cleanup, version 3   3&nbsp;   4&nbsp;#  Warning:   5&nbsp;#  -------   6&nbsp;#  This script uses quite a number of features that will be explained   7&nbsp;#+ later on.   8&nbsp;#  By the time you've finished the first half of the book,   9&nbsp;#+ there should be nothing mysterious about it.  10&nbsp;  11&nbsp;  12&nbsp;  13&nbsp;LOG_DIR=/var/log  14&nbsp;ROOT_UID=0     # Only users with $UID 0 have root privileges.  15&nbsp;LINES=50       # Default number of lines saved.  16&nbsp;E_XCD=66       # Can't change directory?  17&nbsp;E_NOTROOT=67   # Non-root exit error.  18&nbsp;  19&nbsp;  20&nbsp;# Run as root, of course.  21&nbsp;if [ "$UID" -ne "$ROOT_UID" ]  22&nbsp;then  23&nbsp;  echo "Must be root to run this script."  24&nbsp;  exit $E_NOTROOT  25&nbsp;fi    26&nbsp;  27&nbsp;if [ -n "$1" ]  28&nbsp;# Test if command line argument present (non-empty).  29&nbsp;then  30&nbsp;  lines=$1  31&nbsp;else    32&nbsp;  lines=$LINES # Default, if not specified on command line.  33&nbsp;fi    34&nbsp;  35&nbsp;  36&nbsp;#  Stephane Chazelas suggests the following,  37&nbsp;#+ as a better way of checking command line arguments,  38&nbsp;#+ but this is still a bit advanced for this stage of the tutorial.  39&nbsp;#  40&nbsp;#    E_WRONGARGS=65  # Non-numerical argument (bad arg format)  41&nbsp;#  42&nbsp;#    case "$1" in  43&nbsp;#    ""      ) lines=50;;  44&nbsp;#    *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS;;  45&nbsp;#    *       ) lines=$1;;  46&nbsp;#    esac  47&nbsp;#  48&nbsp;#* Skip ahead to "Loops" chapter to decipher all this.  49&nbsp;  50&nbsp;  51&nbsp;cd $LOG_DIR  52&nbsp;  53&nbsp;if [ `pwd` != "$LOG_DIR" ]  # or   if [ "$PWD" != "$LOG_DIR" ]  54&nbsp;                            # Not in /var/log?  55&nbsp;then  56&nbsp;  echo "Can't change to $LOG_DIR."  57&nbsp;  exit $E_XCD  58&nbsp;fi  # Doublecheck if in right directory, before messing with log file.  59&nbsp;  60&nbsp;# far more efficient is:  61&nbsp;#  62&nbsp;# cd /var/log || {  63&nbsp;#   echo "Cannot change to necessary directory." &#62;&#38;2  64&nbsp;#   exit $E_XCD;  65&nbsp;# }  66&nbsp;  67&nbsp;  68&nbsp;  69&nbsp;  70&nbsp;tail -n $lines messages &#62; mesg.temp # Saves last section of message log file.  71&nbsp;mv mesg.temp messages               # Becomes new log directory.  72&nbsp;  73&nbsp;  74&nbsp;# cat /dev/null &#62; messages  75&nbsp;#* No longer needed, as the above method is safer.  76&nbsp;  77&nbsp;cat /dev/null &#62; wtmp  #  ': &#62; wtmp' and '&#62; wtmp'  have the same effect.  78&nbsp;echo "Logs cleaned up."  79&nbsp;  80&nbsp;exit 0  81&nbsp;#  A zero return value from the script upon exit  82&nbsp;#+ indicates success to the shell.</PRE></TD></TR></TABLE><HR></DIV><P>Since you may not wish to wipe out the entire system log,      this version of the script keeps the last section of the message      log intact. You will constantly discover ways of fine-tuning      previously written scripts for increased effectiveness.</P><P><ANAME="SHABANGREF"></A></P><P>The      <ICLASS="FIRSTTERM"> sha-bang</I>      (<SPANCLASS="TOKEN">	 #!</SPAN>)	  <ANAME="AEN192"HREF="#FTN.AEN192">[1]</A>	at the head of a script      tells your system that this file is a set of commands to be fed      to the command interpreter indicated.  <ANAME="MAGNUMREF"></A>The      <SPANCLASS="TOKEN">#!</SPAN> is actually a two-byte        <ANAME="AEN202"HREF="#FTN.AEN202">[2]</A>		<ICLASS="FIRSTTERM">magic number</I>, a special marker that	designates a file type, or in this case an executable shell	script (type <TTCLASS="USERINPUT"><B>man magic</B></TT> for more	details on this fascinating topic). Immediately following	the <ICLASS="FIRSTTERM">sha-bang</I> is a <ICLASS="FIRSTTERM">path	name</I>. This is the path to the program that interprets	the commands in the script, whether it be a shell, a programming	language, or a utility. This command interpreter then executes	the commands in the script, starting at the top (line following	the <ICLASS="FIRSTTERM">sha-bang</I> line), ignoring comments.	  <ANAME="AEN214"HREF="#FTN.AEN214">[3]</A>	</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/sh   2&nbsp;#!/bin/bash   3&nbsp;#!/usr/bin/perl   4&nbsp;#!/usr/bin/tcl   5&nbsp;#!/bin/sed -f   6&nbsp;#!/usr/awk -f</PRE></TD></TR></TABLE></P><P>Each of the above script header lines calls a different command      interpreter, be it <TTCLASS="FILENAME">/bin/sh</TT>, the default shell      (<BCLASS="COMMAND">bash</B> in a Linux system) or otherwise.        <ANAME="AEN230"HREF="#FTN.AEN230">[4]</A>      Using <TTCLASS="USERINPUT"><B>#!/bin/sh</B></TT>, the default Bourne shell      in most commercial variants of UNIX, makes the script <AHREF="portabilityissues.html">portable</A> to non-Linux machines,      though you <AHREF="gotchas.html#BINSH">sacrifice Bash-specific      features</A>.  The script will, however, conform to the      <SPANCLASS="ACRONYM">POSIX</SPAN>	 <ANAME="AEN244"HREF="#FTN.AEN244">[5]</A>      <BCLASS="COMMAND">sh</B> standard.</P><P>Note that the path given at the <SPANCLASS="QUOTE">"sha-bang"</SPAN> must      be correct, otherwise an error message -- usually <SPANCLASS="QUOTE">"Command      not found."</SPAN> -- will be the only result of running the      script.        <ANAME="AEN257"HREF="#FTN.AEN257">[6]</A>            </P><P><SPANCLASS="TOKEN">#!</SPAN> can be omitted if the script consists only      of a set of generic system commands, using no internal      shell directives.  The second example, above, requires the      initial <SPANCLASS="TOKEN">#!</SPAN>, since the variable assignment line,      <TTCLASS="USERINPUT"><B>lines=50</B></TT>, uses a shell-specific construct.	<ANAME="AEN267"HREF="#FTN.AEN267">[7]</A>      Note again that <TTCLASS="USERINPUT"><B>#!/bin/sh</B></TT> invokes the default      shell interpreter, which defaults to <TTCLASS="FILENAME">/bin/bash</TT>      on a Linux machine.</P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>This tutorial encourages a modular approach	to constructing a script. Make note of and collect	<SPANCLASS="QUOTE">"boilerplate"</SPAN> code snippets that might be useful	in future scripts. Eventually you can build quite an extensive	library of nifty routines. As an example, the following script	prolog tests whether the script has been invoked with the correct	number of parameters.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;E_WRONG_ARGS=65   2&nbsp;script_parameters="-a -h -m -z"   3&nbsp;#                  -a = all, -h = help, etc.   4&nbsp;   5&nbsp;if [ $# -ne $Number_of_expected_args ]   6&nbsp;then   7&nbsp;  echo "Usage: `basename $0` $script_parameters"   8&nbsp;  # `basename $0` is the script's filename.   9&nbsp;  exit $E_WRONG_ARGS  10&nbsp;fi</PRE></TD></TR></TABLE>      </P><P>Many times, you will write a script that carries out one        particular task. The first script in this chapter is an        example of this. Later, it might occur to you to generalize        the script to do other, similar tasks. Replacing the literal        (<SPANCLASS="QUOTE">"hard-wired"</SPAN>) constants by variables is a step in        that direction, as is replacing repetitive code blocks by <AHREF="functions.html#FUNCTIONREF">functions</A>.</P></TD></TR></TABLE></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="INVOKING"></A>2.1. Invoking the script</H1><P>Having written the script, you can invoke it by <TTCLASS="USERINPUT"><B>sh	scriptname</B></TT>,	  <ANAME="AEN288"HREF="#FTN.AEN288">[8]</A>	or alternatively <TTCLASS="USERINPUT"><B>bash scriptname</B></TT>. (Not	recommended is using <TTCLASS="USERINPUT"><B

⌨️ 快捷键说明

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