2011年12月29日 星期四

How to become a Hacker

How to become a Hacker


Become an ethical hacker
There are two kind of hackers, malicious hackers who crack email accounts, distribute viruses, cause destruction and steal money from others, known as crackers or black hat hackers, and ethical hackers who tweak hardware and software to create things that nobody ever thought about, hackers are also programmers and security penetration testers whose job is to find a vulnerability, software bug or technical error before the bad guys do, warning the company/community, aka white hat hackers, this post is about how to become an ethical hacker.
To become a computer hacker requires intelligence, dedication and tenacity, problem solving abilities are a must have and above all you must like computer tinkering since you will spend lots of time working with them. Even thought most desktop computers run on Windows, most servers run some kind of Unix variant, i.e. Linux, FreeBSD, Solaris.
Knowing only Windows will get you nowhere, the first step you should do to become a hacker is to get used to a Unix based operating system where you get to see that not all is a point and click mouse and you can play with a program’s source code, unlike Windows or MAC, source code in Unix is free to modify and distribute, aka open source.
GRML Linux hackers distribution
GRML Linux hackers distribution
Linux distributions for hackers:
GRML: A Debian based Linux distribution specially designed for system administrators and text tool users, capable of analyzing systems/networks, to be used as a rescue system or as a main desktop.
BackTrack: Linux distribution intended for security professionals, with tools for hacking wireless access points, exploiting web servers  and learning, an indispensable  security penetration testing tool.
Tip: If you are a Linux beginner download something easier like Knoppix, if you do not want to install it in your computer it can be run as a live CD/DVD or USB thumbdrive, and do not forget that BSD distributions like OpenBSD are also appropriate for hackers.
Programming languages a hacker should learn 
Python is a well designed and documented programming language ideal for beginners due to its power and friendliness, C is a +40 years old general purpose programming language still in use, C++ is related to C and knowing one of the languages will make learning the other easy, Perl is a Unix scripting language often used in network administration, you will need to know Perl in order to understand other people scripts rather than writing it yourself, LISP (LISt Processing) is an ancient programming language closely connected with artificial intelligence projects, reverse engineering and being able to disassemble software is a must have skill for software hackers, if you come across a trojan you will want to know how it works and what it does.
Python: Flexible intuitive programming language with clear and readable syntax, it uses standard libraries and third party modules.
Perl: Feature rich programming language that can be embedded into webservers and databases, also used in e-commerce.
Learn the English language 
English is the main communication language in between people of different countries wherever you go, the ability to communicate verbally and in writing in English is an essential skill, if you have a different mother tongue, once you have mastered the English language you will be able to translate documentation and will be in a better position than monolingual people. English speaking people must also take care not to misspell or make grammatical errors as that makes you look illiterate, impressions count, nobody is going to hire someone who appears to be uneducated and can’t spell.
Dynamo Dictionary: A free online vocabulary game to improve your reading and writing skills.
  Social engineering
Information and communication technologies relies on humans one way or another, human resources, network administrators and security guards are all composed of humans, if a system is too hard to get into then hack the people in charge (aka social engineering). Some companies hire penetration testers to discover their own weak points and test staff security awareness before something happens for real.
How to become a hacker
How to become a hacker
You will want to learn human psychology, what makes people give up their password to strangers? How to get information out of people without knowing them? Can people be fooled into thinking a stranger works for the company just because he says so? All of that is used by hackers to learn of administrator passwords and much more.
Social engineer podcast: A podcast with interviews and tutorials teaching you how people get fooled into giving up private information that later be used for malicious hacking and other bad deeds.
Learn HTML
Everything worthwhile to learn is on the Internet, tutorials, ebooks, software, is all served to you in websites, you should learn HTML language (not a programming language) this will beuseful to understand how websites work and help you to create a blog/website or manage a forum where to interact with others.
W3Schools: Web developer portal for beginners and advanced users, you will learn how to develop a website and technologies being used to do that (HTML, XML, CSS, JavaScript, PHP, and SQL).
Hacking war games
Technology evolves quickly you need to keep learning and adapt to new challenges, security is not a target that can be reached, it is a process that you need to keep improving for ever, hackers never give up due to failure, they keep trying until they find a solution no matter how hard it is. Getting involved in an open source project will be a good way to be part of a community and keep your skills up to date, open source project take all kind of volunteers regardless of skills, if you can’t program yet maybe you can write documentation on how things work.
Another good way to keep your skills up to date and getting started in the hacking world is by joining a war game where the target is to hack other people, everything being legal and only being simulated, aka hacker games.
HackThisSite: A free legal website where to train your hacking skills, with tutorials and a forum where to discuss network security and anything related with computers.
Hacker-Project: Online hacking simulation game where you have to complete tasks like installing virii, hiding/encrypting files,recover logs, acquire computer IPs and collect profits of your hacking activities.
Get hacker status
If you help to test and debug open source software, keep the infrastructure working, write open source software, and publish useful information and submit it to conferences, you will soon come into contact with other hackers who will appreciate your help and dedication as well as admire you for your work, you will make a name for yourself and if you are good enough, get invited to conferences as a speaker, networking will help you be part of the community and maybe even find an employer or people with whom to work together in a project.
Defcon (DEFense CONdition): One of the oldest hackers conferences in the world where to meet like minded people and showcase new hacking tricks and tips.
BlackHat: A series of highly technical information security conferences that brings together hackers from government agencies, corporations and the underground world.

2011年12月12日 星期一

駭客一定要會的進階shell script 設計

Back to FAQS.ORG homepage

Advanced Bash-Scripting Guide

An in-depth exploration of the art of shell scripting

Version 1.8.4
12 June 2003

Mendel Cooper


thegrendel@theriver.com
This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction . . . all the while sneaking in little snippets of UNIX wisdom and lore. It serves as a textbook, a manual for self-study, and a reference and source of knowledge on shell scripting techniques. The exercises and heavily-commented examples invite active reader participation, under the premise that the only way to really learn scripting is to write scripts.
Author's home site.


Dedication

For Anita, the source of all the magic

Table of Contents
Part 1. Introduction
1. Why Shell Programming?
2. Starting Off With a Sha-Bang
2.1. Invoking the script
2.2. Preliminary Exercises
Part 2. Basics
3. Special Characters
4. Introduction to Variables and Parameters
4.1. Variable Substitution
4.2. Variable Assignment
4.3. Bash Variables Are Untyped
4.4. Special Variable Types

5. Quoting

6. Exit and Exit Status

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. Operations and Related Topics


8.1. Operators
8.2. Numerical Constants

Part 3. Beyond the Basics


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

10. Loops and Branches


10.1. Loops
10.2. Nested Loops
10.3. Loop Control
10.4. Testing and Branching

11. Internal Commands and Builtins


11.1. Job Control Commands

12. External Filters, Programs and Commands


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

13. System and Administrative Commands

14. Command Substitution

15. Arithmetic Expansion

16. I/O Redirection


16.1. Using exec
16.2. Redirecting Code Blocks
16.3. Applications

17. Here Documents

18. Recess Time


Part 4. Advanced Topics


19. Regular Expressions
19.1. A Brief Introduction to Regular Expressions
19.2. Globbing

20. Subshells

21. Restricted Shells

22. Process Substitution

23. Functions


23.1. Complex Functions and Function Complexities
23.2. Local Variables

24. Aliases

25. List Constructs

26. Arrays

27. Files

28. /dev and /proc


28.1. /dev
28.2. /proc

29. Of Zeros and Nulls

30. Debugging

31. Options

32. Gotchas

33. Scripting With Style


33.1. Unofficial Shell Scripting Stylesheet

34. Miscellany


34.1. Interactive and non-interactive shells and scripts
34.2. Shell Wrappers
34.3. Tests and Comparisons: Alternatives
34.4. Recursion
34.5. "Colorizing" Scripts
34.6. Optimizations
34.7. Assorted Tips
34.8. Security Issues
34.9. Portability Issues
34.10. Shell Scripting Under Windows

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.3.1. Hardware
36.3.2. Software and Printware

36.4. Credits


Bibliography

A. Contributed Scripts

B. A Sed and Awk Micro-Primer


B.1. Sed
B.2. Awk

C. Exit Codes With Special Meanings

D. A Detailed Introduction to I/O and I/O Redirection

E. Localization

F. History Commands

G. A Sample .bashrc File

H. Converting DOS Batch Files to Shell Scripts

I. Exercises


I.1. Analyzing Scripts
I.2. Writing Scripts

J. Copyright


List of Examples
2-1. cleanup: A script to clean up the log files in /var/log
2-2. cleanup: An enhanced and generalized version of above script.
3-1. Code blocks and I/O redirection
3-2. Saving the results of a code block to a file
3-3. Running a loop in the background
3-4. Backup of all files changed in last day
4-1. Variable assignment and substitution
4-2. Plain Variable Assignment
4-3. Variable Assignment, plain and fancy
4-4. Integer or string?
4-5. Positional Parameters
4-6. whwhois domain name lookup
4-7. Using shift
5-1. Echoing Weird Variables
5-2. Escaped Characters
6-1. exit / exit status
6-2. Negating a condition using !
7-1. What is truth?
7-2. Equivalence of test/usr/bin/test[ ], and /usr/bin/[
7-3. Arithmetic Tests using (( ))
7-4. Testing for broken links
7-5. arithmetic and string comparisons
7-6. testing whether a string is null
7-7. zmost
8-1. Greatest common divisor
8-2. Using Arithmetic Operations
8-3. Compound Condition Tests Using && and ||
8-4. Representation of numerical constants
9-1. $IFS and whitespace
9-2. Timed Input
9-3. Once more, timed input
9-4. Timed read
9-5. Am I root?
9-6. arglist: Listing arguments with $* and $@
9-7. Inconsistent $* and $@ behavior
9-8. $* and $@ when $IFS is empty
9-9. underscore variable
9-10. Inserting a blank line between paragraphs in a text file
9-11. Converting graphic file formats, with filename change
9-12. Alternate ways of extracting substrings
9-13. Using parameter substitution and error messages
9-14. Parameter substitution and "usage" messages
9-15. Length of a variable
9-16. Pattern matching in parameter substitution
9-17. Renaming file extensions:
9-18. Using pattern matching to parse arbitrary strings
9-19. Matching patterns at prefix or suffix of string
9-20. Using declare to type variables
9-21. Indirect References
9-22. Passing an indirect reference to awk
9-23. Generating random numbers
9-24. Picking a random card from a deck
9-25. Rolling the die with RANDOM
9-26. Reseeding RANDOM
9-27. Pseudorandom numbers, using awk
9-28. C-type manipulation of variables
10-1. Simple for loops
10-2. for loop with two parameters in each [list] element
10-3. Fileinfo: operating on a file list contained in a variable
10-4. Operating on files with a for loop
10-5. Missing in [list] in a for loop
10-6. Generating the [list] in a for loop with command substitution
10-7. grep replacement for binary files
10-8. Listing all users on the system
10-9. Checking all the binaries in a directory for authorship
10-10. Listing the symbolic links in a directory
10-11. Symbolic links in a directory, saved to a file
10-12. A C-like for loop
10-13. Using efax in batch mode
10-14. Simple while loop
10-15. Another while loop
10-16. while loop with multiple conditions
10-17. C-like syntax in a while loop
10-18. until loop
10-19. Nested Loop
10-20. Effects of break and continue in a loop
10-21. Breaking out of multiple loop levels
10-22. Continuing at a higher loop level
10-23. Using "continue N" in an actual task
10-24. Using case
10-25. Creating menus using case
10-26. Using command substitution to generate the case variable
10-27. Simple string matching
10-28. Checking for alphabetic input
10-29. Creating menus using select
10-30. Creating menus using select in a function
11-1. printf in action
11-2. Variable assignment, using read
11-3. What happens when read has no variable
11-4. Multi-line input to read
11-5. Detecting the arrow keys
11-6. Using read with file redirection
11-7. Changing the current working directory
11-8. Letting let do some arithmetic.
11-9. Showing the effect of eval
11-10. Forcing a log-off
11-11. A version of "rot13"
11-12. Using eval to force variable substitution in a Perl script
11-13. Using set with positional parameters
11-14. Reassigning the positional parameters
11-15. "unsetting" a variable
11-16. Using export to pass a variable to an embedded awk script
11-17. Using getopts to read the options/arguments passed to a script
11-18. "Including" a data file
11-19. A (useless) script that sources itself
11-20. Effects of exec
11-21. A script that exec's itself
11-22. Waiting for a process to finish before proceeding
11-23. A script that kills itself
12-1. Using ls to create a table of contents for burning a CDR disk
12-2. Badname, eliminate file names in current directory containing bad characters andwhitespace.
12-3. Deleting a file by its inode number
12-4. Logfile using xargs to monitor system log
12-5. copydir, copying files in current directory to another, using xargs
12-6. Using expr
12-7. Using date
12-8. Word Frequency Analysis
12-9. Which files are scripts?
12-10. Generating 10-digit random numbers
12-11. Using tail to monitor the system log
12-12. Emulating "grep" in a script
12-13. Checking words in a list for validity
12-14. toupper: Transforms a file to all uppercase.
12-15. lowercase: Changes all filenames in working directory to lowercase.
12-16. du: DOS to UNIX text file conversion.
12-17. rot13: rot13, ultra-weak encryption.
12-18. Generating "Crypto-Quote" Puzzles
12-19. Formatted file listing.
12-20. Using column to format a directory listing
12-21. nl: A self-numbering script.
12-22. Using cpio to move a directory tree
12-23. Unpacking an rpm archive
12-24. stripping comments from C program files
12-25. Exploring /usr/X11R6/bin
12-26. An "improved" strings command
12-27. Using cmp to compare two files within a script.
12-28. basename and dirname
12-29. Checking file integrity
12-30. uudecoding encoded files
12-31. A script that mails itself
12-32. Monthly Payment on a Mortgage
12-33. Base Conversion
12-34. Another way to invoke bc
12-35. Calculating PI
12-36. Converting a decimal number to hexadecimal
12-37. Factoring
12-38. Calculating the hypotenuse of a triangle
12-39. Using seq to generate loop arguments
12-40. Using getopt to parse command-line options
12-41. Capturing Keystrokes
12-42. Securely deleting a file
12-43. Filename generator
12-44. Converting meters to miles
12-45. Using m4
13-1. setting an erase character
13-2. secret password: Turning off terminal echoing
13-3. Keypress detection
13-4. pidof helps kill a process
13-5. Checking a CD image
13-6. Creating a filesystem in a file
13-7. Adding a new hard drive
13-8. killall, from /etc/rc.d/init.d
14-1. Stupid script tricks
14-2. Generating a variable from a loop
16-1. Redirecting stdin using exec
16-2. Redirecting stdout using exec
16-3. Redirecting both stdin and stdout in the same script with exec
16-4. Redirected while loop
16-5. Alternate form of redirected while loop
16-6. Redirected until loop
16-7. Redirected for loop
16-8. Redirected for loop (both stdin and stdout redirected)
16-9. Redirected if/then test
16-10. Data file "names.data" for above examples
16-11. Logging events
17-1. dummyfile: Creates a 2-line dummy file
17-2. broadcast: Sends message to everyone logged in
17-3. Multi-line message using cat
17-4. Multi-line message, with tabs suppressed
17-5. Here document with parameter substitution
17-6. Parameter substitution turned off
17-7. upload: Uploads a file pair to "Sunsite" incoming directory
17-8. Here documents and functions
17-9. "Anonymous" Here Document
17-10. Commenting out a block of code
17-11. A self-documenting script
20-1. Variable scope in a subshell
20-2. List User Profiles
20-3. Running parallel processes in subshells
21-1. Running a script in restricted mode
23-1. Simple function
23-2. Function Taking Parameters
23-3. Maximum of two numbers
23-4. Converting numbers to Roman numerals
23-5. Testing large return values in a function
23-6. Comparing two large integers
23-7. Real name from username
23-8. Local variable visibility
23-9. Recursion, using a local variable
24-1. Aliases within a script
24-2. unalias: Setting and unsetting an alias
25-1. Using an "and list" to test for command-line arguments
25-2. Another command-line arg test using an "and list"
25-3. Using "or lists" in combination with an "and list"
26-1. Simple array usage
26-2. Formatting a poem
26-3. Some special properties of arrays
26-4. Of empty arrays and empty elements
26-5. Copying and concatenating arrays
26-6. An old friend: The Bubble Sort
26-7. Embedded arrays and indirect references
26-8. Complex array application: Sieve of Eratosthenes
26-9. Emulating a push-down stack
26-10. Complex array application: Exploring a weird mathematical series
26-11. Simulating a two-dimensional array, then tilting it
28-1. Finding the process associated with a PID
28-2. On-line connect status
29-1. Hiding the cookie jar
29-2. Setting up a swapfile using /dev/zero
29-3. Creating a ramdisk
30-1. A buggy script
30-2. Missing keyword
30-3. test24, another buggy script
30-4. Testing a condition with an "assert"
30-5. Trapping at exit
30-6. Cleaning up after Control-C
30-7. Tracing a variable
32-1. Subshell Pitfalls
32-2. Piping the output of echo to a read
34-1. shell wrapper
34-2. A slightly more complex shell wrapper
34-3. shell wrapper around an awk script
34-4. Perl embedded in a Bash script
34-5. Bash and Perl scripts combined
34-6. A (useless) script that recursively calls itself
34-7. A (useful) script that recursively calls itself
34-8. "colorized" address database
34-9. Echoing colored text
34-10. Return value trickery
34-11. Even more return value trickery
34-12. Passing and returning arrays
34-13. Fun with anagrams
35-1. String expansion
35-2. Indirect variable references - the new way
35-3. Simple database application, using indirect variable referencing
35-4. Using arrays and other miscellaneous trickery to deal four random hands from a deck of cards
A-1. manview: Viewing formatted manpages
A-2. mailformat: Formatting an e-mail message
A-3. rn: A simple-minded file rename utility
A-4. blank-rename: renames filenames containing blanks
A-5. encryptedpw: Uploading to an ftp site, using a locally encrypted password
A-6. copy-cd: Copying a data CD
A-7. Collatz series
A-8. days-between: Calculate number of days between two dates
A-9. Make a "dictionary"
A-10. Soundex conversion
A-11. "Game of Life"
A-12. Data file for "Game of Life"
A-13. behead: Removing mail and news message headers
A-14. ftpget: Downloading files via ftp
A-15. password: Generating random 8-character passwords
A-16. fifo: Making daily backups, using named pipes
A-17. Generating prime numbers using the modulo operator
A-18. tree: Displaying a directory tree
A-19. string functions: C-like string functions
A-20. Directory information
A-21. Object-oriented database
G-1. Sample .bashrc file
H-1. VIEWDATA.BAT: DOS Batch File
H-2. viewdata.sh: Shell Script Conversion of VIEWDATA.BAT

  Next
  Introduction

Miscellaneous command on Linux

http://www.faqs.org/docs/abs/HTML/extmisc.html#BLOTOUT


12.9. Miscellaneous Commands

Command that fit in no special category
jotseq
These utilities emit a sequence of integers, with a user-selected increment. The normal separator character between each integer is a newline, but this can be changed with the -soption.
bash$ seq 5
 1
 2
 3
 4
 5
 
 
 
 bash$ seq -s : 5
 1:2:3:4:5
        
Both jot and seq come in handy in a for loop.

Example 12-39. Using seq to generate loop arguments
1 #!/bin/bash
   2 # Using "seq"
   3 
   4 echo
   5 
   6 for a in `seq 80`  # or   for a in $( seq 80 )
   7 # Same as   for a in 1 2 3 4 5 ... 80   (saves much typing!).
   8 # May also use 'jot' (if present on system).
   9 do
  10   echo -n "$a "
  11 done      # 1 2 3 4 5 ... 80
  12 # Example of using the output of a command to generate 
  13 # the [list] in a "for" loop.
  14 
  15 echo; echo
  16 
  17 
  18 COUNT=80  # Yes, 'seq' may also take a replaceable parameter.
  19 
  20 for a in `seq $COUNT`  # or   for a in $( seq $COUNT )
  21 do
  22   echo -n "$a "
  23 done      # 1 2 3 4 5 ... 80
  24 
  25 echo; echo
  26 
  27 BEGIN=75
  28 END=80
  29 
  30 for a in `seq $BEGIN $END`
  31 #  Giving "seq" two arguments starts the count at the first one,
  32 #+ and continues until it reaches the second.
  33 do
  34   echo -n "$a "
  35 done      # 75 76 77 78 79 80
  36 
  37 echo; echo
  38 
  39 BEGIN=45
  40 INTERVAL=5
  41 END=80
  42 
  43 for a in `seq $BEGIN $INTERVAL $END`
  44 #  Giving "seq" three arguments starts the count at the first one,
  45 #+ uses the second for a step interval,
  46 #+ and continues until it reaches the third.
  47 do
  48   echo -n "$a "
  49 done      # 45 50 55 60 65 70 75 80
  50 
  51 echo; echo
  52 
  53 exit 0

getopt
The getopt command parses command-line options preceded by a dash. This external command corresponds to the getopts Bash builtin, but it is not nearly as versatile.

Example 12-40. Using getopt to parse command-line options
1 #!/bin/bash
   2 
   3 # Try the following when invoking this script.
   4 #   sh ex33a -a
   5 #   sh ex33a -abc
   6 #   sh ex33a -a -b -c
   7 #   sh ex33a -d
   8 #   sh ex33a -dXYZ
   9 #   sh ex33a -d XYZ
  10 #   sh ex33a -abcd
  11 #   sh ex33a -abcdZ
  12 #   sh ex33a -z
  13 #   sh ex33a a
  14 # Explain the results of each of the above.
  15 
  16 E_OPTERR=65
  17 
  18 if [ "$#" -eq 0 ]
  19 then   # Script needs at least one command-line argument.
  20   echo "Usage $0 -[options a,b,c]"
  21   exit $E_OPTERR
  22 fi  
  23 
  24 set -- `getopt "abcd:" "$@"`
  25 # Sets positional parameters to command-line arguments.
  26 # What happens if you use "$*" instead of "$@"?
  27 
  28 while [ ! -z "$1" ]
  29 do
  30   case "$1" in
  31     -a) echo "Option \"a\"";;
  32     -b) echo "Option \"b\"";;
  33     -c) echo "Option \"c\"";;
  34     -d) echo "Option \"d\" $2";;
  35      *) break;;
  36   esac
  37 
  38   shift
  39 done
  40 
  41 #  It is better to use the 'getopts' builtin in a script,
  42 #+ rather than 'getopt'.
  43 #  See "ex33.sh".
  44 
  45 exit 0

run-parts
The run-parts command [1] executes all the scripts in a target directory, sequentially in ASCII-sorted filename order. Of course, the scripts need to have execute permission. The crond daemon invokes run-parts to run the scripts in the /etc/cron.* directories.
yes
In its default behavior the yes command feeds a continuous string of the character y followed by a line feed to stdout. A control-c terminates the run. A different output string may be specified, as in yes different string, which would continually output different string to stdout. One might well ask the purpose of this. From the command line or in a script, the output of yes can be redirected or piped into a program expecting user input. In effect, this becomes a sort of poor man's version of expect. yes | fsck /dev/hda1 runs fsck non-interactively (careful!). yes | rm -r dirname has same effect as rm -rf dirname (careful!).
WarningCaution advised when piping yes to a potentially dangerous system command, such as fsck orfdisk. It may have unintended side-effects.
banner
Prints arguments as a large vertical banner to stdout, using an ASCII character (default '#'). This may be redirected to a printer for hardcopy.
printenv
Show all the environmental variables set for a particular user.
bash$ printenv | grep HOME
 HOME=/home/bozo
        
lp
The lp and lpr commands send file(s) to the print queue, to be printed as hard copy. [2] These commands trace the origin of their names to the line printers of another era. bash$ lp file1.txt or bash lp <file1.txt It is often useful to pipe the formatted output from pr to lp. bash$ pr -options file1.txt | lp Formatting packages, such as groff and Ghostscript may send their output directly to lp. bash$ groff -Tascii file.tr | lp bash$ gs -options | lp file.ps Related commands are lpq, for viewing the print queue, and lprm, for removing jobs from the print queue.
tee
[UNIX borrows an idea here from the plumbing trade.] This is a redirection operator, but with a difference. Like the plumber's "tee," it permits "siponing off"to a file the output of a command or commands within a pipe, but without affecting the result. This is useful for printing an ongoing process to a file or paper, perhaps to keep track of it for debugging purposes.
tee
                  |------> to file
                  |
   ===============|===============
   command--->----|-operator-->---> result of command(s)
   ===============================
        
1 cat listfile* | sort | tee check.file | uniq > result.file
(The file check.file contains the concatenated sorted "listfiles", before the duplicate lines are removed byuniq.)
mkfifo
This obscure command creates a named pipe, a temporary first-in-first-out buffer for transferring data between processes. [3] Typically, one process writes to the FIFO, and the other reads from it. See Example A-16.
pathchk
This command checks the validity of a filename. If the filename exceeds the maximum allowable length (255 characters) or one or more of the directories in its path is not searchable, then an error message results. Unfortunately, pathchk does not return a recognizable error code, and it is therefore pretty much useless in a script. Consider instead the file test operators.
dd
This is the somewhat obscure and much feared "data duplicator" command. Originally a utility for exchanging data on magnetic tapes between UNIX minicomputers and IBM mainframes, this command still has its uses. Thedd command simply copies a file (or stdin/stdout), but with conversions. Possible conversions are ASCII/EBCDIC,[4] upper/lower case, swapping of byte pairs between input and output, and skipping and/or truncating the head or tail of the input file. A dd --help lists the conversion and other options that this powerful utility takes.
1 # Exercising 'dd'.
   2 
   3 n=3
   4 p=5
   5 input_file=project.txt
   6 output_file=log.txt
   7 
   8 dd if=$input_file of=$output_file bs=1 skip=$((n-1)) count=$((p-n+1)) 2> /dev/null
   9 # Extracts characters n to p from file $input_file.
  10 
  11 
  12 
  13 
  14 echo -n "hello world" | dd cbs=1 conv=unblock 2> /dev/null
  15 # Echoes "hello world" vertically.
  16 
  17 
  18 # Thanks, S.C.
To demonstrate just how versatile dd is, let's use it to capture keystrokes.

Example 12-41. Capturing Keystrokes
1 #!/bin/bash
   2 # Capture keystrokes without needing to press ENTER.
   3 
   4 
   5 keypresses=4                      # Number of keypresses to capture.
   6 
   7 
   8 old_tty_setting=$(stty -g)        # Save old terminal settings.
   9 
  10 echo "Press $keypresses keys."
  11 stty -icanon -echo                # Disable canonical mode.
  12                                   # Disable local echo.
  13 keys=$(dd bs=1 count=$keypresses 2> /dev/null)
  14 # 'dd' uses stdin, if "if" not specified.
  15 
  16 stty "$old_tty_setting"           # Restore old terminal settings.
  17 
  18 echo "You pressed the \"$keys\" keys."
  19 
  20 # Thanks, S.C. for showing the way.
  21 exit 0

The dd command can do random access on a data stream.
1 echo -n . | dd bs=1 seek=4 of=file conv=notrunc
   2 # The "conv=notrunc" option means that the output file will not be truncated.  
   3 
   4 # Thanks, S.C.
The dd command can copy raw data and disk images to and from devices, such as floppies and tape drives (Example A-6). A common use is creating boot floppies. dd if=kernel-image of=/dev/fd0H1440 Similarly, dd can copy the entire contents of a floppy, even one formatted with a "foreign" OS, to the hard drive as an image file. dd if=/dev/fd0 of=/home/bozo/projects/floppy.img Other applications of dd include initializing temporary swap files (Example 29-2) and ramdisks (Example 29-3). It can even do a low-level copy of an entire hard drive partition, although this is not necessarily recommended. People (with presumably nothing better to do with their time) are constantly thinking of interesting applications of dd.

Example 12-42. Securely deleting a file
1 #!/bin/bash
   2 # blotout.sh: Erase all traces of a file.
   3 
   4 #  This script overwrites a target file alternately
   5 #+ with random bytes, then zeros before finally deleting it.
   6 #  After that, even examining the raw disk sectors
   7 #+ will not reveal the original file data.
   8 
   9 PASSES=7         # Number of file-shredding passes.
  10 BLOCKSIZE=1      #  I/O with /dev/urandom requires unit block size,
  11                  #+ otherwise you get weird results.
  12 E_BADARGS=70
  13 E_NOT_FOUND=71
  14 E_CHANGED_MIND=72
  15 
  16 if [ -z "$1" ]   # No filename specified.
  17 then
  18   echo "Usage: `basename $0` filename"
  19   exit $E_BADARGS
  20 fi
  21 
  22 file=$1
  23 
  24 if [ ! -e "$file" ]
  25 then
  26   echo "File \"$file\" not found."
  27   exit $E_NOT_FOUND
  28 fi  
  29 
  30 echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? "
  31 read answer
  32 case "$answer" in
  33 [nN]) echo "Changed your mind, huh?"
  34       exit $E_CHANGED_MIND
  35       ;;
  36 *)    echo "Blotting out file \"$file\".";;
  37 esac
  38 
  39 
  40 flength=$(ls -l "$file" | awk '{print $5}')  # Field 5 is file length.
  41 
  42 pass_count=1
  43 
  44 echo
  45 
  46 while [ "$pass_count" -le "$PASSES" ]
  47 do
  48   echo "Pass #$pass_count"
  49   sync         # Flush buffers.
  50   dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength
  51                # Fill with random bytes.
  52   sync         # Flush buffers again.
  53   dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength
  54                # Fill with zeros.
  55   sync         # Flush buffers yet again.
  56   let "pass_count += 1"
  57   echo
  58 done  
  59 
  60 
  61 rm -f $file    # Finally, delete scrambled and shredded file.
  62 sync           # Flush buffers a final time.
  63 
  64 echo "File \"$file\" blotted out and deleted."; echo
  65 
  66 
  67 #  This is a fairly secure, if inefficient and slow method
  68 #+ of thoroughly "shredding" a file. The "shred" command,
  69 #+ part of the GNU "fileutils" package, does the same thing,
  70 #+ but more efficiently.
  71 
  72 #  The file cannot not be "undeleted" or retrieved by normal methods.
  73 #  However...
  74 #+ this simple method will likely *not* withstand forensic analysis.
  75 
  76 
  77 #  Tom Vier's "wipe" file-deletion package does a much more thorough job
  78 #+ of file shredding than this simple script.
  79 #     http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2
  80 
  81 #  For an in-depth analysis on the topic of file deletion and security,
  82 #+ see Peter Gutmann's paper,
  83 #+     "Secure Deletion of Data From Magnetic and Solid-State Memory".
  84 #         http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
  85 
  86 
  87 exit 0

od
The od, or octal dump filter converts input (or files) to octal (base-8) or other bases. This is useful for viewing or processing binary data files or otherwise unreadable system device files, such as /dev/urandom, and as a filter for binary data. See Example 9-26 and Example 12-10.
hexdump
Performs a hexadecimal, octal, decimal, or ASCII dump of a binary file. This command is the rough equivalent of od, above, but not nearly as useful.
objdump
Displays an object file or binary executable in either hexadecimal form or as a disassembled listing (with the -d option).
bash$ objdump -d /bin/ls
 /bin/ls:     file format elf32-i386

 Disassembly of section .init:

 080490bc <.init>:
  80490bc:       55                      push   %ebp
  80490bd:       89 e5                   mov    %esp,%ebp
  . . .
        
mcookie
This command generates a "magic cookie", a 128-bit (32-character) pseudorandom hexadecimal number, normally used as an authorization "signature" by the X server. This also available for use in a script as a "quick 'n dirty" random number.
1 random000=`mcookie | sed -e '2p'`
   2 # Uses 'sed' to strip off extraneous characters.
Of course, a script could use md5 for the same purpose.
1 # Generate md5 checksum on the script itself.
   2 random001=`md5sum $0 | awk '{print $1}'`
   3 # Uses 'awk' to strip off the filename.
The mcookie command gives yet another way to generate a "unique" filename.

Example 12-43. Filename generator
1 #!/bin/bash
   2 # tempfile-name.sh:  temp filename generator
   3 
   4 BASE_STR=`mcookie`   # 32-character magic cookie.
   5 POS=11               # Arbitrary position in magic cookie string.
   6 LEN=5                # Get $LEN consecutive characters.
   7 
   8 prefix=temp          #  This is, after all, a "temp" file.
   9                      #  For more "uniqueness," generate the filename prefix
  10                      #+ using the same method as the suffix, below.
  11 
  12 suffix=${BASE_STR:POS:LEN}
  13                      # Extract a 5-character string, starting at position 11.
  14 
  15 temp_filename=$prefix.$suffix
  16                      # Construct the filename.
  17 
  18 echo "Temp filename = "$temp_filename""
  19 
  20 # sh tempfile-name.sh
  21 # Temp filename = temp.e19ea
  22 
  23 exit 0

units
This utility converts between different units of measure. While normally invoked in interactive mode, unitsmay find use in a script.

Example 12-44. Converting meters to miles
1 #!/bin/bash
   2 # unit-conversion.sh
   3 
   4 
   5 convert_units ()  # Takes as arguments the units to convert.
   6 {
   7   cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')
   8   # Strip off everything except the actual conversion factor.
   9   echo "$cf"
  10 }  
  11 
  12 Unit1=miles
  13 Unit2=meters
  14 cfactor=`convert_units $Unit1 $Unit2`
  15 quantity=3.73
  16 
  17 result=$(echo $quantity*$cfactor | bc)
  18 
  19 echo "There are $result $Unit2 in $quantity $Unit1."
  20 
  21 #  What happens if you pass incompatible units,
  22 #+ such as "acres" and "miles" to the function?
  23 
  24 exit 0

m4
A hidden treasure, m4 is a powerful macro processing filter, [5] virtually a complete language. Although originally written as a pre-processor for RatForm4 turned out to be useful as a stand-alone utility. In fact, m4 combines some of the functionality of evaltr, and awk, in addition to its extensive macro expansion facilities. The April, 2002 issue of Linux Journal has a very nice article on m4 and its uses.

Example 12-45. Using m4
1 #!/bin/bash
   2 # m4.sh: Using the m4 macro processor
   3 
   4 # Strings
   5 string=abcdA01
   6 echo "len($string)" | m4                           # 7
   7 echo "substr($string,4)" | m4                      # A01
   8 echo "regexp($string,[0-1][0-1],\&Z)" | m4         # 01Z
   9 
  10 # Arithmetic
  11 echo "incr(22)" | m4                               # 23
  12 echo "eval(99 / 3)" | m4                           # 33
  13 
  14 exit 0

doexec
The doexec command enables passing an arbitrary list of arguments to a binary executable. In particular, passing argv[0] (which corresponds to $0 in a script) lets the executable be invoked by various names, and it can then carry out different sets of actions, according to the name by which it was called. What this amounts to is roundabout way of passing options to an executable. For example, the /usr/local/bin directory might contain a binary called "aaa". Invoking doexec /usr/local/bin/aaa list would list all those files in the current working directory beginning with an"a", while invoking (the same executable with) doexec /usr/local/bin/aaa delete would delete those files.
NoteThe various behaviors of the executable must be defined within the code of the executable itself, analogous to something like the following in a shell script:
1 case `basename $0` in
   2 "name1" ) do_something;;
   3 "name2" ) do_something_else;;
   4 "name3" ) do_yet_another_thing;;
   5 *       ) bail_out;;
   6 esac

Notes

[1]This is actually a script adapted from the Debian Linux distribution.
[2]The print queue is the group of jobs "waiting in line" to be printed.
[3]For an excellent overview of this topic, see Andy Vaught's article, Introduction to Named Pipes, in the September, 1997 issue of Linux Journal.
[4]EBCDIC (pronounced "ebb-sid-ic") is an acronym for Extended Binary Coded Decimal Interchange Code. This is an IBM data format no longer in much use. A bizarre application of the conv=ebcdic option of dd is as a quick 'n easy, but not very secure text file encoder.
1 cat $file | dd conv=swab,ebcdic > $file_encrypted
   2 # Encode (looks like gibberish).      
   3 # Might as well switch bytes (swab), too, for a little extra obscurity.
   4 
   5 cat $file_encrypted | dd conv=swab,ascii > $file_plaintext
   6 # Decode.
[5]macro is a symbolic constant that expands into a command string or a set of operations on parameters.