📄 sha-bang.html
字号:
<!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%"> </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 # Cleanup 2 # Run as root, of course. 3 4 cd /var/log 5 cat /dev/null > messages 6 cat /dev/null > wtmp 7 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 #!/bin/bash 2 # Proper header for a Bash script. 3 4 # Cleanup, version 2 5 6 # Run as root, of course. 7 # Insert code here to print error message and exit if not root. 8 9 LOG_DIR=/var/log 10 # Variables are better than hard-coded values. 11 cd $LOG_DIR 12 13 cat /dev/null > messages 14 cat /dev/null > wtmp 15 16 17 echo "Logs cleaned up." 18 19 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 #!/bin/bash 2 # Cleanup, version 3 3 4 # Warning: 5 # ------- 6 # This script uses quite a number of features that will be explained 7 #+ later on. 8 # By the time you've finished the first half of the book, 9 #+ there should be nothing mysterious about it. 10 11 12 13 LOG_DIR=/var/log 14 ROOT_UID=0 # Only users with $UID 0 have root privileges. 15 LINES=50 # Default number of lines saved. 16 E_XCD=66 # Can't change directory? 17 E_NOTROOT=67 # Non-root exit error. 18 19 20 # Run as root, of course. 21 if [ "$UID" -ne "$ROOT_UID" ] 22 then 23 echo "Must be root to run this script." 24 exit $E_NOTROOT 25 fi 26 27 if [ -n "$1" ] 28 # Test if command line argument present (non-empty). 29 then 30 lines=$1 31 else 32 lines=$LINES # Default, if not specified on command line. 33 fi 34 35 36 # Stephane Chazelas suggests the following, 37 #+ as a better way of checking command line arguments, 38 #+ but this is still a bit advanced for this stage of the tutorial. 39 # 40 # E_WRONGARGS=65 # Non-numerical argument (bad arg format) 41 # 42 # case "$1" in 43 # "" ) lines=50;; 44 # *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS;; 45 # * ) lines=$1;; 46 # esac 47 # 48 #* Skip ahead to "Loops" chapter to decipher all this. 49 50 51 cd $LOG_DIR 52 53 if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ] 54 # Not in /var/log? 55 then 56 echo "Can't change to $LOG_DIR." 57 exit $E_XCD 58 fi # Doublecheck if in right directory, before messing with log file. 59 60 # far more efficient is: 61 # 62 # cd /var/log || { 63 # echo "Cannot change to necessary directory." >&2 64 # exit $E_XCD; 65 # } 66 67 68 69 70 tail -n $lines messages > mesg.temp # Saves last section of message log file. 71 mv mesg.temp messages # Becomes new log directory. 72 73 74 # cat /dev/null > messages 75 #* No longer needed, as the above method is safer. 76 77 cat /dev/null > wtmp # ': > wtmp' and '> wtmp' have the same effect. 78 echo "Logs cleaned up." 79 80 exit 0 81 # A zero return value from the script upon exit 82 #+ 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 #!/bin/sh 2 #!/bin/bash 3 #!/usr/bin/perl 4 #!/usr/bin/tcl 5 #!/bin/sed -f 6 #!/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 E_WRONG_ARGS=65 2 script_parameters="-a -h -m -z" 3 # -a = all, -h = help, etc. 4 5 if [ $# -ne $Number_of_expected_args ] 6 then 7 echo "Usage: `basename $0` $script_parameters" 8 # `basename $0` is the script's filename. 9 exit $E_WRONG_ARGS 10 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 + -