| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Starting Off With a Sha-Bang</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="UP" |
| 14 | TITLE="Introduction" |
| 15 | HREF="part1.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Shell Programming!" |
| 18 | HREF="why-shell.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Preliminary Exercises" |
| 21 | HREF="prelimexer.html"><META |
| 22 | HTTP-EQUIV="Content-Style-Type" |
| 23 | CONTENT="text/css"><LINK |
| 24 | REL="stylesheet" |
| 25 | HREF="common/kde-common.css" |
| 26 | TYPE="text/css"><META |
| 27 | HTTP-EQUIV="Content-Type" |
| 28 | CONTENT="text/html; charset=iso-8859-1"><META |
| 29 | HTTP-EQUIV="Content-Language" |
| 30 | CONTENT="en"><LINK |
| 31 | REL="stylesheet" |
| 32 | HREF="common/kde-localised.css" |
| 33 | TYPE="text/css" |
| 34 | TITLE="KDE-English"><LINK |
| 35 | REL="stylesheet" |
| 36 | HREF="common/kde-default.css" |
| 37 | TYPE="text/css" |
| 38 | TITLE="KDE-Default"></HEAD |
| 39 | ><BODY |
| 40 | CLASS="CHAPTER" |
| 41 | BGCOLOR="#FFFFFF" |
| 42 | TEXT="#000000" |
| 43 | LINK="#AA0000" |
| 44 | VLINK="#AA0055" |
| 45 | ALINK="#AA0000" |
| 46 | STYLE="font-family: sans-serif;" |
| 47 | ><DIV |
| 48 | CLASS="NAVHEADER" |
| 49 | ><TABLE |
| 50 | SUMMARY="Header navigation table" |
| 51 | WIDTH="100%" |
| 52 | BORDER="0" |
| 53 | CELLPADDING="0" |
| 54 | CELLSPACING="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | COLSPAN="3" |
| 58 | ALIGN="center" |
| 59 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 60 | ></TR |
| 61 | ><TR |
| 62 | ><TD |
| 63 | WIDTH="10%" |
| 64 | ALIGN="left" |
| 65 | VALIGN="bottom" |
| 66 | ><A |
| 67 | HREF="why-shell.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | ></TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="prelimexer.html" |
| 82 | ACCESSKEY="N" |
| 83 | >Next</A |
| 84 | ></TD |
| 85 | ></TR |
| 86 | ></TABLE |
| 87 | ><HR |
| 88 | ALIGN="LEFT" |
| 89 | WIDTH="100%"></DIV |
| 90 | ><DIV |
| 91 | CLASS="CHAPTER" |
| 92 | ><H1 |
| 93 | ><A |
| 94 | NAME="SHA-BANG" |
| 95 | ></A |
| 96 | >Chapter 2. Starting Off With a Sha-Bang</H1 |
| 97 | ><TABLE |
| 98 | BORDER="0" |
| 99 | WIDTH="100%" |
| 100 | CELLSPACING="0" |
| 101 | CELLPADDING="0" |
| 102 | CLASS="EPIGRAPH" |
| 103 | ><TR |
| 104 | ><TD |
| 105 | WIDTH="45%" |
| 106 | > </TD |
| 107 | ><TD |
| 108 | WIDTH="45%" |
| 109 | ALIGN="LEFT" |
| 110 | VALIGN="TOP" |
| 111 | ><I |
| 112 | ><P |
| 113 | ><I |
| 114 | >Shell programming is a 1950s juke box . . .</I |
| 115 | ></P |
| 116 | ><P |
| 117 | ><I |
| 118 | >--Larry Wall</I |
| 119 | ></P |
| 120 | ></I |
| 121 | ></TD |
| 122 | ></TR |
| 123 | ></TABLE |
| 124 | ><P |
| 125 | >In the simplest case, a script is nothing more than a list |
| 126 | of system commands stored in a file. At the very least, this saves |
| 127 | the effort of retyping that particular sequence of commands each |
| 128 | time it is invoked.</P |
| 129 | ><DIV |
| 130 | CLASS="EXAMPLE" |
| 131 | ><HR><A |
| 132 | NAME="EX1" |
| 133 | ></A |
| 134 | ><P |
| 135 | ><B |
| 136 | >Example 2-1. <I |
| 137 | CLASS="FIRSTTERM" |
| 138 | >cleanup</I |
| 139 | >: A script to clean up log |
| 140 | files in /var/log </B |
| 141 | ></P |
| 142 | ><TABLE |
| 143 | BORDER="0" |
| 144 | BGCOLOR="#E0E0E0" |
| 145 | WIDTH="100%" |
| 146 | ><TR |
| 147 | ><TD |
| 148 | ><PRE |
| 149 | CLASS="PROGRAMLISTING" |
| 150 | > 1 # Cleanup |
| 151 | 2 # Run as root, of course. |
| 152 | 3 |
| 153 | 4 cd /var/log |
| 154 | 5 cat /dev/null > messages |
| 155 | 6 cat /dev/null > wtmp |
| 156 | 7 echo "Log files cleaned up."</PRE |
| 157 | ></TD |
| 158 | ></TR |
| 159 | ></TABLE |
| 160 | ><HR></DIV |
| 161 | ><P |
| 162 | >There is nothing unusual here, only a set of commands that |
| 163 | could just as easily have been invoked one by one from the |
| 164 | command-line on the console or in a terminal window. |
| 165 | The advantages of placing the commands in a script go far beyond |
| 166 | not having to retype them time and again. The script becomes a |
| 167 | <I |
| 168 | CLASS="FIRSTTERM" |
| 169 | >program</I |
| 170 | > -- a <SPAN |
| 171 | CLASS="emphasis" |
| 172 | ><I |
| 173 | CLASS="EMPHASIS" |
| 174 | >tool</I |
| 175 | ></SPAN |
| 176 | > -- |
| 177 | and it can easily be modified or customized for a particular |
| 178 | application.</P |
| 179 | ><DIV |
| 180 | CLASS="EXAMPLE" |
| 181 | ><HR><A |
| 182 | NAME="EX1A" |
| 183 | ></A |
| 184 | ><P |
| 185 | ><B |
| 186 | >Example 2-2. <I |
| 187 | CLASS="FIRSTTERM" |
| 188 | >cleanup</I |
| 189 | >: An improved clean-up |
| 190 | script</B |
| 191 | ></P |
| 192 | ><TABLE |
| 193 | BORDER="0" |
| 194 | BGCOLOR="#E0E0E0" |
| 195 | WIDTH="100%" |
| 196 | ><TR |
| 197 | ><TD |
| 198 | ><PRE |
| 199 | CLASS="PROGRAMLISTING" |
| 200 | > 1 #!/bin/bash |
| 201 | 2 # Proper header for a Bash script. |
| 202 | 3 |
| 203 | 4 # Cleanup, version 2 |
| 204 | 5 |
| 205 | 6 # Run as root, of course. |
| 206 | 7 # Insert code here to print error message and exit if not root. |
| 207 | 8 |
| 208 | 9 LOG_DIR=/var/log |
| 209 | 10 # Variables are better than hard-coded values. |
| 210 | 11 cd $LOG_DIR |
| 211 | 12 |
| 212 | 13 cat /dev/null > messages |
| 213 | 14 cat /dev/null > wtmp |
| 214 | 15 |
| 215 | 16 |
| 216 | 17 echo "Logs cleaned up." |
| 217 | 18 |
| 218 | 19 exit # The right and proper method of "exiting" from a script. |
| 219 | 20 # A bare "exit" (no parameter) returns the exit status |
| 220 | 21 #+ of the preceding command. </PRE |
| 221 | ></TD |
| 222 | ></TR |
| 223 | ></TABLE |
| 224 | ><HR></DIV |
| 225 | ><P |
| 226 | >Now <SPAN |
| 227 | CLASS="emphasis" |
| 228 | ><I |
| 229 | CLASS="EMPHASIS" |
| 230 | >that's</I |
| 231 | ></SPAN |
| 232 | > beginning to look like a real |
| 233 | script. But we can go even farther . . .</P |
| 234 | ><DIV |
| 235 | CLASS="EXAMPLE" |
| 236 | ><HR><A |
| 237 | NAME="EX2" |
| 238 | ></A |
| 239 | ><P |
| 240 | ><B |
| 241 | >Example 2-3. <I |
| 242 | CLASS="FIRSTTERM" |
| 243 | >cleanup</I |
| 244 | >: An enhanced |
| 245 | and generalized version of above scripts.</B |
| 246 | ></P |
| 247 | ><TABLE |
| 248 | BORDER="0" |
| 249 | BGCOLOR="#E0E0E0" |
| 250 | WIDTH="100%" |
| 251 | ><TR |
| 252 | ><TD |
| 253 | ><PRE |
| 254 | CLASS="PROGRAMLISTING" |
| 255 | > 1 #!/bin/bash |
| 256 | 2 # Cleanup, version 3 |
| 257 | 3 |
| 258 | 4 # Warning: |
| 259 | 5 # ------- |
| 260 | 6 # This script uses quite a number of features that will be explained |
| 261 | 7 #+ later on. |
| 262 | 8 # By the time you've finished the first half of the book, |
| 263 | 9 #+ there should be nothing mysterious about it. |
| 264 | 10 |
| 265 | 11 |
| 266 | 12 |
| 267 | 13 LOG_DIR=/var/log |
| 268 | 14 ROOT_UID=0 # Only users with $UID 0 have root privileges. |
| 269 | 15 LINES=50 # Default number of lines saved. |
| 270 | 16 E_XCD=86 # Can't change directory? |
| 271 | 17 E_NOTROOT=87 # Non-root exit error. |
| 272 | 18 |
| 273 | 19 |
| 274 | 20 # Run as root, of course. |
| 275 | 21 if [ "$UID" -ne "$ROOT_UID" ] |
| 276 | 22 then |
| 277 | 23 echo "Must be root to run this script." |
| 278 | 24 exit $E_NOTROOT |
| 279 | 25 fi |
| 280 | 26 |
| 281 | 27 if [ -n "$1" ] |
| 282 | 28 # Test whether command-line argument is present (non-empty). |
| 283 | 29 then |
| 284 | 30 lines=$1 |
| 285 | 31 else |
| 286 | 32 lines=$LINES # Default, if not specified on command-line. |
| 287 | 33 fi |
| 288 | 34 |
| 289 | 35 |
| 290 | 36 # Stephane Chazelas suggests the following, |
| 291 | 37 #+ as a better way of checking command-line arguments, |
| 292 | 38 #+ but this is still a bit advanced for this stage of the tutorial. |
| 293 | 39 # |
| 294 | 40 # E_WRONGARGS=85 # Non-numerical argument (bad argument format). |
| 295 | 41 # |
| 296 | 42 # case "$1" in |
| 297 | 43 # "" ) lines=50;; |
| 298 | 44 # *[!0-9]*) echo "Usage: `basename $0` lines-to-cleanup"; |
| 299 | 45 # exit $E_WRONGARGS;; |
| 300 | 46 # * ) lines=$1;; |
| 301 | 47 # esac |
| 302 | 48 # |
| 303 | 49 #* Skip ahead to "Loops" chapter to decipher all this. |
| 304 | 50 |
| 305 | 51 |
| 306 | 52 cd $LOG_DIR |
| 307 | 53 |
| 308 | 54 if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ] |
| 309 | 55 # Not in /var/log? |
| 310 | 56 then |
| 311 | 57 echo "Can't change to $LOG_DIR." |
| 312 | 58 exit $E_XCD |
| 313 | 59 fi # Doublecheck if in right directory before messing with log file. |
| 314 | 60 |
| 315 | 61 # Far more efficient is: |
| 316 | 62 # |
| 317 | 63 # cd /var/log || { |
| 318 | 64 # echo "Cannot change to necessary directory." >&2 |
| 319 | 65 # exit $E_XCD; |
| 320 | 66 # } |
| 321 | 67 |
| 322 | 68 |
| 323 | 69 |
| 324 | 70 |
| 325 | 71 tail -n $lines messages > mesg.temp # Save last section of message log file. |
| 326 | 72 mv mesg.temp messages # Rename it as system log file. |
| 327 | 73 |
| 328 | 74 |
| 329 | 75 # cat /dev/null > messages |
| 330 | 76 #* No longer needed, as the above method is safer. |
| 331 | 77 |
| 332 | 78 cat /dev/null > wtmp # ': > wtmp' and '> wtmp' have the same effect. |
| 333 | 79 echo "Log files cleaned up." |
| 334 | 80 # Note that there are other log files in /var/log not affected |
| 335 | 81 #+ by this script. |
| 336 | 82 |
| 337 | 83 exit 0 |
| 338 | 84 # A zero return value from the script upon exit indicates success |
| 339 | 85 #+ to the shell.</PRE |
| 340 | ></TD |
| 341 | ></TR |
| 342 | ></TABLE |
| 343 | ><HR></DIV |
| 344 | ><P |
| 345 | >Since you may not wish to wipe out the entire system log, |
| 346 | this version of the script keeps the last section of the message |
| 347 | log intact. You will constantly discover ways of fine-tuning |
| 348 | previously written scripts for increased effectiveness.</P |
| 349 | ><P |
| 350 | ><A |
| 351 | NAME="SHABANGREF" |
| 352 | ></A |
| 353 | >* * *</P |
| 354 | ><P |
| 355 | ><A |
| 356 | NAME="MAGNUMREF" |
| 357 | ></A |
| 358 | >The |
| 359 | <I |
| 360 | CLASS="FIRSTTERM" |
| 361 | > sha-bang</I |
| 362 | > |
| 363 | (<SPAN |
| 364 | CLASS="TOKEN" |
| 365 | > #!</SPAN |
| 366 | >) |
| 367 | |
| 368 | <A |
| 369 | NAME="AEN205" |
| 370 | HREF="#FTN.AEN205" |
| 371 | >[1]</A |
| 372 | > |
| 373 | |
| 374 | at the head of a script tells your system that this file is a set |
| 375 | of commands to be fed to the command interpreter indicated. The |
| 376 | <SPAN |
| 377 | CLASS="TOKEN" |
| 378 | >#!</SPAN |
| 379 | > is actually a two-byte |
| 380 | |
| 381 | <A |
| 382 | NAME="AEN214" |
| 383 | HREF="#FTN.AEN214" |
| 384 | >[2]</A |
| 385 | > |
| 386 | |
| 387 | |
| 388 | <I |
| 389 | CLASS="FIRSTTERM" |
| 390 | >magic number</I |
| 391 | >, a special marker that |
| 392 | designates a file type, or in this case an executable shell |
| 393 | script (type <TT |
| 394 | CLASS="USERINPUT" |
| 395 | ><B |
| 396 | >man magic</B |
| 397 | ></TT |
| 398 | > for more |
| 399 | details on this fascinating topic). Immediately following |
| 400 | the <I |
| 401 | CLASS="FIRSTTERM" |
| 402 | >sha-bang</I |
| 403 | > is a <I |
| 404 | CLASS="FIRSTTERM" |
| 405 | >path |
| 406 | name</I |
| 407 | >. This is the path to the program that interprets |
| 408 | the commands in the script, whether it be a shell, a programming |
| 409 | language, or a utility. This command interpreter then executes |
| 410 | the commands in the script, starting at the top (the line |
| 411 | following the <I |
| 412 | CLASS="FIRSTTERM" |
| 413 | >sha-bang</I |
| 414 | > line), and ignoring |
| 415 | comments. |
| 416 | |
| 417 | <A |
| 418 | NAME="AEN226" |
| 419 | HREF="#FTN.AEN226" |
| 420 | >[3]</A |
| 421 | > |
| 422 | |
| 423 | </P |
| 424 | ><P |
| 425 | ><TABLE |
| 426 | BORDER="0" |
| 427 | BGCOLOR="#E0E0E0" |
| 428 | WIDTH="100%" |
| 429 | ><TR |
| 430 | ><TD |
| 431 | ><PRE |
| 432 | CLASS="PROGRAMLISTING" |
| 433 | > 1 #!/bin/sh |
| 434 | 2 #!/bin/bash |
| 435 | 3 #!/usr/bin/perl |
| 436 | 4 #!/usr/bin/tcl |
| 437 | 5 #!/bin/sed -f |
| 438 | 6 #!/bin/awk -f</PRE |
| 439 | ></TD |
| 440 | ></TR |
| 441 | ></TABLE |
| 442 | ></P |
| 443 | ><P |
| 444 | >Each of the above script header lines calls a different command |
| 445 | interpreter, be it <TT |
| 446 | CLASS="FILENAME" |
| 447 | >/bin/sh</TT |
| 448 | >, the default shell |
| 449 | (<B |
| 450 | CLASS="COMMAND" |
| 451 | >bash</B |
| 452 | > in a Linux system) or otherwise. |
| 453 | |
| 454 | <A |
| 455 | NAME="AEN242" |
| 456 | HREF="#FTN.AEN242" |
| 457 | >[4]</A |
| 458 | > |
| 459 | |
| 460 | Using <TT |
| 461 | CLASS="USERINPUT" |
| 462 | ><B |
| 463 | >#!/bin/sh</B |
| 464 | ></TT |
| 465 | >, the default Bourne shell |
| 466 | in most commercial variants of UNIX, makes the script <A |
| 467 | HREF="portabilityissues.html" |
| 468 | >portable</A |
| 469 | > to non-Linux machines, |
| 470 | though you <A |
| 471 | HREF="gotchas.html#BINSH" |
| 472 | >sacrifice Bash-specific |
| 473 | features</A |
| 474 | >. The script will, however, conform to the |
| 475 | <SPAN |
| 476 | CLASS="ACRONYM" |
| 477 | >POSIX</SPAN |
| 478 | > |
| 479 | |
| 480 | <A |
| 481 | NAME="AEN256" |
| 482 | HREF="#FTN.AEN256" |
| 483 | >[5]</A |
| 484 | > |
| 485 | |
| 486 | <B |
| 487 | CLASS="COMMAND" |
| 488 | >sh</B |
| 489 | > standard.</P |
| 490 | ><P |
| 491 | >Note that the path given at the <SPAN |
| 492 | CLASS="QUOTE" |
| 493 | >"sha-bang"</SPAN |
| 494 | > must |
| 495 | be correct, otherwise an error message -- usually <SPAN |
| 496 | CLASS="QUOTE" |
| 497 | >"Command |
| 498 | not found."</SPAN |
| 499 | > -- will be the only result of running the |
| 500 | script. |
| 501 | <A |
| 502 | NAME="AEN269" |
| 503 | HREF="#FTN.AEN269" |
| 504 | >[6]</A |
| 505 | > |
| 506 | |
| 507 | </P |
| 508 | ><P |
| 509 | ><SPAN |
| 510 | CLASS="TOKEN" |
| 511 | >#!</SPAN |
| 512 | > can be omitted if the script consists only |
| 513 | of a set of generic system commands, using no internal |
| 514 | shell directives. The second example, above, requires the |
| 515 | initial <SPAN |
| 516 | CLASS="TOKEN" |
| 517 | >#!</SPAN |
| 518 | >, since the variable assignment line, |
| 519 | <TT |
| 520 | CLASS="USERINPUT" |
| 521 | ><B |
| 522 | >lines=50</B |
| 523 | ></TT |
| 524 | >, uses a shell-specific construct. |
| 525 | <A |
| 526 | NAME="AEN279" |
| 527 | HREF="#FTN.AEN279" |
| 528 | >[7]</A |
| 529 | > |
| 530 | Note again that <TT |
| 531 | CLASS="USERINPUT" |
| 532 | ><B |
| 533 | >#!/bin/sh</B |
| 534 | ></TT |
| 535 | > invokes the default |
| 536 | shell interpreter, which defaults to <TT |
| 537 | CLASS="FILENAME" |
| 538 | >/bin/bash</TT |
| 539 | > |
| 540 | on a Linux machine.</P |
| 541 | ><DIV |
| 542 | CLASS="TIP" |
| 543 | ><TABLE |
| 544 | CLASS="TIP" |
| 545 | WIDTH="100%" |
| 546 | BORDER="0" |
| 547 | ><TR |
| 548 | ><TD |
| 549 | WIDTH="25" |
| 550 | ALIGN="CENTER" |
| 551 | VALIGN="TOP" |
| 552 | ><IMG |
| 553 | SRC="common/tip.png" |
| 554 | HSPACE="5" |
| 555 | ALT="Tip"></TD |
| 556 | ><TD |
| 557 | ALIGN="LEFT" |
| 558 | VALIGN="TOP" |
| 559 | ><P |
| 560 | >This tutorial encourages a modular approach |
| 561 | to constructing a script. Make note of and collect |
| 562 | <SPAN |
| 563 | CLASS="QUOTE" |
| 564 | >"boilerplate"</SPAN |
| 565 | > code snippets that might be useful |
| 566 | in future scripts. Eventually you will build quite an extensive |
| 567 | library of nifty routines. As an example, the following script |
| 568 | prolog tests whether the script has been invoked with the correct |
| 569 | number of parameters.</P |
| 570 | ><P |
| 571 | ><TABLE |
| 572 | BORDER="0" |
| 573 | BGCOLOR="#E0E0E0" |
| 574 | WIDTH="100%" |
| 575 | ><TR |
| 576 | ><TD |
| 577 | ><PRE |
| 578 | CLASS="PROGRAMLISTING" |
| 579 | > 1 E_WRONG_ARGS=85 |
| 580 | 2 script_parameters="-a -h -m -z" |
| 581 | 3 # -a = all, -h = help, etc. |
| 582 | 4 |
| 583 | 5 if [ $# -ne $Number_of_expected_args ] |
| 584 | 6 then |
| 585 | 7 echo "Usage: `basename $0` $script_parameters" |
| 586 | 8 # `basename $0` is the script's filename. |
| 587 | 9 exit $E_WRONG_ARGS |
| 588 | 10 fi</PRE |
| 589 | ></TD |
| 590 | ></TR |
| 591 | ></TABLE |
| 592 | > |
| 593 | </P |
| 594 | ><P |
| 595 | >Many times, you will write a script that carries out one |
| 596 | particular task. The first script in this chapter is an |
| 597 | example. Later, it might occur to you to generalize |
| 598 | the script to do other, similar tasks. Replacing the literal |
| 599 | (<SPAN |
| 600 | CLASS="QUOTE" |
| 601 | >"hard-wired"</SPAN |
| 602 | >) constants by variables is a step in |
| 603 | that direction, as is replacing repetitive code blocks by <A |
| 604 | HREF="functions.html#FUNCTIONREF" |
| 605 | >functions</A |
| 606 | >.</P |
| 607 | ></TD |
| 608 | ></TR |
| 609 | ></TABLE |
| 610 | ></DIV |
| 611 | ><DIV |
| 612 | CLASS="SECT1" |
| 613 | ><H1 |
| 614 | CLASS="SECT1" |
| 615 | ><A |
| 616 | NAME="INVOKING" |
| 617 | ></A |
| 618 | >2.1. Invoking the script</H1 |
| 619 | ><P |
| 620 | >Having written the script, you can invoke it by <TT |
| 621 | CLASS="USERINPUT" |
| 622 | ><B |
| 623 | >sh |
| 624 | scriptname</B |
| 625 | ></TT |
| 626 | >, |
| 627 | |
| 628 | <A |
| 629 | NAME="AEN300" |
| 630 | HREF="#FTN.AEN300" |
| 631 | >[8]</A |
| 632 | > |
| 633 | |
| 634 | or alternatively <TT |
| 635 | CLASS="USERINPUT" |
| 636 | ><B |
| 637 | >bash scriptname</B |
| 638 | ></TT |
| 639 | >. (Not |
| 640 | recommended is using <TT |
| 641 | CLASS="USERINPUT" |
| 642 | ><B |
| 643 | >sh <scriptname</B |
| 644 | ></TT |
| 645 | >, |
| 646 | since this effectively disables reading from |
| 647 | <A |
| 648 | HREF="ioredirintro.html#STDINOUTDEF" |
| 649 | ><TT |
| 650 | CLASS="FILENAME" |
| 651 | >stdin</TT |
| 652 | ></A |
| 653 | > |
| 654 | within the script.) Much more convenient is to make |
| 655 | the script itself directly executable with a <A |
| 656 | HREF="external.html#CHMODREF" |
| 657 | >chmod</A |
| 658 | >. |
| 659 | |
| 660 | <DIV |
| 661 | CLASS="VARIABLELIST" |
| 662 | ><DL |
| 663 | ><DT |
| 664 | >Either:</DT |
| 665 | ><DD |
| 666 | ><P |
| 667 | ><TT |
| 668 | CLASS="USERINPUT" |
| 669 | ><B |
| 670 | >chmod 555 scriptname</B |
| 671 | ></TT |
| 672 | > (gives |
| 673 | everyone read/execute permission) |
| 674 | <A |
| 675 | NAME="AEN315" |
| 676 | HREF="#FTN.AEN315" |
| 677 | >[9]</A |
| 678 | > |
| 679 | </P |
| 680 | ></DD |
| 681 | ><DT |
| 682 | >or</DT |
| 683 | ><DD |
| 684 | ><P |
| 685 | ><TT |
| 686 | CLASS="USERINPUT" |
| 687 | ><B |
| 688 | >chmod +rx scriptname</B |
| 689 | ></TT |
| 690 | > (gives |
| 691 | everyone read/execute permission)</P |
| 692 | ><P |
| 693 | ><TT |
| 694 | CLASS="USERINPUT" |
| 695 | ><B |
| 696 | >chmod |
| 697 | u+rx scriptname</B |
| 698 | ></TT |
| 699 | > (gives only the |
| 700 | script owner read/execute permission)</P |
| 701 | ></DD |
| 702 | ></DL |
| 703 | ></DIV |
| 704 | > |
| 705 | </P |
| 706 | ><P |
| 707 | >Having made the script executable, you may now test it by |
| 708 | <TT |
| 709 | CLASS="USERINPUT" |
| 710 | ><B |
| 711 | >./scriptname</B |
| 712 | ></TT |
| 713 | >. |
| 714 | |
| 715 | <A |
| 716 | NAME="AEN327" |
| 717 | HREF="#FTN.AEN327" |
| 718 | >[10]</A |
| 719 | > |
| 720 | |
| 721 | If it begins with a <SPAN |
| 722 | CLASS="QUOTE" |
| 723 | >"sha-bang"</SPAN |
| 724 | > line, invoking the |
| 725 | script calls the correct command interpreter to run it.</P |
| 726 | ><P |
| 727 | >As a final step, after testing and debugging, |
| 728 | you would likely want to move it to <TT |
| 729 | CLASS="FILENAME" |
| 730 | >/usr/local/bin</TT |
| 731 | > (as |
| 732 | <I |
| 733 | CLASS="FIRSTTERM" |
| 734 | >root</I |
| 735 | >, of course), to make the script |
| 736 | available to yourself and all other users as a systemwide |
| 737 | executable. The script could then be invoked by simply typing |
| 738 | <B |
| 739 | CLASS="COMMAND" |
| 740 | >scriptname</B |
| 741 | > <B |
| 742 | CLASS="KEYCAP" |
| 743 | >[ENTER]</B |
| 744 | > from the |
| 745 | command-line.</P |
| 746 | ></DIV |
| 747 | ></DIV |
| 748 | ><H3 |
| 749 | CLASS="FOOTNOTES" |
| 750 | >Notes</H3 |
| 751 | ><TABLE |
| 752 | BORDER="0" |
| 753 | CLASS="FOOTNOTES" |
| 754 | WIDTH="100%" |
| 755 | ><TR |
| 756 | ><TD |
| 757 | ALIGN="LEFT" |
| 758 | VALIGN="TOP" |
| 759 | WIDTH="5%" |
| 760 | ><A |
| 761 | NAME="FTN.AEN205" |
| 762 | HREF="sha-bang.html#AEN205" |
| 763 | >[1]</A |
| 764 | ></TD |
| 765 | ><TD |
| 766 | ALIGN="LEFT" |
| 767 | VALIGN="TOP" |
| 768 | WIDTH="95%" |
| 769 | ><P |
| 770 | >More commonly seen in the literature as |
| 771 | <I |
| 772 | CLASS="FIRSTTERM" |
| 773 | >she-bang</I |
| 774 | > or <I |
| 775 | CLASS="FIRSTTERM" |
| 776 | >sh-bang</I |
| 777 | >. |
| 778 | This derives from the concatenation of the tokens |
| 779 | <I |
| 780 | CLASS="FIRSTTERM" |
| 781 | >sharp</I |
| 782 | > (<SPAN |
| 783 | CLASS="TOKEN" |
| 784 | >#</SPAN |
| 785 | >) and |
| 786 | <I |
| 787 | CLASS="FIRSTTERM" |
| 788 | >bang</I |
| 789 | > (<SPAN |
| 790 | CLASS="TOKEN" |
| 791 | >!</SPAN |
| 792 | >).</P |
| 793 | ></TD |
| 794 | ></TR |
| 795 | ><TR |
| 796 | ><TD |
| 797 | ALIGN="LEFT" |
| 798 | VALIGN="TOP" |
| 799 | WIDTH="5%" |
| 800 | ><A |
| 801 | NAME="FTN.AEN214" |
| 802 | HREF="sha-bang.html#AEN214" |
| 803 | >[2]</A |
| 804 | ></TD |
| 805 | ><TD |
| 806 | ALIGN="LEFT" |
| 807 | VALIGN="TOP" |
| 808 | WIDTH="95%" |
| 809 | ><P |
| 810 | >Some flavors of UNIX (those based on 4.2 BSD) |
| 811 | allegedly take a four-byte magic number, requiring |
| 812 | a blank after the <SPAN |
| 813 | CLASS="TOKEN" |
| 814 | >!</SPAN |
| 815 | > -- |
| 816 | <TT |
| 817 | CLASS="USERINPUT" |
| 818 | ><B |
| 819 | >#! /bin/sh</B |
| 820 | ></TT |
| 821 | >. <A |
| 822 | HREF="http://www.in-ulm.de/~mascheck/various/shebang/#details" |
| 823 | TARGET="_top" |
| 824 | > According to Sven Mascheck</A |
| 825 | > this is probably a myth.</P |
| 826 | ></TD |
| 827 | ></TR |
| 828 | ><TR |
| 829 | ><TD |
| 830 | ALIGN="LEFT" |
| 831 | VALIGN="TOP" |
| 832 | WIDTH="5%" |
| 833 | ><A |
| 834 | NAME="FTN.AEN226" |
| 835 | HREF="sha-bang.html#AEN226" |
| 836 | >[3]</A |
| 837 | ></TD |
| 838 | ><TD |
| 839 | ALIGN="LEFT" |
| 840 | VALIGN="TOP" |
| 841 | WIDTH="95%" |
| 842 | ><P |
| 843 | >The <SPAN |
| 844 | CLASS="TOKEN" |
| 845 | >#!</SPAN |
| 846 | > line in a shell script |
| 847 | will be the first thing the command interpreter |
| 848 | (<B |
| 849 | CLASS="COMMAND" |
| 850 | >sh</B |
| 851 | > or <B |
| 852 | CLASS="COMMAND" |
| 853 | >bash</B |
| 854 | >) |
| 855 | sees. Since this line begins with a <SPAN |
| 856 | CLASS="TOKEN" |
| 857 | >#</SPAN |
| 858 | >, |
| 859 | it will be correctly interpreted as a comment when the |
| 860 | command interpreter finally executes the script. The |
| 861 | line has already served its purpose - calling the command |
| 862 | interpreter.</P |
| 863 | ><P |
| 864 | >If, in fact, the script includes an |
| 865 | <SPAN |
| 866 | CLASS="emphasis" |
| 867 | ><I |
| 868 | CLASS="EMPHASIS" |
| 869 | >extra</I |
| 870 | ></SPAN |
| 871 | > <SPAN |
| 872 | CLASS="TOKEN" |
| 873 | >#!</SPAN |
| 874 | > line, then |
| 875 | <B |
| 876 | CLASS="COMMAND" |
| 877 | >bash</B |
| 878 | > will interpret it as a comment. |
| 879 | <TABLE |
| 880 | BORDER="0" |
| 881 | BGCOLOR="#E0E0E0" |
| 882 | WIDTH="100%" |
| 883 | ><TR |
| 884 | ><TD |
| 885 | ><PRE |
| 886 | CLASS="PROGRAMLISTING" |
| 887 | > 1 #!/bin/bash |
| 888 | 2 |
| 889 | 3 echo "Part 1 of script." |
| 890 | 4 a=1 |
| 891 | 5 |
| 892 | 6 #!/bin/bash |
| 893 | 7 # This does *not* launch a new script. |
| 894 | 8 |
| 895 | 9 echo "Part 2 of script." |
| 896 | 10 echo $a # Value of $a stays at 1.</PRE |
| 897 | ></TD |
| 898 | ></TR |
| 899 | ></TABLE |
| 900 | ></P |
| 901 | ></TD |
| 902 | ></TR |
| 903 | ><TR |
| 904 | ><TD |
| 905 | ALIGN="LEFT" |
| 906 | VALIGN="TOP" |
| 907 | WIDTH="5%" |
| 908 | ><A |
| 909 | NAME="FTN.AEN242" |
| 910 | HREF="sha-bang.html#AEN242" |
| 911 | >[4]</A |
| 912 | ></TD |
| 913 | ><TD |
| 914 | ALIGN="LEFT" |
| 915 | VALIGN="TOP" |
| 916 | WIDTH="95%" |
| 917 | ><P |
| 918 | >This allows some cute tricks.</P |
| 919 | ><P |
| 920 | ><TABLE |
| 921 | BORDER="0" |
| 922 | BGCOLOR="#E0E0E0" |
| 923 | WIDTH="100%" |
| 924 | ><TR |
| 925 | ><TD |
| 926 | ><PRE |
| 927 | CLASS="PROGRAMLISTING" |
| 928 | > 1 #!/bin/rm |
| 929 | 2 # Self-deleting script. |
| 930 | 3 |
| 931 | 4 # Nothing much seems to happen when you run this... except that the file disappears. |
| 932 | 5 |
| 933 | 6 WHATEVER=85 |
| 934 | 7 |
| 935 | 8 echo "This line will never print (betcha!)." |
| 936 | 9 |
| 937 | 10 exit $WHATEVER # Doesn't matter. The script will not exit here. |
| 938 | 11 # Try an echo $? after script termination. |
| 939 | 12 # You'll get a 0, not a 85.</PRE |
| 940 | ></TD |
| 941 | ></TR |
| 942 | ></TABLE |
| 943 | ></P |
| 944 | ><P |
| 945 | >Also, try starting a <TT |
| 946 | CLASS="FILENAME" |
| 947 | >README</TT |
| 948 | > file with a |
| 949 | <TT |
| 950 | CLASS="USERINPUT" |
| 951 | ><B |
| 952 | >#!/bin/more</B |
| 953 | ></TT |
| 954 | >, and making it executable. |
| 955 | The result is a self-listing documentation file. (A <A |
| 956 | HREF="here-docs.html#HEREDOCREF" |
| 957 | >here document</A |
| 958 | > using |
| 959 | <A |
| 960 | HREF="external.html#CATREF" |
| 961 | >cat</A |
| 962 | > is possibly a better alternative |
| 963 | -- see <A |
| 964 | HREF="here-docs.html#EX71" |
| 965 | >Example 19-3</A |
| 966 | >).</P |
| 967 | ></TD |
| 968 | ></TR |
| 969 | ><TR |
| 970 | ><TD |
| 971 | ALIGN="LEFT" |
| 972 | VALIGN="TOP" |
| 973 | WIDTH="5%" |
| 974 | ><A |
| 975 | NAME="FTN.AEN256" |
| 976 | HREF="sha-bang.html#AEN256" |
| 977 | >[5]</A |
| 978 | ></TD |
| 979 | ><TD |
| 980 | ALIGN="LEFT" |
| 981 | VALIGN="TOP" |
| 982 | WIDTH="95%" |
| 983 | ><P |
| 984 | ><A |
| 985 | NAME="POSIX2REF" |
| 986 | ></A |
| 987 | ><SPAN |
| 988 | CLASS="strong" |
| 989 | ><B |
| 990 | CLASS="EMPHASIS" |
| 991 | >P</B |
| 992 | ></SPAN |
| 993 | >ortable |
| 994 | <SPAN |
| 995 | CLASS="strong" |
| 996 | ><B |
| 997 | CLASS="EMPHASIS" |
| 998 | >O</B |
| 999 | ></SPAN |
| 1000 | >perating |
| 1001 | <SPAN |
| 1002 | CLASS="strong" |
| 1003 | ><B |
| 1004 | CLASS="EMPHASIS" |
| 1005 | >S</B |
| 1006 | ></SPAN |
| 1007 | >ystem <SPAN |
| 1008 | CLASS="bold" |
| 1009 | ><B |
| 1010 | CLASS="EMPHASIS" |
| 1011 | >I</B |
| 1012 | ></SPAN |
| 1013 | >nterface, an attempt to |
| 1014 | standardize UNI<SPAN |
| 1015 | CLASS="strong" |
| 1016 | ><B |
| 1017 | CLASS="EMPHASIS" |
| 1018 | >X</B |
| 1019 | ></SPAN |
| 1020 | >-like |
| 1021 | OSes. The POSIX specifications are listed on the <A |
| 1022 | HREF="http://www.opengroup.org/onlinepubs/007904975/toc.htm" |
| 1023 | TARGET="_top" |
| 1024 | >Open |
| 1025 | Group site</A |
| 1026 | >.</P |
| 1027 | ></TD |
| 1028 | ></TR |
| 1029 | ><TR |
| 1030 | ><TD |
| 1031 | ALIGN="LEFT" |
| 1032 | VALIGN="TOP" |
| 1033 | WIDTH="5%" |
| 1034 | ><A |
| 1035 | NAME="FTN.AEN269" |
| 1036 | HREF="sha-bang.html#AEN269" |
| 1037 | >[6]</A |
| 1038 | ></TD |
| 1039 | ><TD |
| 1040 | ALIGN="LEFT" |
| 1041 | VALIGN="TOP" |
| 1042 | WIDTH="95%" |
| 1043 | ><P |
| 1044 | >To avoid this possibility, a script may begin |
| 1045 | with a <A |
| 1046 | HREF="system.html#ENVV2REF" |
| 1047 | >#!/bin/env bash</A |
| 1048 | > |
| 1049 | <I |
| 1050 | CLASS="FIRSTTERM" |
| 1051 | >sha-bang</I |
| 1052 | > line. This may be |
| 1053 | useful on UNIX machines where <I |
| 1054 | CLASS="FIRSTTERM" |
| 1055 | >bash</I |
| 1056 | > |
| 1057 | is not located in <TT |
| 1058 | CLASS="FILENAME" |
| 1059 | >/bin</TT |
| 1060 | ></P |
| 1061 | ></TD |
| 1062 | ></TR |
| 1063 | ><TR |
| 1064 | ><TD |
| 1065 | ALIGN="LEFT" |
| 1066 | VALIGN="TOP" |
| 1067 | WIDTH="5%" |
| 1068 | ><A |
| 1069 | NAME="FTN.AEN279" |
| 1070 | HREF="sha-bang.html#AEN279" |
| 1071 | >[7]</A |
| 1072 | ></TD |
| 1073 | ><TD |
| 1074 | ALIGN="LEFT" |
| 1075 | VALIGN="TOP" |
| 1076 | WIDTH="95%" |
| 1077 | ><P |
| 1078 | >If <I |
| 1079 | CLASS="FIRSTTERM" |
| 1080 | >Bash</I |
| 1081 | > is your default |
| 1082 | shell, then the <SPAN |
| 1083 | CLASS="TOKEN" |
| 1084 | >#!</SPAN |
| 1085 | > isn't necessary at the |
| 1086 | beginning of a script. However, if launching a script from |
| 1087 | a different shell, such as <I |
| 1088 | CLASS="FIRSTTERM" |
| 1089 | >tcsh</I |
| 1090 | >, |
| 1091 | then you <SPAN |
| 1092 | CLASS="emphasis" |
| 1093 | ><I |
| 1094 | CLASS="EMPHASIS" |
| 1095 | >will</I |
| 1096 | ></SPAN |
| 1097 | > need the |
| 1098 | <SPAN |
| 1099 | CLASS="TOKEN" |
| 1100 | >#!</SPAN |
| 1101 | >.</P |
| 1102 | ></TD |
| 1103 | ></TR |
| 1104 | ><TR |
| 1105 | ><TD |
| 1106 | ALIGN="LEFT" |
| 1107 | VALIGN="TOP" |
| 1108 | WIDTH="5%" |
| 1109 | ><A |
| 1110 | NAME="FTN.AEN300" |
| 1111 | HREF="sha-bang.html#AEN300" |
| 1112 | >[8]</A |
| 1113 | ></TD |
| 1114 | ><TD |
| 1115 | ALIGN="LEFT" |
| 1116 | VALIGN="TOP" |
| 1117 | WIDTH="95%" |
| 1118 | ><P |
| 1119 | >Caution: invoking a <I |
| 1120 | CLASS="FIRSTTERM" |
| 1121 | >Bash</I |
| 1122 | > |
| 1123 | script by <TT |
| 1124 | CLASS="USERINPUT" |
| 1125 | ><B |
| 1126 | >sh scriptname</B |
| 1127 | ></TT |
| 1128 | > turns off |
| 1129 | Bash-specific extensions, and the script may therefore fail |
| 1130 | to execute.</P |
| 1131 | ></TD |
| 1132 | ></TR |
| 1133 | ><TR |
| 1134 | ><TD |
| 1135 | ALIGN="LEFT" |
| 1136 | VALIGN="TOP" |
| 1137 | WIDTH="5%" |
| 1138 | ><A |
| 1139 | NAME="FTN.AEN315" |
| 1140 | HREF="sha-bang.html#AEN315" |
| 1141 | >[9]</A |
| 1142 | ></TD |
| 1143 | ><TD |
| 1144 | ALIGN="LEFT" |
| 1145 | VALIGN="TOP" |
| 1146 | WIDTH="95%" |
| 1147 | ><P |
| 1148 | >A script needs <I |
| 1149 | CLASS="FIRSTTERM" |
| 1150 | >read</I |
| 1151 | >, as |
| 1152 | well as execute permission for it to run, since the shell |
| 1153 | needs to be able to read it.</P |
| 1154 | ></TD |
| 1155 | ></TR |
| 1156 | ><TR |
| 1157 | ><TD |
| 1158 | ALIGN="LEFT" |
| 1159 | VALIGN="TOP" |
| 1160 | WIDTH="5%" |
| 1161 | ><A |
| 1162 | NAME="FTN.AEN327" |
| 1163 | HREF="sha-bang.html#AEN327" |
| 1164 | >[10]</A |
| 1165 | ></TD |
| 1166 | ><TD |
| 1167 | ALIGN="LEFT" |
| 1168 | VALIGN="TOP" |
| 1169 | WIDTH="95%" |
| 1170 | ><P |
| 1171 | >Why not simply invoke the script with |
| 1172 | <TT |
| 1173 | CLASS="USERINPUT" |
| 1174 | ><B |
| 1175 | >scriptname</B |
| 1176 | ></TT |
| 1177 | >? If the directory you |
| 1178 | are in (<A |
| 1179 | HREF="variables2.html#PWDREF" |
| 1180 | >$PWD</A |
| 1181 | >) is where |
| 1182 | <TT |
| 1183 | CLASS="FILENAME" |
| 1184 | >scriptname</TT |
| 1185 | > is located, why doesn't |
| 1186 | this work? This fails because, for security reasons, the |
| 1187 | current directory (<TT |
| 1188 | CLASS="FILENAME" |
| 1189 | >./</TT |
| 1190 | >) |
| 1191 | is not by default included in a user's <A |
| 1192 | HREF="variables2.html#PATHREF" |
| 1193 | >$PATH</A |
| 1194 | >. It is therefore necessary to |
| 1195 | explicitly invoke the script in the current directory with |
| 1196 | a <TT |
| 1197 | CLASS="USERINPUT" |
| 1198 | ><B |
| 1199 | >./scriptname</B |
| 1200 | ></TT |
| 1201 | >.</P |
| 1202 | ></TD |
| 1203 | ></TR |
| 1204 | ></TABLE |
| 1205 | ><DIV |
| 1206 | CLASS="NAVFOOTER" |
| 1207 | ><HR |
| 1208 | ALIGN="LEFT" |
| 1209 | WIDTH="100%"><TABLE |
| 1210 | SUMMARY="Footer navigation table" |
| 1211 | WIDTH="100%" |
| 1212 | BORDER="0" |
| 1213 | CELLPADDING="0" |
| 1214 | CELLSPACING="0" |
| 1215 | ><TR |
| 1216 | ><TD |
| 1217 | WIDTH="33%" |
| 1218 | ALIGN="left" |
| 1219 | VALIGN="top" |
| 1220 | ><A |
| 1221 | HREF="why-shell.html" |
| 1222 | ACCESSKEY="P" |
| 1223 | >Prev</A |
| 1224 | ></TD |
| 1225 | ><TD |
| 1226 | WIDTH="34%" |
| 1227 | ALIGN="center" |
| 1228 | VALIGN="top" |
| 1229 | ><A |
| 1230 | HREF="index.html" |
| 1231 | ACCESSKEY="H" |
| 1232 | >Home</A |
| 1233 | ></TD |
| 1234 | ><TD |
| 1235 | WIDTH="33%" |
| 1236 | ALIGN="right" |
| 1237 | VALIGN="top" |
| 1238 | ><A |
| 1239 | HREF="prelimexer.html" |
| 1240 | ACCESSKEY="N" |
| 1241 | >Next</A |
| 1242 | ></TD |
| 1243 | ></TR |
| 1244 | ><TR |
| 1245 | ><TD |
| 1246 | WIDTH="33%" |
| 1247 | ALIGN="left" |
| 1248 | VALIGN="top" |
| 1249 | >Shell Programming!</TD |
| 1250 | ><TD |
| 1251 | WIDTH="34%" |
| 1252 | ALIGN="center" |
| 1253 | VALIGN="top" |
| 1254 | ><A |
| 1255 | HREF="part1.html" |
| 1256 | ACCESSKEY="U" |
| 1257 | >Up</A |
| 1258 | ></TD |
| 1259 | ><TD |
| 1260 | WIDTH="33%" |
| 1261 | ALIGN="right" |
| 1262 | VALIGN="top" |
| 1263 | >Preliminary Exercises</TD |
| 1264 | ></TR |
| 1265 | ></TABLE |
| 1266 | ></DIV |
| 1267 | ></BODY |
| 1268 | ></HTML |
| 1269 | > |