Advanced Bash-Scripting HOWTO: A guide to shell scripting, using Bash | ||
---|---|---|
Prev | Chapter 3. Tutorial / Reference | Next |
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 or stderr (from Example 3-80):
rm $badname 2>/dev/null # So error messages [stderr] deep-sixed. |
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 cat /dev/null > /var/log/wtmp |
Automatically emptying the contents of a log file (especially good for dealing with those nasty "cookies" sent by Web commercial sites):
rm -f ~/.netscape/cookies ln -s /dev/null ~/.netscape/cookies # All cookies now get sent to a black hole, rather than saved to disk. |
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 3-79. Setting up a swapfile using /dev/zero
#!/bin/bash # Creating a swapfile. # This script must be run as root. FILE=/swap BLOCKSIZE=1024 PARAM_ERROR=33 SUCCESS=0 if [ -z $1 ] then echo "Usage: `basename $0` swapfile-size" # Must be at least 40 blocks. exit $PARAM_ERROR fi dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$1 echo "Creating swapfile of size $1 blocks (KB)." mkswap $FILE $1 swapon $FILE echo "Swapfile activated." exit $SUCCESS |
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |