
- •Advanced Bash-Scripting Guide
- •Dedication
- •Table of Contents
- •Part 1. Introduction
- •Advanced Bash-Scripting Guide
- •Chapter 2. Starting Off With a Sha-Bang
- •2.1. Invoking the script
- •2.2. Preliminary Exercises
- •Part 2. Basics
- •Chapter 3. Exit and Exit Status
- •Chapter 4. Special Characters
- •Chapter 5. Introduction to Variables and Parameters
- •5.1. Variable Substitution
- •5.2. Variable Assignment
- •5.3. Bash Variables Are Untyped
- •5.4. Special Variable Types
- •Chapter 6. Quoting
- •Chapter 7. Tests
- •7.1. Test Constructs
- •7.2. File test operators
- •7.3. Comparison operators (binary)
- •7.4. Nested if/then Condition Tests
- •7.5. Testing Your Knowledge of Tests
- •8.1. Operators
- •8.2. Numerical Constants
- •Part 3. Beyond the Basics
- •Chapter 9. Variables Revisited
- •9.1. Internal Variables
- •9.2. Manipulating Strings
- •9.3. Parameter Substitution
- •9.4. Typing variables: declare or typeset
- •9.5. Indirect References to Variables
- •9.6. $RANDOM: generate random integer
- •9.7. The Double Parentheses Construct
- •Chapter 10. Loops and Branches
- •10.1. Loops
- •10.2. Nested Loops
- •10.3. Loop Control
- •10.4. Testing and Branching
- •Chapter 11. Internal Commands and Builtins
- •12.1. Basic Commands
- •12.2. Complex Commands
- •12.3. Time / Date Commands
- •12.4. Text Processing Commands
- •12.5. File and Archiving Commands
- •12.6. Communications Commands
- •12.7. Terminal Control Commands
- •12.8. Math Commands
- •12.9. Miscellaneous Commands
- •Chapter 13. System and Administrative Commands
- •Chapter 14. Command Substitution
- •Chapter 15. Arithmetic Expansion
- •Chapter 16. I/O Redirection
- •16.1. Using exec
- •16.2. Redirecting Code Blocks
- •16.3. Applications
- •Chapter 17. Here Documents
- •Chapter 18. Recess Time
- •Part 4. Advanced Topics
- •Chapter 19. Regular Expressions
- •19.1. A Brief Introduction to Regular Expressions
- •19.2. Globbing
- •Chapter 20. Subshells
- •Chapter 21. Restricted Shells
- •Chapter 22. Process Substitution
- •Chapter 23. Functions
- •23.1. Complex Functions and Function Complexities
- •23.2. Local Variables
- •Chapter 24. Aliases
- •Chapter 25. List Constructs
- •Chapter 26. Arrays
- •Chapter 27. Files
- •Chapter 28. /dev and /proc
- •28.2. /proc
- •Chapter 29. Of Zeros and Nulls
- •Chapter 30. Debugging
- •Chapter 31. Options
- •Chapter 32. Gotchas
- •Chapter 33. Scripting With Style
- •Chapter 34. Miscellany
- •34.2. Shell Wrappers
- •34.3. Tests and Comparisons: Alternatives
- •34.4. Optimizations
- •34.5. Assorted Tips
- •34.6. Oddities
- •34.7. Security Issues
- •34.8. Portability Issues
- •34.9. Shell Scripting Under Windows
- •Chapter 35. Bash, version 2
- •36. Endnotes
- •36.1. Author's Note
- •36.2. About the Author
- •36.3. Tools Used to Produce This Book
- •36.4. Credits
- •List of Tables
- •List of Examples
- •Bibliography

Starting Off With a Sha-Bang
Advanced Bash-Scripting Guide:
Prev |
Next |
Chapter 2. Starting Off With a Sha-Bang
Table of Contents
2.1.Invoking the script
2.2.Preliminary Exercises
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.
Example 2-1. cleanup: A script to clean up the log files in /var/log
#cleanup
#Run as root, of course.
cd /var/log
cat /dev/null > messages cat /dev/null > wtmp echo "Logs cleaned up."
There is nothing unusual here, just a set of commands that could just as easily be invoked one by one from the command line on the console or in an xterm. The advantages of placing the commands in a script go beyond not having to retype them time and again. The script can easily be modified, customized, or generalized for a particular application.
Example 2-2. cleanup: An enhanced and generalized version of above script.
#!/bin/bash
#cleanup, version 2
#Run as root, of course.
LOG_DIR=/var/log |
|
|
ROOT_UID=0 |
# |
Only users with $UID 0 have root privileges. |
LINES=50 |
# |
Default number of lines saved. |
E_XCD=66 |
# |
Can't change directory? |
E_NOTROOT=67 |
# |
Non-root exit error. |
if [ "$UID" -ne "$ROOT_UID" ] then
echo "Must be root to run this script." exit $E_NOTROOT
fi
if [ -n "$1" ]
# Test if command line argument present (non-empty). then
lines=$1 else
lines=$LINES # Default, if not specified on command line.
fi
http://tldp.org/LDP/abs/html/sha-bang.html (1 of 4) [7/15/2002 6:34:27 PM]

Starting Off With a Sha-Bang
#Stephane Chazelas suggests the following,
#+ as a better way of checking command line arguments,
#+ but this is still |
a bit advanced for this stage of the tutorial. |
|
# |
|
|
# |
E_WRONGARGS=65 |
# Non-numerical argument (bad arg format) |
# |
|
|
#case "$1" in
# "" ) lines=50;;
#*[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS;;
# |
* |
) lines=$1;; |
# |
esac |
|
# |
|
|
#* Skip ahead |
to "Loops" chapter to decipher all this. |
|
cd $LOG_DIR |
|
|
if [ `pwd` != |
"$LOG_DIR" ] # or |
if [ "$PWD" != "$LOG_DIR" ] |
|
# Not in /var/log? |
|
then |
|
|
echo "Can't |
change to $LOG_DIR." |
|
exit $E_XCD |
|
|
fi # Doublecheck if in right directory, before messing with log file.
#far more efficient is:
#cd /var/log || {
#echo "Cannot change to necessary directory." >&2
#exit $E_XCD;
#}
tail |
-$lines messages > |
mesg.temp # Saves last section of message log file. |
|
mv |
mesg.temp messages |
# Becomes new log directory. |
|
# cat /dev/null > messages |
|||
#* |
No longer needed, as |
the above method is safer. |
|
cat /dev/null > wtmp # |
': > wtmp' and '> wtmp' have the same effect. |
||
echo |
"Logs cleaned up." |
|
|
exit |
0 |
|
|
# |
A |
zero return value from the script upon exit |
|
#+ |
indicates success to |
the shell. |
Since you may not wish to wipe out the entire system log, this variant of the first script keeps the last section of the message log intact. You will constantly discover ways of refining previously written scripts for increased effectiveness.
The sha-bang ( #!) 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. The #! is actually a two-byte [1] "magic number", a special marker that designates a file type, or in this case an executable shell script (see man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or
http://tldp.org/LDP/abs/html/sha-bang.html (2 of 4) [7/15/2002 6:34:27 PM]

Starting Off With a Sha-Bang
a utility. This command interpreter then executes the commands in the script, starting at the top (line 1 of the script), ignoring comments. [2]
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl #!/bin/sed -f #!/usr/awk -f
Each of the above script header lines calls a different command interpreter, be it /bin/sh, the default shell (bash in a Linux system) or otherwise. [3] Using #!/bin/sh, the default Bourne Shell in most commercial variants of UNIX, makes the script portable to non-Linux machines, though you may have to sacrifice a few Bash-specific features (the script will conform to the POSIX [4] sh standard).
Note that the path given at the "sha-bang" must be correct, otherwise an error message, usually "Command not found" will be the only result of running the script.
#! can be omitted if the script consists only of a set of generic system commands, using no internal shell directives. Example 2, above, requires the initial #!, since the variable assignment line, lines=50, uses a shell-specific construct. Note that #!/bin/sh invokes the default shell interpreter, which defaults to /bin/bash on a Linux machine.
This tutorial encourages a modular approach to constructing a script. Make note of and collect "boilerplate" code snippets that might be useful in future scripts. Eventually you can build a quite 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.
if [ $# -ne Number_of_expected args ] then
echo "Usage: `basename $0` whatever" exit $WRONG_ARGS
fi
Notes
[1]Some flavors of UNIX (those based on 4.2BSD) take a four-byte magic number, requiring a blank after the !, #! /bin/sh.
[2]The #! line in a shell script will be the first thing the command interpreter (sh or bash) sees. Since this line begins with a #, 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.
[3]This allows some cute tricks.
http://tldp.org/LDP/abs/html/sha-bang.html (3 of 4) [7/15/2002 6:34:27 PM]

Starting Off With a Sha-Bang
#!/bin/rm
#Self-deleting script.
#Nothing much seems to happen when you run this... except that the file disappears.
WHATEVER=65
echo "This line will never print (betcha!)."
exit $WHATEVER # Doesn't matter. The script will not exit here.
Also, try starting a README file with a #!/bin/more, and making it executable. The result is a self-listing documentation file.
[4] Portable Operating System Interface, an attempt to standardize UNIX-like OSes. |
|
|
Prev |
Home |
Next |
Why Shell Programming? |
Up |
Invoking the script |
http://tldp.org/LDP/abs/html/sha-bang.html (4 of 4) [7/15/2002 6:34:27 PM]