- •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
Of Zeros and Nulls
Advanced Bash-Scripting Guide:
Prev |
Next |
Chapter 29. Of Zeros and Nulls
/dev/zero and /dev/null
Uses of /dev/null
Think of /dev/null as a "black hole". It is the nearest equivalent to a write-only file. Everything written to it disappears forever. Attempts to read or output from it result in nothing. Nevertheless, /dev/null can be quite useful from both the command line and in scripts.
Suppressing stdout.
cat $filename >/dev/null
# Contents of the file will not list to stdout.
Suppressing stderr (from Example 12-2).
rm $badname 2>/dev/null
#So error messages [stderr] deep-sixed.
Suppressing output from both stdout and stderr.
cat $filename 2>/dev/null >/dev/null
#If "$filename" does not exist, there will be no error message output.
#If "$filename" does exist, the contents of the file will not list to stdout.
#Therefore, no output at all will result from the above line of code.
#
#This can be useful in situations where the return code from a command #+ needs to be tested, but no output is desired.
#cat $filename &>/dev/null
#also works, as Baris Cicek points out.
Deleting contents of a file, but preserving the file itself, with all attendant permissions (from Example 2-1 and Example 2-2):
cat /dev/null > /var/log/messages
# : > /var/log/messages has same effect, but does not spawn a new process.
cat /dev/null > /var/log/wtmp
http://tldp.org/LDP/abs/html/zeros.html (1 of 4) [7/15/2002 6:34:39 PM]
Of Zeros and Nulls
Automatically emptying the contents of a logfile (especially good for dealing with those nasty "cookies" sent by Web commercial sites):
Example 29-1. Hiding the cookie jar
if [ -f ~/.netscape/cookies ] # Remove, if exists. then
rm -f ~/.netscape/cookies
fi
ln -s /dev/null ~/.netscape/cookies
# All cookies now get sent to a black hole, rather than saved to disk.
Uses of /dev/zero
Like /dev/null, /dev/zero is a pseudo file, but it actually contains nulls (numerical zeros, not the ASCII kind). Output written to it disappears, and it is fairly difficult to actually read the nulls in /dev/zero, though it can be done with od or a hex editor. The chief use for /dev/zero is in creating an initialized dummy file of specified length intended as a temporary swap file.
Example 29-2. Setting up a swapfile using /dev/zero
#!/bin/bash
#Creating a swapfile.
#This script must be run as root.
ROOT_UID=0 |
# Root has $UID 0. |
E_WRONG_USER=65 |
# Not root? |
FILE=/swap |
|
BLOCKSIZE=1024 |
|
MINBLOCKS=40 |
|
SUCCESS=0 |
|
if [ "$UID" -ne "$ROOT_UID" ] then
echo; echo "You must be root to run this script."; echo exit $E_WRONG_USER
fi |
|
if [ -n "$1" ] |
|
then |
|
blocks=$1 |
|
else |
|
blocks=$MINBLOCKS |
# Set to default of 40 blocks |
fi |
# if nothing specified on command line. |
if [ "$blocks" -lt $MINBLOCKS ] |
|
then |
|
blocks=$MINBLOCKS |
# Must be at least 40 blocks long. |
fi |
|
http://tldp.org/LDP/abs/html/zeros.html (2 of 4) [7/15/2002 6:34:39 PM]
Of Zeros and Nulls
echo "Creating swap file of size $blocks blocks (KB)."
dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks # Zero out file.
mkswap |
$FILE $blocks |
# |
Designate it a swap file. |
swapon |
$FILE |
# |
Activate swap file. |
echo "Swap file created and activated."
exit $SUCCESS
Another application of /dev/zero is to "zero out" a file of a designated size for a special purpose, such as mounting a filesystem on a loopback device (see Example 13-6) or securely deleting a file (see Example 12-41).
Example 29-3. Creating a ramdisk
#!/bin/bash
#ramdisk.sh
#A "ramdisk" is a segment of system RAM memory #+ that acts as if it were a filesystem.
#Its advantage is very fast access (read/write time).
#Disadvantages: volatility, loss of data on reboot or powerdown.
# |
less RAM available to system. |
# |
|
#What good is a ramdisk?
#Keeping a large dataset, such as a table or dictionary on ramdisk
#+ speeds up data lookup, since memory access is much faster than disk access.
E_NON_ROOT_USER=70 |
# Must run as root. |
|
ROOTUSER_NAME=root |
|
|
MOUNTPT=/mnt/ramdisk |
|
|
SIZE=2000 |
# 2K |
blocks (change as appropriate) |
BLOCKSIZE=1024 |
# 1K |
(1024 byte) block size |
DEVICE=/dev/ram0 |
# First ram device |
username=`id -nu`
if [ "$username" != "$ROOTUSER_NAME" ] then
echo "Must be root to run \"`basename $0`\"." exit $E_NON_ROOT_USER
fi |
|
|
if [ ! -d "$MOUNTPT" ] |
# |
Test whether mount point already there, |
then |
#+ |
so no error if this script is run |
mkdir $MOUNTPT |
#+ multiple times. |
|
fi |
|
|
dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE |
# Zero out RAM device. |
|
mke2fs $DEVICE |
# Create an ext2 filesystem on it. |
|
mount $DEVICE $MOUNTPT |
# Mount it. |
|
chmod 777 $MOUNTPT |
# So ordinary user can |
access ramdisk. |
http://tldp.org/LDP/abs/html/zeros.html (3 of 4) [7/15/2002 6:34:39 PM]
Of Zeros and Nulls
# However, must be root to unmount it.
echo "\"$MOUNTPT\" now available for use."
#The ramdisk is now accessible for storing files, even by an ordinary user.
#Caution, the ramdisk is volatile, and its contents will disappear
#+ on reboot or power loss.
#Copy anything you want saved to a regular directory.
#After reboot, run this script again to set up ramdisk.
#Remounting /mnt/ramdisk without the other steps will not work.
exit 0
Prev |
Home |
Next |
/proc |
Up |
Debugging |
http://tldp.org/LDP/abs/html/zeros.html (4 of 4) [7/15/2002 6:34:39 PM]