Thursday, July 31, 2008

Shell Utilities

basename
Strips the path information from a file name, printing only the file name. The construction basename $0 lets the script know its name, that is, the name it was invoked by. This can be used for "usage" messages if, for example a script is called with missing arguments:
echo "Usage: `basename $0` arg1 arg2 ... argn"

split, csplit
These are utilities for splitting a file into smaller chunks. Their usual use is for splitting up large files in order to back them up on floppies or preparatory to e-mailing or uploading them.

The csplit command splits a file according to context, the split occuring where patterns are matched.

sum, cksum, md5sum, sha1sum
These are utilities for generating checksums. A checksum is a number mathematically calculated from the contents of a file, for the purpose of checking its integrity. A script might refer to a list of checksums for security purposes, such as ensuring that the contents of key system files have not been altered or corrupted. For security applications, use the md5sum (message digest 5 checksum) command, or better yet, the newer sha1sum (Secure Hash Algorithm).

NOTE :
There have been reports that the 128-bit md5sum can be cracked, so the more secure 160-bit sha1sum is a welcome new addition to the checksum toolkit.

uuencode
This utility encodes binary files (images, sound files, compressed files, etc.) into ASCII characters, making them suitable for transmission in the body of an e-mail message or in a newsgroup posting.
This is especially useful where MIME (multimedia) encoding is not available.

uudecode
This reverses the encoding, decoding uuencoded files back into the original binaries.

crypt
At one time, this was the standard UNIX file encryption utility.

mktemp
Create a temporary file with a "unique" filename. When invoked from the command line without additional arguments, it creates a zero-length file in the /tmp directory.
bash$ mktemp
/tmp/tmp.zzsvql3154

PREFIX=filename
tempfile=`mktemp $PREFIX.XXXXXX`

make
Utility for building and compiling binary packages. This can also be used for any set of operations that is triggered by incremental changes in source files.

The make command checks a Makefile, a list of file dependencies and operations to be carried out.

install
Special purpose file copying command, similar to cp, but capable of setting permissions and attributes of the copied files.


finger
Retrieve information about users on a network. Optionally, this command can display a user's ~/.plan, ~/.project, and ~/.forward files, if present.

run-parts
The run-parts command executes all the scripts in a target directory, sequentially in ASCII-sorted filename order. Of course, the scripts need to have execute permission.
The cron daemon invokes run-parts to run the scripts in the /etc/cron.* directories.

Anacron
Anacron can be used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that aren’t running 24 hours a day, to control daily, weekly, and monthly jobs that are usually controlled by cron.

When executed, Anacron reads a list of jobs from a configuration file, normally /etc/anacrontab (see anacrontab(5)). This file contains the list of jobs that Anacron controls. Each job entry specifies a period in days, a delay in minutes, a unique job identifier, and a shell command.

For each job, Anacron checks whether this job has been executed in the last n days, where n is the period specified for that job. If not, Anacron runs the job’s shell command, after waiting for the number of minutes specified as the delay parameter.

After the command exits, Anacron records the date in a special timestamp file for that job, so it can know when to execute it again. Only the date is used for the time calculations. The hour is not used.

When there are no more jobs to be run, Anacron exits.

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.

tee
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.

dd
This is the somewhat obscure and much feared data duplicator command. Originally a utility for exchanging data on magnetic tapes.
Some basic options to dd are:
◊ if=INFILE INFILE is the source file.
◊ of=OUTFILE OUTFILE is target file, that will have the data written to it.
◊ bs=BLOCKSIZE
◊ count=BLOCKS Copy only this many blocks of data.

Ex.
file_subscript=copy
dd if=$0 of=$0.$file_subscript 2>/dev/null

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.
random000=$(mcookie)
Of course, a script could use md5 for the same purpose. The mcookie command gives yet another way to generate a "unique" filename.

No comments: