Digits graph Printable characters except space lower Lowercase letters print Printable characters punct Punctuation space Whitespace (horizontal or vertical) upper Uppercase letters xdigit Hexadecimal digits [=char=] The class of characters in which char belongs. Examples Change uppercase to lowercase in a file: cat file | tr ‘[A-Z]’ ‘[a-z]’ Turn spaces into newlines (ASCII code 012): tr ‘ ‘ ‘ 12′ < file Strip blank lines from file and save in new.file (or use 011 to change successive tabs into one tab): cat file | tr -s "" " 12" > new.file Delete colons from file; save result in new.file: tr -d : < file > new.file #BREAK# traceroute traceroute [options] host [packetsize] TCP/IP command. Trace route taken by packets to reach network host. traceroute attempts tracing by launching UDP probe packets with a small TTL (time to live), then listening for an ICMP “time exceeded” reply from a gateway. host is the destination hostname or the IP number of host to reach. packetsize is the packet size in bytes of the probe datagram. Default is 38 bytes. Options -d Turn on socket-level debugging. -g addr Enable the IP LSRR (Loose Source Record Route) option in addition to the TTL tests, to ask how someone at IP address addr can reach a particular target. -l Include the time-to-live value for each packet received. -m max_ttl Set maximum time-to-live used in outgoing probe packets to max-ttl hops. Default is 30 hops. -n Show numerical addresses; do not look up hostnames. (Useful if DNS is not functioning properly.) -p port Set base UDP port number used for probe packets to port. Default is (decimal) 33434. -q n Set number of probe packets for each time-to-live setting to the value n. Default is 3. -r Bypass normal routing tables and send directly to a host on an attached network. -s src_addr Use src_addr as the IP address that will serve as the source address in outgoing probe packets. -t tos Set the type-of-service in probe packets to tos (default 0). The value must be a decimal integer in the range 0 to 255. -v Verbose — received ICMP packets (other than TIME_EXCEEDED and PORT_UNREACHABLE) will be listed. -w wait Set time to wait for a response to an outgoing probe packet to wait seconds (default is 3 seconds). #BREAK# troff troff See groff. true true A null command that returns a successful (0) exit status. See also false. tune2fs tune2fs [options] device System administration command. Tune the parameters of a Linux Second Extended Filesystem by adjusting various parameters. You must specify the device on which the filesystem resides; it must not be mounted read/write when you change its parameters. Options -c mount-counts Specify the maximum number of mount counts between two checks on the filesystem. -e behavior Specify the kernel’s behavior when encountering errors. behavior must be one of: continue Continue as usual. remount-ro Remount the offending filesystem in read-only mode. panic Cause a kernel panic. -g group Allow group (a group ID or name) to use reserved blocks. -i interval[d|w|m] Specify the maximum interval between filesystem checks. Units may be in days (d), weeks (w), or months (m). If interval is 0, checking will not be time-dependent. -l Display a list of the superblock’s contents. -m percentage Specify the percentage of blocks that will be reserved for use by privileged users. -r num Specify the number of blocks that will be reserved for use by privileged users. -u user #BREAK# Allow user (a user ID or name) to use reserved blocks. tunelp tunelp device [options] System administration command. Control a lineprinter’s device parameters. Without options, print information about device(s). Options -a [on|off] Specify whether or not to abort if the printer encounters an error. By default, do not abort. -c n Retry device n times if it refuses a character. (Default is 250.) After exhausting n, sleep before retrying. -i irq Use irq for specified parallel port. Ignore -t and -c. If 0, restore noninterrupt driven (polling) action. -o [on|off] Specify whether to abort if device is not online or is out of paper. -q [on|off] Specify whether to print current IRQ setting. -r Reset port. -s Display printer’s current status. -t time Specify a delay of time in jiffies to sleep before resending a refused character to the device. A jiffy is defined as either one tick of the system clock or one AC cycle time; it should be approximately 1/100th of a second. -w time Specify a delay of time in jiffies to sleep before resending a strobe signal. -C [on|off] Specify whether to be extremely careful in checking for printer error. #BREAK# ul ul [options] [names] Translate underscores to underlining. The correct sequence with which to do this will vary by terminal type. Some terminals are unable to handle underlining. Options -i Translate -, when on a separate line, to underline, instead of translating underscores. -t terminal-type Specify terminal type. By default, TERM is consulted. umount umount [options] [special-device/directory] System administration command. Unmount a filesystem. umount announces to the system that the removable file structure previously mounted on device special-device is to be removed. umount also works by specifying the directory. Any pending I/O for the filesystem is completed, and the file structure is flagged as clean. Options -a Unmount all filesystems that are listed in /etc/mtab. -n Unmount, but do not record changes in /etc/mtab. -t type Unmount only filesystems of type type. uname uname [options] Print information about the machine and operating system. Without options, print the name of the operating system (Linux). Options -a, –all Combine all the system information from the other options. -m, –machine Print the hardware the system is running on. -n, –nodename Print the machine’s hostname. -r, –release Print the release number of the kernel. -s, –sysname Print the name of the operating system (Linux). -p, –processor #BREAK# Print the type of processor (not available on all versions). -v Print build information about the kernel. –help Display a help message and then exit. –version Print version information and then exit. uncompress uncompress [options] files Uncompress files that were compressed (i.e., whose names end in .Z). See compress for the available options; uncompress takes all the same options except -r and -b. unexpand unexpand [options] [files] Convert strings of initial whitespace, consisting of at least two spaces and/or tabs to tabs. Read from standard input if given no file or a file named -. Options -a, –all Convert all, not just initial, strings of spaces and tabs. -nums, -t nums, –tabs nums nums is a comma-separated list of integers that specify the placement of tab stops. If a single integer is provided, the tab stops are set to every integer spaces. By default, tab stops are 8 spaces apart. With -t and –tabs, the list may be separated by whitespace instead of commas. This option implies -a. –help Print help message and then exit. –version Print the version number and then exit. uniq uniq [options] [file1 [file2]] Remove duplicate adjacent lines from sorted file1, sending one copy of each line to file2 (or to standard output). Often used as a filter. Specify only one of -d or -u. See also comm and sort. Options -n, -f n, –skip-fields=n Ignore first n fields of a line. Fields are separated by spaces or by tabs. +n, -s n, –skip-chars=n Ignore first n characters of a field. -c, –count Print each line once, prefixing number of instances. #BREAK# -d, –repeated Print duplicate lines once but no unique lines. -i, –ignore-case Ignore case differences when checking for duplicates. -u, –unique Print only unique lines (no copy of duplicate entries is kept). -w n, –check-chars=n Compare only first n characters per line (beginning after skipped fields and characters). –help Print a help message and then exit. –version Print version information and then exit. Examples Send one copy of each line from list to output file list.new: uniq list list.new Show which names appear more than once: sort names | uniq -d unshar unshar [options] [files] Unpack a shell archive (shar file). unshar scans mail messages looking for the start of a shell archive. It then passes the archive through a copy of the shell to unpack it. unshar accepts multiple files. If no files are given, standard input is used. Options -c, –overwrite Overwrite existing files. -d directory, –directory=directory Change to directory before unpacking any files. -e, –exit-0 Sequentially unpack multiple archives stored in same file; uses clue that many shar files are terminated by an exit 0 at the beginning of a line. (Equivalent to -E “exit 0″.) -E string, –split-at=string Like -e, but allows you to specify the string that separates archives. #BREAK# -f, –force Same as -c. –help Print help message and then exit. –version Print the version number and then exit. update update [options] System administration command. update is a daemon that controls how often the kernel’s disk buffers are flushed to disk. update is also known as bdflush. The daemon forks a couple of processes to call system functions flush() and sync(). When called by an unprivileged user, no daemon is created. Instead, update calls sync() and then exits. By default, update will wake up every 5 seconds and flush() some dirty buffers. If that doesn’t work, it will try waking up every 30 seconds to sync() the buffers to disk. Not all of the listed options are available in every version of update. Options -d Display the kernel parameters. This does not start the update daemon. -f seconds Call flush() at this interval. Default is 5. -h Help. Print a command summary. -s seconds Call sync() at this interval. Default is 30. -S Always use sync() instead of flush. -0 percent Flush buffers when the specified percent of the buffer cache is dirty. -1 blocks The maximum number of dirty blocks to write out per wake cycle. -2 buffers The number of clean buffers to try to obtain each time the free buffers are refilled. -3 blocks Flush buffers if dirty blocks exceed blocks when trying to refill the buffers. -4 percent #BREAK# Percent of buffer cache to scan when looking for free clusters. -5 seconds Time for a data buffer to age before being flushed. -6 seconds Time for a nondata buffer to age before being flushed. -7 constant The time constant to use for load average. -8 ratio How low the load average can be before trimming back the number of buffers. uptime uptime Print the current time, amount of time logged in, number of users currently logged in (which may include the same user multiple times), and system load averages. This output is also produced by the first line of the w command. useradd useradd [options] [user] System administration command. Create new user accounts or update default account information. Unless invoked with the -D option, user must be given. useradd will create new entries in system files. Home directories and initial files may also be created as needed. Options -c comment Comment field. -d dir Home directory. The default is to use user as the directory name under the home directory specified with the -D option. -e date Account expiration date. date is in the format MM/DD/YYYY. Two-digit year fields are also accepted. The value is stored as the number of days since January 1, 1970. This option requires the use of shadow passwords. -f days Permanently disable account this many days after the password has expired. A value of -1 disables this feature. This option requires the use of shadow passwords. -g group Initial group name or ID number. If a different default group has not been specified using the -D option, the default group is 1. -G groups #BREAK# Supplementary groups given by name or number in a comma-separated list with no whitespace. -k [dir] Copy default files to user’s home directory. Meaningful only when used with the -m option. Default files are copied from /etc/skel/ unless an alternate dir is specified. -m Make user’s home directory if it does not exist. The default is not to make the home directory. -o Override. Accept a nonunique uid with the -u option. (Probably a bad idea.) -s shell Login shell. -u uid Numerical user ID. The value must be unique unless the -o option is used. The default value is the smallest ID value greater than 99 and greater than every other uid. -D [options] Set or display defaults. If options are specified, set them. If no options are specified, display current defaults. The options are: -b dir Home directory prefix to be used in creating home directories. If the -d option is not used when creating an account, the user name will be appended to dir. -e date Expire date. Requires the use of shadow passwords. -f days Number of days after a password expires to disable an account. Requires the use of shadow passwords. -g group Initial group name or ID number. -s shell Default login shell. #BREAK# userdel userdel [option] user System administration command. Delete all entries for user in system account files. Option -r Remove the home directory of user and any files contained in it. usermod usermod [options] user System administration command. Modify user account information. Options -c comment Comment field. -d dir Home directory. -e date Account expiration date. date is in the format MM/DD/YYYY. Two-digit year fields are also accepted, but the value is stored as the number of days since January 1, 1970. This option requires the use of shadow passwords. -f days Permanently disable account this many days after the password has expired. A value of -1 disables this feature. This option requires the use of shadow passwords. -g group Initial group name or number. -G groups Supplementary groups given by name or number in a comma-separated list with no whitespace. user will be removed from any groups to which they currently belong that are not included in groups. -l name Login name. This cannot be changed while the user is logged in. -o Override. Accept a nonunique uid with the -u option. -s shell Login shell. -u uid Numerical user ID. The value must be unique unless the -o option is used. Any files owned by user in the user’s home directory will have their user ID changed automatically. Files outside of the home directory #BREAK# will not be changed. user should not be executing any processes while this is changed. users users [file] Print a space-separated list of each login session on the host. Note that this may include the same user multiple times. Consult file or, by default, /etc/utmp. usleep usleep [microseconds] usleep [options] Sleep some number of microseconds (default is 1). Options –help Print help information and then exit. –usage Print usage message and then exit. -v, –version Print version information. uudecode uudecode [-o outfile] [file] Read a uuencoded file and re-create the original file with the permissions and name set in the file (see uuencode). The -o option specifies an alternate output file. uuencode uuencode [-m] [file] name Encode a binary file. The encoding uses only printable ASCII characters and includes the permissions and name of the file. When file is reconverted via uudecode, the output is saved as name. If the file argument is omitted, uuencode can take standard input, so a single argument is taken as the name to be given to the file when it is decoded. With the -m option, base64 encoding is used. Example It’s common to encode a file and save it with an identifying extension, such as .uue. This example encodes the binary file flower12.jpg, names it rose.jpg, and saves it to a .uue file: % uuencode flower12.jpg rose.jpg > rose.uue Encode flower12.jpg and mail it: % uuencode flower12.jpg flower12.jpg | mail ellen@oreilly.com #BREAK# vacation vacation vacation [options] [user] Automatically return a mail message to the sender announcing that you are on vacation. Use vacation with no options to initialize the vacation mechanism. The process performs several steps. 1. Creates a .forward file in your home directory. The .forward file contains: user, “|/usr/bin/vacation user” user is your login name. The action of this file is to actually deliver the mail to user (i.e., you) and to run the incoming mail through vacation. 2. Creates the .vacation.pag and .vacation.dir files. These files keep track of who has sent you messages, so that they receive only one “I’m on vacation” message from you per week. 3. Starts an editor to edit the contents of .vacation.msg. The contents of this file are mailed back to whomever sends you mail. Within its body, $subject is replaced with the contents of the incoming message’s Subject line. Remove or rename the .forward file to disable vacation processing. Options The -a and -r options are used within a .forward file; see the example. -a alias Mail addressed to alias is actually mail for the user and should produce an automatic reply. -i Reinitialize the .vacation.pag and .vacation.dir files. Use this right before leaving for your next vacation. -r interval By default, no more than one message per week is sent to any sender. This option changes that interval. interval is a number with a trailing s, m, h, d, or w indicating seconds, minutes, hours, days, or weeks, respectively. If interval is infinite, only one reply is sent to each sender. Example Send no more than one reply every three weeks to any given sender: $ cd$ vacation -I$ cat .forwardjp, “|/usr/bin/vacation -r3w jp” $ cat .vacation.msgFrom: jp@wizard-corp.com (J. Programmer, via the vacation program) Subject: I’m out of the office … Hi. I’m off on a well-deserved vacation after finishingup whizprog 1.0. I will read and reply to your mailregarding “$SUBJECT” when I return. Have a nice day. #BREAK# vi vi [options] [files] A screen-oriented text editor based on ex. For more information on vi, see Chapter 11, “The vi Editor”. vidmode vidmode [option] image [mode [offset]] System administration command. Sets the video mode for a kernel image. If no arguments are specified, print current mode value. mode is a 1-byte value located at offset 506 in a kernel image. You may change the mode by specifying the kernel image to change, the new mode, and the byte offset at which to place the new information (the default is 506). Note that rdev -v is a synonym for vidmode. If LILO is used, vidmode is not needed. The video mode can be set from the LILO prompt during a boot. Modes -3 Prompt -2 Extended VGA -1 Normal VGA 0 Same as entering 0 at the prompt 1 Same as entering 1 at the prompt 2 Same as entering 2 at the prompt 3 Same as entering 3 at the prompt n Same as entering n at the prompt Option -o offset Same as specifying an offset as an argument. #BREAK# w w [options] [user] Print summaries of system usage, currently logged-in users, and what they are doing. w is essentially a combination of uptime, who, and ps -a. Display output for one user by specifying user. Options -f Toggle printing the from (remote hostname) field. -h Suppress headings and uptime information. -s Use the short format. -u Ignore the username while figuring out the current process and CPU times. -V Display version information. File /var/run/utmp List of users currently logged on. wall wall [file] System administration command. Write to all users. wall reads a message from the standard input until an end-of- file. It then sends this message to all users currently logged in, preceded by “Broadcast Message from…” If file is specified, read input from that, rather than from standard input. wc wc [options] [files] Print character, word, and line counts for each file. Print a total line for multiple files. If no files are given, read standard input. See other examples under ls and sort. Options -c, -bytes, –chars Print character count only. -l, –lines Print line count only. -w, –words Print word count only. –help #BREAK# Print help message and then exit. –version Print the version number and then exit. Examples Count the number of users logged in: who | wc -l Count the words in three essay files: wc -w essay.[123] Count lines in the file named by variable $file (don’t display filename): wc -l < $file whatis whatis keywords Search the short manual page descriptions in the whatis database for each keyword and print a one-line description to standard output for each match. Like apropos, except that it only searches for complete words. Equivalent to man -f. whereis whereis [options] files Locate the binary, source, and manual page files for specified commands/files. The supplied filenames are first stripped of leading pathname components and any (single) trailing extension of the form .ext (for example, .c). Prefixes of s. resulting from use of source code control are also dealt with. whereis then attempts to locate the desired program in a list of standard Linux directories (e.g., /bin, /etc, /usr/bin, /usr/local/bin/, etc.). Options -b Search only for binaries. -f Terminate the last directory list and signal the start of filenames; required when any of the -B, -M, or -S options are used. -m Search only for manual sections. -s Search only for sources. -u Search for unusual entries, that is, files that do not have one entry of each requested type. Thus, the command whereis -m -u * asks for those files in the current directory that have no documentation. -B directories #BREAK# Change or otherwise limit the directories to search for binaries. -M directory Change or otherwise limit the directories to search for manual sections. -S directory Change or otherwise limit the directories to search for sources. Example Find all files in /usr/bin that are not documented in /usr/man/man1 but that have source in /usr/src: % cd /usr/bin% whereis -u -M /usr/man/man1 -S /usr/src -f * which which [options] [--] [command] [...] List the full pathnames of the files that would be executed if the named commands had been run. which searches the user's $PATH environment variable. The C shell and tcsh have a built-in which command that has no options. To use the options, specify the full pathname (e.g., /usr/bin/which). Options -a, --all Print all matches, not just the first. -i, --read-alias Read aliases from standard input and write matches to standard output. Useful for using an alias for which. --skip-alias Ignore --read-alias if present. Useful for finding normal binaries while using --read-alias in an alias for which. --skip-dot Skip directories that start with a dot. --skip-tilde Skip directories that start with a tilde (~) and executables in $HOME. --show-dot If a matching command is found in a directory that starts with a dot, print ./cmdname instead of the full pathname. --show-tilde Print a tilde (~) to indicate the user's home directory. Ignored if the user is root. --tty-only Stop processing options on the right if not on a tty. #BREAK# -v, -V, --version Print version information and then exit. Example $ which cc ls/usr/bin/ccls: aliased to ls -sFC who who [options] [file] who am i Show who is logged in to the system. With no options, list the names of users currently logged in, their terminal, the time they have been logged in, and the name of the host from which they have logged on. An optional system file (default is /etc/utmp) can be supplied to give additional information. Options am i Print the username of the invoking user. --help Print a help message and then exit. -i, -u, --idle Include idle times. An idle time of . indicates activity within the last minute; one of old indicates no activity in more than a day. -l, --lookup Attempt to include canonical hostnames via DNS. -m Same as who am i. -q, --count "Quick." Display only the usernames and total number of users. --version Print version information and then exit. -w, -T, --mesg, --message, --writable Include user's message status: + mesg y (write messages allowed) - mesg n (write messages refused) #BREAK# ? Cannot find terminal device -H, --heading Print headings. Example This sample output was produced at 8 a.m. on April 17: $ who -uHNAME LINE TIME IDLE PID COMMENTSEarvin ttyp3 Apr 16 08:14 16:25 2240 Larry ttyp0 Apr 17 07:33 . 15182 Since Earvin has been idle since yesterday afternoon (16 hours), it appears that he isn't at work yet. He simply left himself logged in. Larry's terminal is currently in use. whoami whoami Print current user ID. Equivalent to id -un. write write user [tty] message Initiate or respond to an interactive conversation with user. A write session is terminated with EOF. If the user is logged in to more than one terminal, specify a tty number. See also talk; use mesg to keep other users from writing to your terminal. xargs xargs [options] [command] Execute command (with any initial arguments), but read remaining arguments from standard input instead of specifying them directly. xargs passes these arguments in several bundles to command, allowing command to process more arguments than it could normally handle at once. The arguments are typically a long list of filenames (generated by ls or find, for example) that get passed to xargs via a pipe. Options -0, --null Expect filenames to be terminated by NULL instead of whitespace. Do not treat quotes or backslashes specially. -e[string], --eof[=string] Set EOF to _ or, if specified, to string. --help Print a summary of the options to xargs and then exit. -i[string], --replace[=string] Edit all occurrences of {}, or string, to the names read in on standard input. Unquoted blanks are not considered argument terminators. Implies -x and -l 1. #BREAK# -l[lines], --max-lines[=lines] Allow no more than 1, or lines, nonblank input lines on the command line. Implies -x. -n args, --max-args=args Allow no more than args arguments on the command line. May be overridden by -s. -p, --interactive Prompt for confirmation before running each command line. Implies -t. -P max, --max-procs=max Allow no more than max processes to run at once. The default is 1. A maximum of 0 allows as many as possible to run at once. -r, --no-run-if-empty Do not run command if standard input contains only blanks. -s max, --max-chars=max Allow no more than max characters per command line. -t, --verbose Verbose mode. Print command line on standard error before executing. -x, --exit If the maximum size (as specified by -s) is exceeded, exit. --version Print the version number of xargs and then exit. Examples grep for pattern in all files on the system: find / -print | xargs grep pattern > out & Run diff on file pairs (e.g., f1.a and f1.b, f2.a and f2.b …): echo $* | xargs -n2 diff The previous line would be invoked as a shell script, specifying filenames as arguments. Display file, one word per line (same as deroff -w): cat file | xargs -n1 Move files in olddir to newdir, showing each command: ls olddir | xargs -i -t mv olddir/{} newdir/{} #BREAK# yacc yacc [options] file Given a file containing context-free grammar, convert file into tables for subsequent parsing and send output to y.tab.c. This command name stands for yet another compiler-compiler. See also flex, bison, and lex & yacc by John Levine, Tony Mason, and Doug Brown. Options -b prefix Prepend prefix, instead of y, to the output file. -d Generate y.tab.h, producing #define statements that relate yacc’s token codes to the token names declared by the user. -l Exclude #line constructs from code produced in y.tab.c. (Use after debugging is complete.) -t Compile runtime debugging code. -v Generate y.output, a file containing diagnostics and notes about the parsing tables. yes yes [strings] yes [option] Print the command-line arguments, separated by spaces and followed by a newline, until killed. If no arguments are given, print y followed by a newline until killed. Useful in scripts and in the background; its output can be piped to a program that issues prompts. Options –help Print a help message and then exit. –version Print version information and then exit. ypbind ypbind [options] NFS/NIS command. NIS binder process. ypbind is a daemon process typically activated at system startup time. Its function is to remember information that lets client processes on a single node communicate with some ypserv process. The information ypbind remembers is called a binding — the association of a domain name with the Internet address of the NIS server and the port on that host at which the ypserv process is listening for service requests. This information is cached in the file /var/yp/bindings/domainname.version. Options -ypset #BREAK# May be used to change the binding. This option is very dangerous and should be used only for debugging the network from a remote machine. -ypsetme ypset requests may be issued from this machine only. Security is based on IP address checking, which can be defeated on networks on which untrusted individuals may inject packets. This option is not recommended. ypcat ypcat [options] mname NFS/NIS command. Print values in an NIS database specified by mname, which may be either a map name or a map nickname. Options -d domain Specify domain other than default domain. -k Display keys for maps in which values are null or key is not part of value. -t Do not translate mname to map name. -x Display map nickname table listing the nicknames (mnames) known and map name associated with each nickname. Do not require an mname argument. ypchfn ypchfn [option] [user] NFS/NIS command. Change your information stored in /etc/passwd and displayed when you are fingered; distribute the change over NIS. Without options, ypchfn enters interactive mode and prompts for changes. To make a field blank, enter the keyword none. The superuser can change the information for any user. See also yppasswd and ypchsh. Options -f Behave like ypchfn (default). -l Behave like ypchsh. -p Behave like yppasswd. #BREAK# ypchsh ypchsh [option] [user] NFS/NIS command. Change your login shell and distribute this information over NIS. Warn if shell does not exist in /etc/shells. The superuser can change the shell for any user. See also yppasswd and ypchfn. Options -f Behave like ypchfn. -l Behave like ypchsh (default). -p Behave like yppasswd. ypinit ypinit [options] NFS/NIS command. Build and install an NIS database on an NIS server. ypinit can be used to set up a master or a slave server or slave copier. Only a privileged user can run ypinit. Options -c master_name Set up a slave copier database. master_name should be the hostname of an NIS server, either the master server for all the maps or a server on which the database is up-to-date and stable. -m Indicates that the local host is to be the NIS server. -s master_name Set up a slave server database. master_name should be the hostname of an NIS server, either the master server for all the maps or a server on which the database is up-to-date and stable. ypmatch ypmatch [options] key…mname NFS/NIS command. Print value of one or more keys from an NIS map specified by mname. mname may be either a map name or a map nickname. Options -d domain Specify domain other than default domain. -k Before printing value of a key, print key itself, followed by a colon (:). -t Do not translate nickname to map name. #BREAK# -x Display map nickname table listing the nicknames (mnames) known, and map name associated with each nickname. Do not require an mname argument. yppasswd yppasswd [option] [name] NFS/NIS command. Change login password in Network Information Service. Create or change your password, and distribute the new password over NIS. The superuser can change the password for any user. See also ypchfn and ypchsh. Options -f Behave like ypchfn. -l Behave like ypchsh. -p Behave like yppasswd (default). yppasswdd rpc.yppasswdd [option] NFS/NIS command. Server for modifying the NIS password file. yppasswdd handles password change requests from yppasswd. It changes a password entry only if the password represented by yppasswd matches the encrypted password of that entry and if the user ID and group ID match those in the server’s /etc/passwd file. Then it updates /etc/passwd and the password maps on the local server. Option -s Support shadow password functions. yppoll yppoll [options] mapname NFS/NIS command. Determine version of NIS map at NIS server. yppoll asks a ypserv process for the order number and the hostname of the master NIS server for the named map. Options -h host Ask the ypserv process at host about the map parameters. If host is not specified, the hostname of the NIS server for the local host (the one returned by ypwhich) is used. -d domain Use domain instead of the default domain. #BREAK# yppush yppush [options] mapnames NFS/NIS command. Force propagation of changed NIS map. yppush copies a new version of an NIS map, mapname, from the master NIS server to the slave NIS servers. It first constructs a list of NIS server hosts by reading the NIS map ypservers with the -d option’s domain argument. Keys within this map are the ASCII names of the machines on which the NIS servers run. A “transfer map” request is sent to the NIS server at each host, along with the information needed by the transfer agent to call back the yppush. When the attempt has been completed and the transfer agent has sent yppush a status message, the results may be printed to stdout. Normally invoked by /var/yp/Makefile. Options -d domain Specify a domain. -v Verbose — print message when each server is called and for each response. ypserv ypserv [options] NFS/NIS command. NIS server process. ypserv is a daemon process typically activated at system startup time. It runs only on NIS server machines with a complete NIS database. Its primary function is to look up information in its local database of NIS maps. The operations performed by ypserv are defined for the implementor by the NIS protocol specification and for the programmer by the header file . Communication to and from ypserv is by means of RPC calls. Options -d NIS service should go to the DNS for more host information. -localonly Indicates ypserv should not respond to outside requests. Files and directories /var/yp/[domainname]/ Location of NIS databases. /var/yp/Makefile Makefile that is responsible for creating NIS databases. ypset ypset [options] server NFS/NIS command. Point ypbind at a particular server. ypset tells ypbind to get NIS services for the specified domain from the ypserv process running on server. server indicates the NIS server to bind to and can be specified as a name or an IP address. Options -d domain Use domain instead of the default domain. #BREAK# -h host Set ypbind’s binding on host, instead of locally. host can be specified as a name or an IP address. ypwhich ypwhich [options] [host] NFS/NIS command. Return hostname of NIS server or map master. Without arguments, ypwhich cites the NIS server for the local machine. If host is specified, that machine is queried to find out which NIS master it is using. Options -d domain Use domain instead of the default domain. -m map Find master NIS server for a map. No host can be specified with -m. map may be a map name or a nickname for a map. -t mapname Inhibit nickname translation. -x Display map nickname table. Do not allow any other options. ypxfr ypxfr [options] mapname NFS/NIS command. Transfer an NIS map from the server to the local host by making use of normal NIS services. ypxfr creates a temporary map in the directory /etc/yp/domain (where domain is the default domain for the local host), fills it by enumerating the map’s entries, and fetches the map parameters and loads them. If run interactively, ypxfr writes its output to the terminal. However, if it is invoked without a controlling terminal, and if the log file /usr/admin/nislog exists, it appends all its output to that file. Options -b Preserve the resolver flag in the map during the transfer. -C tid prog ipadd port This option is for use only by ypserv. When ypserv invokes ypxfr, it specifies that ypxfr should call back a yppush process at the host with IP address ipadd, registered as program number prog, listening on port port, and waiting for a response to transaction tid. -c Do not send a “Clear current map” request to the local ypserv process. -d domain Specify a domain other than the default domain. -f Force the transfer to occur even if the version at the master is older than the local version. #BREAK# -h host Get the map from host, regardless of what the map says the master is. If host is not specified, ypxfr asks the NIS service for the name of the master and tries to get the map from there. host may be a name or an Internet address in the form h.h.h.h. -S Use only NIS servers running as root and using a reserved port. -s domain Specify a source domain from which to transfer a map that should be the same across domains (such as the services.byname map). zcat zcat [options] [files] Read one or more files that have been compressed with gzip or compress and write them to standard output. Read standard input if no files are specified or if - is specified as one of the files; end input with EOF. zcat is identical to gunzip -c and takes the options -fhLV described for gzip/gunzip. zcmp zcmp [options] files Read compressed files and pass them, uncompressed, to the cmp command, along with any command-line options. If a second file is not specified for comparison, look for a file called file.gz. zdiff zdiff [options] files Read compressed files and pass them, uncompressed, to the diff command, along with any command-line options. If a second file is not specified for comparison, look for a file called file.gz. zdump zdump [options] [zones] System administration command. Dump a list of all known time zones or, if an argument is provided, a specific zone or list of zones. Include each zone’s current time with its name. Options -c year Specify a cutoff year to limit verbose output. Meaningful only with -v. -v Verbose mode. Include additional information about each zone. zforce zforce [names] Rename all gzipped files to filename.gz, unless file already has a .gz extension. #BREAK# zgrep zgrep [options] [files] Uncompress files and pass to grep, along with any command-line arguments. If no files are provided, read from (and attempt to uncompress) standard input. May be invoked as zegrep or zfgrep and will in those cases invoke egrep or fgrep. zic zic [options] [files] System administration command. Create time conversion information files from the file or files specified. If the specified file is -, read information from standard input. Options -d directory Place the newly created files in directory. Default is /usr/local/etc/zoneinfo. -l timezone Specify a timezone to use for local time. zic links the zone information for timezone with the zone localtime. -p timezone Set the default rules for handling POSIX-format environment variables to the zone name specified by timezone. -s Store time values only if they are the same when signed as when unsigned. -v Verbose mode. Include extra error checking and warnings. -y command Check year types with command. Default is yearistype. -L file Consult file for information about leap seconds. The source file(s) for zic should be formatted as a sequence of rule lines, zone lines, and link lines. An optional file containing leap second rules can be specified on the command line. Rule lines describe how time should be calculated. They describe changes in time, daylight savings time, war time, and any other changes that might affect a particular time zone. Zone lines specify which rules apply to a given zone. Link lines link similar zones together. Leap lines describe the exact time when leap seconds should be added or subtracted. Each of these lines is made up of fields. Fields are separated from one another by any number of whitespace characters. Comment lines are preceded by a #. The fields used in each line are listed next. Rule line fields The format of a rule line is: Rule NAME FROM TO TYPE IN ON AT SAVE LETTERS NAME Name this set of rules. #BREAK# FROM Specify the first year to which this rule applies. Gregorian calendar dates are assumed. Instead of specifying an actual year, you may specify minimum or maximum for the minimum or maximum year representable as an integer. TO Specify the last year to which this rule applies. Syntax is the same as for the FROM field. TYPE Specify the type of year to which this rule should be applied. The wildcard - instructs that all years be included. Any given year’s type will be checked with the command given with the -y option or the default yearistype year type. An exit status of 0 is taken to mean the year is of the given type; an exit status of 1 means that it is not of the given type (see -y option). IN Specify month in which this rule should be applied. ON Specify day in which this rule should be applied. Whitespace is not allowed. For example: 1 The 1st firstSun The first Sunday Sun>=3 The first Sunday to occur before or on the 3rd AT Specify the time after which the rule is in effect. For example, you may use 13, 13:00, or 13:00:00 for 1:00 p.m.. You may include one of several suffixes (without whitespace between): s Local standard time. u, g, z Universal time. w Wall clock time (default). SAVE Add this amount of time to the local standard time. Formatted like AT, without suffixes. LETTERS #BREAK# Specify letter or letters to be used in time zone abbreviations (for example, S for EST). For no abbreviation, enter -. Zone line fields The format of a zone line is: Zone NAME GMTOFF RULES/SAVE FORMAT [UNTIL] NAME Time zone name. GMTOFF The amount of hours by which this time zone differs from GMT. Formatted like AT. Negative times are subtracted from GMT; by default, times are added to it. RULES/SAVE Either the name of the rule to apply to this zone or the amount of time to add to local standard time. To make the zone the same as local standard time, specify -. FORMAT How to format time zone abbreviations. Specify the variable part with %s. UNTIL Change the rule for the zone at this date. The next line must specify the new zone information and therefore must omit the string “Zone” and the NAME field. Link line fields The format of a link line is: Link LINK-FROM LINK-TO LINK-FROM The name of the zone that is being linked. LINK-TO An alternate name for the zone that was specified as LINK-FROM. Leap line fields The format of a leap line is: Leap YEAR MONTH DAY HH:MM:SS CORR R/S YEAR MONTH DAY HH:MM:SS Specify when the leap second happened. CORR Uses a + or a - to show whether the second was added or skipped. R/S #BREAK# An abbreviation of Rolling or Stationary to describe whether the leap second should be applied to local wall clock time or to GMT. zmore zmore [files] Similar to more. Uncompress files and print them, one screenful at a time. Works on files compressed with compress, gzip, or pack and with uncompressed files. Commands space Print next screenful. i[number] Print next screenful, or number lines. Set i to number lines. d, ^D Print next i, or 11, lines. iz Print next i lines or a screenful. is Skip i lines. Print next screenful. if Skip i screens. Print next screenful. q, Q, :q, :Q Go to next file, or, if current file is the last, exit zmore. e, q Exit zmore when the prompt “–More–(Next file: file)” is displayed. s Skip next file and continue. = Print line number. i/expr Search forward for ith occurrence (in all files) of expr, which should be a regular expression. Display occurrence, including the two previous lines of context. in Search forward for the ith occurrence of the last regular expression searched for. #BREAK# !command Execute command in shell. If command is not specified, execute last shell command. To invoke a shell without passing it a command, enter !. . Repeat the previous command. znew znew [options] [files] Uncompress .Z files and recompress them in .gz format. Options -9 Optimal (and slowest) compression method. -f Recompress even if filename.gz already exists. -t Test new .gz files before removing .Z files. -v Verbose mode. -K If the original .Z file is smaller than the .gz file, keep it. -P Pipe data to conversion program. This saves disk space. PreviousHomeNext2.8. RPC and XDR Book Index4. Boot Methods Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 4. Boot Methods Contents: The Boot ProcessLILO: The Linux LoaderLoadlin: Booting from MS-DOSDual Booting Linux and Windows NT/2000Boot-time Kernel Optionsinitrd: Using a RAM Disk This chapter describes some techniques for booting your Linux system. Depending on your hardware and whether you want to run any other operating systems, you can configure the system to boot Linux automatically or to provide a choice between several operating systems. Choosing between operating systems is generally referred to as dual booting, but you can boot more than two (e.g., Linux and Windows 95/98/NT/2000). This chapter covers the following topics: l The boot process l LILO: the Linux loader l Loadlin: booting from MS-DOS l Dual booting Linux and Windows NT/2000 l Boot-time kernel options l initrd: using a RAM disk 4.1. The Boot Process Once your Linux system is up and running, booting the system generally is pretty #BREAK# straightforward. But with the wide variety of hardware and software in use, there are many possibilities for configuring your boot process. The three most common choices are: l Boot Linux from a floppy, leaving any other operating system to boot from the hard drive. l Use the Linux Loader, LILO.[2] This is probably the most common method of booting and lets you boot both Linux and other operating systems. [2]LILO is the standard boot program for i386-architecture machines. On the Alpha, the equivalent boot program is called MILO (Mini Loader), and on the SPARC, it is SILO. l Run Loadlin, which is an MS-DOS program that boots Linux from within DOS. Other boot managers that can load Linux are available, but we don’t discuss them in this chapter. We also won’t talk further about booting from a floppy except to say that whatever method you choose for booting, you should be sure to have a working boot floppy available for emergency use. In particular, don’t experiment with the files and options in this chapter unless you have a boot floppy, because any error could leave you unable to boot from the hard disk. On an Intel-based PC, the first sector of every hard disk is known as the boot sector and contains the partition table for that disk and possibly also code for booting an operating system. The boot sector of the first hard disk is known as the master boot record (MBR), because when you boot the system, the BIOS transfers control to a program that lives on that sector along with the partition table. That code is the boot loader, the code that initiates an operating system. When you add Linux to the system, you need to modify the boot loader, replace it, or boot from a floppy disk to start Linux. In Linux, each disk and each partition on the disk is treated as a device. So, for example, the entire first hard disk is known as /dev/hda and the entire second hard disk, if there is one, is /dev/hdb. The first partition of the first hard drive is /dev/hda1, and the second partition is /dev/hda2; the first partition of the second hard drive is /dev/hdb1; and so on. If your drives are SCSI instead of IDE, the naming works the same way except that the devices are /dev/sda, /dev/sda1, and so on. Thus, if you want to specify that the Linux partition is the second partition of the first hard drive (as in the examples in this chapter), you refer to it as /dev/hda2. The rest of the chapter describes the various techniques for booting Linux and the options that you can specify to configure both the boot loader that you use and the Linux kernel. Both LILO and Loadlin let you pass options to the loader and they also let you specify options for the kernel. #BREAK# PreviousHomeNext3. Linux Commands Book Index4.2. LILO: The Linux Loader Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 4.2. LILO: The Linux Loader Once you’ve made the decision to install LILO, you still need to decide how it should be configured. If you want your system to dual boot Linux and Windows 95/98, you can install LILO on the master boot record (MBR) and set it up to let you select the system to boot. Dual booting Linux and Windows NT is not quite as straightforward, because Windows NT has its own loader on the MBR, and it expects to be the one in charge. Therefore, you need to make Linux an option in the NT loader and install LILO in the Linux partition as a secondary boot loader. The result is that the Windows NT loader transfers control to LILO, which then boots Linux. The same applies to Windows 2000, which uses the NT loader. See Section 4.4, “Dual Booting Linux and Windows NT/2000″ later in this chapter for more information. In addition to booting Linux, LILO can boot other operating systems, such as MS-DOS, Windows 95/98, or OS/2. During installation, the major Linux distributions provide the opportunity to install LILO; it can also be installed later if necessary. LILO can be installed on the master boot record (MBR) of your hard drive or as a secondary boot loader on the Linux partition. LILO consists of several pieces, including the boot loader itself, a configuration file (/etc/lilo.conf), a map file (/boot/map) containing the location of the kernel, and the lilo command (/sbin/lilo), which reads the configuration file and uses the information to create or update the map file and to install the files LILO needs. If LILO is installed on the MBR, it replaces the MS-DOS boot loader. If you have problems with your installation or you simply want to uninstall LILO and restore the original boot loader, you can do one of the following: l Boot Linux from a floppy disk and restore the backed-up boot sector: % /sbin/lilo -u l Boot to DOS and run a special version of the fdisk command that rebuilds the MBR: C:> fdisk /mbr One thing to remember about LILO is that it has two aspects: the boot loader and the lilo command. The lilo command configures and installs the boot loader and updates it as necessary. The boot loader is the code that executes at system boot time and boots Linux or another operating system. 4.2.1. The LILO Configuration File The lilo command reads the LILO configuration file, /etc/lilo.conf, to get the information it needs to install LILO. Among other things, it builds a map file containing the locations of all disk sectors needed for booting. Note that any time you change /etc/lilo.conf or rebuild or move a kernel image, you need to rerun lilo to rebuild the map file and update LILO. The configuration file starts with a section of global options, described in the next section. Global options are those that apply to every system boot, regardless of what operating system you are booting. Here is an example of a global section (a hash sign, #, begins a comment): boot = /dev/hda # The boot device is /dev/hda map = /boot/map # Save the map file as /boot/mapinstall = /boot/boot.b # The file to install as the new boot sector #BREAK# prompt # Always display the boot prompttimeout = 30 # Set a 3-second (30 tenths of a second) timeout Following the global section, there is one section of options for each Linux kernel and for each non-Linux operating system that you want LILO to be able to boot. Each of those sections is referred to as an image section, because each boots a different kernel image (shorthand for a binary file containing a kernel) or another operating system. Each Linux image section begins with an image= line. image = /boot/vmlinuz # Linux image file label = linux # Label that appears at the boot prompt root = /dev/hda2 # Location of the root filesystem vga = ask # Always prompt the user for VGA mode read-only # Mount read-only to run fsck for a filesystem check The equivalent section for a non-Linux operating system begins with other= instead of image=. For example: other = /dev/hda1 # Location of the partition label = dos table = /dev/hda # Location of the partition table Put LILO configuration options that apply to all images into the global section of /etc/lilo.conf and options that apply to a particular image into the section for that image. If an option is specified in both the global section and an image section, the setting in the image section overrides the global setting for that image. Here is an example of a complete /etc/lilo.conf file for a system that has the Linux partition on /dev/hda2: ## Global sectionboot=/dev/hda2map=/boot/map delay=30timeout=50promptvga=ask## Image section: For regular Linuximage=/boot/vmlinuz label=linux root=/dev/hda2 install=/boot/boot.b map=/boot/map read-only ## Image section: For testing a new Linux kernelimage=/testvmlinuz label=testlinux root=/dev/hda2 install=/boot/boot.b map=/boot/map read-only optional # Omit image if not available when map is built## Image section: For booting DOSother=/dev/hda1 label=dos loader=/boot/chain.b table=/dev/hda # The current partition table## Image section: For booting Windows 95other=/dev/hda1 #BREAK# label=win95 loader=/boot/chain.b table=/dev/hda 4.2.1.1. Global options In addition to the options listed here, the kernel options append, read-only, read-write, root, and vga (described in Section 4.2.1.3, “Kernel options” later) also can be set as global options. backup=backup-file Copies the original boot sector to backup-file instead of to the file /boot/boot.nnnn, where nnnn is a number that depends on the disk device type. boot=boot-device Sets the name of the device that contains the boot sector. boot defaults to the device currently mounted as root, such as /dev/hda2. Specifying a device such as /dev/hda (without a number) indicates that LILO should be installed in the master boot record; the alternative is to set it up on a particular partition such as /dev/hda2. compact Merges read requests for adjacent disk sectors to speed up booting. Use of compact is particularly recommended when booting from a floppy disk. Use of compact may conflict with linear. default=name Uses the image name as the default boot image. If default is omitted, the first image specified in the configuration file is used. delay=tsecs Specifies, in tenths of a second, how long the boot loader should wait before booting the default image. If serial is set, delay is set to 20 at a minimum. The default is to not wait. disk=device-name Defines parameters for the disk specified by device-name if LILO can’t figure them out. Normally, LILO can determine the disk parameters itself and this option isn’t needed. When disk is specified, it is followed by one or more parameter lines, such as: disk=/dev/sda bios= 0×80 # First disk is usually 0×80, second is usually 0×81 sectors= … heads= … Note that this option is not the same as the disk geometry parameters you can specify with the hd boot command-line option. With disk, the information is given to LILO; with hd, it is passed to the kernel. The parameters that can be specified with disk are listed briefly here. They are described in detail in the LILO User’s Guide, which comes with the LILO distribution. bios=bios-device-code The number the BIOS uses to refer to the device. See the previous example. cylinders=cylinders The number of cylinders on the disk. heads=heads The number of heads on the disk. #BREAK# inaccessible Tells LILO that the BIOS can’t read the disk; used to prevent the system from becoming unbootable if LILO thinks the BIOS can read it. partition=partition-device Starts a new section for a partition. The section contains one variable, start=partition-offset, which specifies the zero-based number of the first sector of the partition: partition=/dev/sda1 start=2048 sectors=sectors The number of sectors per track. disktab=disktab-file This option has been superseded by the disk= option. fix-table If set, allows lilo to adjust 3D addresses (addresses specified as sector/head/cylinder) in partition tables. This is sometimes necessary if a partition isn’t track-aligned and another operating system such as MS-DOS is on the same disk. See the lilo.conf manpage for details. force-backup=backup-file Like backup but overwrites an old backup copy if one exists. ignore-table Tells lilo to ignore corrupt partition tables. install=boot-sector Installs the specified file as the new boot sector. If install is omitted, the boot sector defaults to /boot/boot.b. lba32 Generates 32-bit Logical Block Addresses instead of sector/head/cylinder addresses, allowing booting from any partition on hard disks greater than 8.4GB (i.e., it removes the 1024-cylinder limit). Requires BIOS support for the EDD packet call interface[3] and at least LILO Version 21-4. [3]If your BIOS is dated after 1998, it should include EDD packet call interface support. linear Generates linear sector addresses, which do not depend on disk geometry, instead of 3D (sector/head/cylinder) addresses. If LILO can’t determine your disk’s geometry itself, you can try using linear; if that doesn’t work, then you need to specify the geometry with disk=. Note, however, that linear sometimes doesn’t work with floppy disks, and it may conflict with compact. lock #BREAK# Tells LILO to record the boot command line and use it as the default for future boots until it is overridden by a new boot command line. lock is useful if there is a set of options that you need to enter on the boot command line every time you boot the system. map=map-file Specifies the location of the map file. Defaults to /boot/map. message=message-file Specifies a file containing a message to be displayed before the boot prompt. The message can include a formfeed character (Ctrl-L) to clear the screen. The map file must be rebuilt by rerunning the lilo command if the message file is changed or moved. The maximum length of the file is 65,535 bytes. nowarn Disables warning messages. optional Specifies that any image that is not available when the map is created should be omitted and not offered as an option at the boot prompt. Like the per-image option optional but applies to all images. password=password Specifies a password that the user is prompted to enter when trying to load an image. The password is not encrypted in the configuration file, so if passwords are used, permissions should be set so that only the superuser is able to read the file. This option is like the per-image version, except that all images are password-protected and they all have the same password. prompt Automatically displays the boot prompt without waiting for the user to press the Shift, Alt, or Scroll Lock key. Note that setting prompt without also setting timeout prevents unattended reboots. restricted Can be used with password to indicate that a password needs to be entered only if the user specifies parameters on the command line. Like the per-image restricted option but applies to all images. serial=parameters Allows the boot loader to accept input from a serial line as well as from the keyboard. Sending a break on the serial line corresponds to pressing a Shift key on the console to get the boot loader’s attention. All boot images should be password-protected if serial access is insecure (e.g., if the line is connected to a modem). Setting serial automatically raises the value of delay to 20 (i.e., 2 seconds) if it is less than that. The parameter string parameters has the following syntax: port[,bps[parity[bits]]] For example, to initialize COM1 with the default parameters: serial=0,2400n8 The parameters are: #BREAK# port The port number of the serial port. The default is 0, which corresponds to COM1 (/dev/ttys0). The value can be one of 0 through 3, for the four possible COM ports. bps The baud rate of the serial port. Possible values of bps are 110, 300, 1200, 2400, 4800, 9600, 19200, and 38400. The default is 2400 bps. parity The parity used on the serial line. Parity is specified as: n or N for no parity, e or E for even parity, and o or O for odd parity. However, the boot loader ignores input parity and strips the 8th bit. bits Specifies whether a character contains 7 or 8 bits. Default is 8 with no parity and 7 otherwise. timeout=tsecs Sets a timeout (specified in tenths of a second) for keyboard input. If no key has been pressed after the specified time, the default image is booted automatically. timeout is also used to determine when to stop waiting for password input. The default timeout is infinite. verbose=level Turns on verbose output, where higher values of level produce more output. If -v is also specified on the lilo command line, the level is incremented by 1 for each occurrence of -v. The maximum verbosity level is 5. 4.2.1.2. Image options The following options are specified for a particular image. alias=name Provides an alternate name for the image that can be used instead of the name specified with the label option. image=pathname Specifies the file or device containing the boot image of a bootable Linux kernel. Each per-image section that specifies a bootable Linux kernel starts with an image option. See also the range option. label=name Specifies the name that is used for the image at the boot prompt. Defaults to the filename of the image file (without the path). loader=chain-loader For a non-Linux operating system, specifies the chain loader to which LILO should pass control for booting that operating system. The default is /boot/chain.b. If the system will be booted from a drive that is neither the first hard disk or a floppy, the chain loader must be specified. lock #BREAK# Like lock as described in the global options section; it can also be specified in an image section. optional Specifies that the image should be omitted if it is not available when the map is created by the lilo command. Useful for specifying test kernels that are not always present. password=password Specifies that the image is password-protected and provides the password that the user is prompted for when booting. The password is not encrypted in the configuration file, so if passwords are used, only the superuser should be able to read the file. range=sectors Used with the image option, when the image is specified as a device (e.g., image=/dev/fd0), to indicate the range of sectors to be mapped into the map file. sectors can be given as the range start-end or as start+number, where start and end are zero-based sector numbers and number is the increment beyond start to include. If only start is specified, only that one sector is mapped. For example: image = /dev/fd0 range = 1+512 # take 512 sectors, starting with sector 1 restricted Specifies that a password is required for booting the image only if boot parameters are specified on the command line. table=device Specifies, for a non-Linux operating system, the device that contains the partition table. If table is omitted, the boot loader does not pass partition information to the operating system being booted. Note that /sbin/lilo must be rerun if the partition table is modified. This option cannot be used with unsafe. unsafe Can be used in the per-image section for a non-Linux operating system to indicate that the boot sector should not be accessed when the map is created. If unsafe is specified, then some checking isn’t done, but the option can be useful for running the lilo command without having to insert a floppy disk when the boot sector is on a fixed-format floppy disk device. This option cannot be used with table. 4.2.1.3. Kernel options The following kernel options can be specified in /etc/lilo.conf as well as on the boot command line: append=string Appends the options specified in string to the parameter line passed to the kernel. This typically is used to specify certain hardware parameters. For example, if your system has more than 64 MB of memory (i.e., more than your BIOS can recognize), you can use append: append = “mem=128M” initrd=filename Specifies the file to load into /dev/initrd when booting with a RAM disk. See also the options load_ramdisk (in #BREAK# Section 4.5, “Boot-time Kernel Options”), prompt_ramdisk, ramdisk_size, and ramdisk_start (in Section 4.6, “initrd: Using a RAM Disk”). literal=string Like append but replaces all other kernel boot options. noinitrd Preserves the contents of /dev/initrd so they can be read once after the kernel is booted. prompt_ramdisk=n Specifies whether the kernel should prompt you to insert the floppy disk that contains the RAM disk image, for use during Linux installation. Values of n are: 0 Don’t prompt. Usually used for an installation in which the kernel and the RAM disk image both fit on one floppy. 1 Prompt. This is the default. ramdisk=size Obsolete; use only with kernels older than Version 1.3.48. For newer kernels, see the option load_ramdisk in Section 4.5, “Boot-time Kernel Options” as well as prompt_ramdisk, ramdisk_size, and ramdisk_start, elsewhere in this section. ramdisk_size=n Specifies the amount of memory, in kilobytes, to be allocated for the RAM disk. The default is 4096, which allocates 4 megabytes. ramdisk_start=offset Used for a Linux installation in which both the kernel and the RAM disk image are on the same floppy. offset indicates the offset on the floppy where the RAM disk image begins; it is specified in kilobytes. root=root-device Specifies the device that should be mounted as root. If the special name current is used as the value, the root device is set to the device on which the root filesystem currently is mounted. Defaults to the root-device setting contained in the kernel image. vga=mode Specifies the VGA text mode that should be selected when booting. mode defaults to the VGA mode setting in the kernel image. The values are case-insensitive. They are: ask Prompts the user for the text mode. Pressing Enter in response to the prompt displays a list of the available modes. extended (or ext) Selects 80×50 text mode. normal Selects normal 80×25 text mode. number Use the text mode that corresponds to number. A list of available modes for your video card can be obtained by booting with vga=ask and pressing Enter. #BREAK# 4.2.2. The lilo Command You need to run the lilo command to install the LILO boot loader and to update it whenever the kernel changes or to reflect changes to /etc/lilo.conf. The path to the lilo command is usually /sbin/lilo. The syntax of the command is: lilo [options] Some of the options correspond to /etc/lilo.conf keywords: Configuration Keyword Command Option boot=bootdev -b bootdev compact -c delay=tsecs -d tsecs default=label -D label disktab=file -f file install=bootsector -i bootsector lba32 -L linear -l map=mapfile -m mapfile fix-table -P fix ignore-table -P ignore backup=file -s file force-backup=file -S file verbose=level -v These options should be put in the configuration file whenever possible; putting them on the lilo command line instead of in /etc/lilo.conf is now deprecated. The next section describes those options that can be given only on the lilo command line; the others are described earlier in this section. 4.2.3. lilo Command Options The following list describes those lilo command options that are available only on the command line. Multiple options are given separately: % lilo -q -v -C config-file Specifies an alternative to the default configuration file (/etc/lilo.conf). lilo uses the configuration file to determine what files to map when it installs LILO. -I label #BREAK# Prints the path to the kernel specified by label to standard output or an error message if no matching label is found. For example: % lilo -I linux/boot/vmlinuz-2.0.34-0.6 -q Lists the currently mapped files. lilo maintains a file (by default /boot/map), containing the name and location of the kernel(s) to boot. Running lilo with this option prints the names of the files in the map file to standard output, as in this example (in which the asterisk indicates that linux is the default): % lilo -qlinux * test -r root-directory Specifies that before doing anything else, lilo should chroot to the indicated directory. Used for repairing a setup from a boot floppy — you can boot from a floppy but have lilo use the boot files from the hard drive. For example, if you issue the following commands, lilo will get the files it needs from the hard drive: % mount /dev/hda2 /mnt% lilo -r /mnt -R command-line Sets the default command for the boot loader the next time it executes. The command executes once and then is removed by the boot loader. This option typically is used in reboot scripts, just before calling shutdown -r. -t Indicates that this is a test. Does not really write a new boot sector or map file. Can be used with -v to find out what lilo would do during a normal run. -u device-name Uninstalls lilo by restoring the saved boot sector from /boot/boot.nnnn, after validating it against a timestamp. device-name is the name of the device on which LILO is installed, such as /dev/hda2. -U device-name Like -u but does not check the timestamp. -V Prints the lilo version number. PreviousHomeNext4. Boot Methods Book Index4.3. Loadlin: Booting from MS-DOS Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 4.3. Loadlin: Booting from MS-DOS Loadlin is a Linux boot loader that you run from within a bootable MS-DOS partition; the system must be in real DOS mode, not in an MS-DOS window running under Windows. No installation is required; you just need to copy the executable file loadlin.exe from the Loadlin distribution to your MS-DOS partition.[4] You also need a compressed Linux kernel (e.g., vmlinuz), which you can load from a floppy, from the DOS partition, or from a RAM disk. For example: [4]If Loadlin didn’t come with your Linux distribution, you can download it from any of the major Linux sites, such as the Metalab site at http://metalab.unc.edu/pub/Linux. C:> loadlin c:vmlinuz root=/dev/hda2 This example loads the Linux kernel image vmlinuz, passing it the boot parameter root=/dev/hda2, telling the kernel that the Linux root partition is /dev/hda2. (If you are using a RAM disk, see Section 4.6, “initrd: Using a RAM Disk” later in this chapter.) If you want to use Loadlin with Windows 95/98, see the Loadlin User Guide and the Loadlin+Win95 mini-HOWTO for how to do that. Note that if your disk uses the FAT32 filesystem, the standard techniques for using Loadlin and Windows 95 won’t work; if this is the case or if you aren’t sure whether you have FAT16 or FAT32, it’s important to read the mini-HOWTO before you proceed. Loadlin can be run directly from the DOS prompt, as in the example, or it can be invoked from CONFIG.SYS or AUTOEXEC.BAT. Like LILO, Loadlin takes both options that direct its operation and options (also referred to as parameters) that it passes to the kernel. There are two forms of the Loadlin syntax: LOADLIN @paramsLOADLIN [zimage_file] [options] [boot_params] #BREAK# 4.3.1. Using a Parameter File In the first form of the preceding syntax, params is a DOS file that contains the options you want Loadlin to run with. The Loadlin distribution comes with a sample parameter file, test.par, that you can use as a basis for creating your own. Each line in a parameter file contains one parameter. If you want to specify the name of the Linux kernel to use (the image= parameter), it must be the first entry in the file. Comments start with a hash sign (#). The entries in the parameter file can be overridden or appended on the command line. For example, to override the value of vga set in the parameter file: C:> LOADLIN @myparam vga=normal 4.3.2. Putting Parameters on the Command Line In the second form of the preceding Loadlin syntax, zimage_file is the name of a Linux kernel to run, followed by a list of Loadlin options and/or boot options. Specifying LOADLIN with no parameters gives a help message listing the Loadlin options and some of the possible kernel boot options. The message is long enough that you probably want to pipe the output through a pager like more: C:> LOADLIN | more The Loadlin options are: -clone Bypasses certain checks — read the LOADLIN User Guide that comes with the Loadlin distribution before using. -d file Debug mode. Like -t but sends output to file as well as to standard output. -dskreset Causes disks to be reset after loading but before booting Linux. -noheap For use by serious Linux hackers only; disables use of the setup heap. -t #BREAK# Test mode. Goes through the loading process but doesn’t actually start Linux. Also sets -v. -txmode Sets the screen to text mode (80×25) on startup. -v Verbose. Prints parameter and configuration information to standard output. -wait=nn After loading, waits nn (DOS) ticks before booting Linux. In addition to these Loadlin options, the help message prints a number of kernel boot options that you can specify. The boot options that it prints are only a few of the many available boot options. See also the BootPrompt-HOWTO for a more complete list. PreviousHomeNext4.2. LILO: The Linux Loader Book Index4.4. Dual Booting Linux and Windows NT/2000 Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 4.4. Dual Booting Linux and Windows NT/2000 As we said earlier, when you run Windows NT, its boot loader expects to be the one in charge; therefore, the normal way to dual boot Windows NT and Linux is to add Linux as an option on the NT boot menu. The information in this section also applies to Windows 2000, which uses the NT loader. To accomplish this, you need to provide the NT loader with a copy of the Linux boot sector. Here’s how you do that on a computer running Windows NT with an NTFS filesystem (note that Windows NT should be installed on your system already). See the NT OS Loader+Linux mini-HOWTO for more information and other alternatives. You should have a Linux boot floppy available so that, if necessary, you can boot Linux before the NT boot loader has been modified. You also should have a DOS-formatted floppy to transfer the boot sector to the Windows NT partition. If LILO is already installed, you may need to modify /etc/lilo.conf as described later. Otherwise, you’ll either install LILO as part of the Linux installation, or you can install it with the QuickInst script that comes with LILO. Once LILO is installed, and you have a configuration file, you can set up the system for dual booting. Note that the following instructions assume your Linux partition is on /dev/hda2. If Linux is on another partition, be sure to replace /dev/hda2 in the following examples with the correct partition. 1. Specify the Linux root partition as your boot device. If you are editing /etc/lilo.conf manually, your entry will look like this: boot=/dev/hda2 and will be the same as the root= entry. 2. Run the lilo command to install LILO on the Linux root partition. 3. At this point, if you need to reboot Linux, you’ll have to use the boot floppy, because the NT loader hasn’t been set up yet to boot Linux. 4. From Linux, run the dd command to make a copy of the Linux boot sector: % dd if=/dev/hda2 of=/bootsect.lnx bs=512 count=1 This command copies one block, with a blocksize of 512 bytes, from the input file /dev/hda2 to the output file /bootsect.lnx. (The output filename can be whatever makes sense to you; it doesn’t have to be bootsect.lnx.) 5. Copy bootsect.lnx to a DOS-formatted floppy disk: % mount -t msdos /dev/fd0 /mnt% cp /bootsect.lnx /mnt% umount /mnt 6. Reboot the system to Windows NT and copy the boot sector from the floppy disk to the hard disk. For example, using the command line to copy the file: C:> copy a:bootsect.lnx c:bootsect.lnx #BREAK# It doesn’t matter where on the hard drive you put the file because you’ll tell the NT loader where to find it in step 8. 7. Modify the attributes of the file boot.ini[5] to remove the system and read-only attributes so you can edit it: [5]boot.ini is the Windows NT counterpart to /etc/lilo.conf. It defines what operating systems the NT loader can boot. C:> attrib -s -r c:boot.ini 8. Edit boot.ini with a text editor to add the line: C:bootsect.lnx=”Linux” This line adds Linux to the boot menu and tells the Windows NT boot loader where to find the Linux boot sector. You can insert the line anywhere in the [operating systems] section of the file. Its position in the file determines where it will show up on the boot menu when you reboot your computer. Adding it at the end, for example, results in a boot.ini file that looks something like this (the second multi(0) entry is wrapped to fit in the margins of this page): [boot loader] timeout=30default=multi(0)disk(0)rdisk(0)partition(1)WINNT[operating systems] multi(0)disk(0)rdisk(0)partition(1)WINNT=”Windows NT Server Version 4.00″ multi(0)disk(0)rdisk(0)partition(1)WINNT=”Windows NT Server Version 4.00 [VGA mode]” /basevideo /sosC:bootsect.lnx=”Linux” If you want Linux to be the default operating system, modify the default= line to say: default=C:bootsect.lnx 9. Rerun attrib to restore the system and read-only attributes: C:> attrib +s +r c:boot.ini Now you can shut down Windows NT and reboot; NT will prompt you with a menu that looks something like this: OS Loader V4.00Please select the operating system to start: Windows NT Workstation Version 4.00Windows NT Workstation Version 4.00 [VGA mode] Linux Select Linux, and the NT loader reads the Linux boot sector and transfers control to LILO, on the Linux partition. If you later modify /etc/lilo.conf or rebuild the kernel, you need to rerun the lilo command, create a new bootsect.lnx file, and replace the version of bootsect.lnx on the Windows NT partition with the new version. That is, you need to rerun steps 2-6. NOTE If you have any problems or you simply want to remove LILO later, you can reverse the installation procedure: boot to Windows NT, change the system and read-only attributes on boot.ini, reedit boot.ini to remove the Linux entry, save the file, restore the system and read-only attributes, and remove the Linux boot sector from the NT partition. #BREAK# PreviousHomeNext4.3. Loadlin: Booting from MS-DOS Book Index4.5. Boot-time Kernel Options Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 4.5. Boot-time Kernel Options The Loadlin and LILO sections of this chapter described some of the options you can specify when you boot Linux. There are many more options that can be specified. This section touches on the ways to pass options to the kernel and then describes some of the kinds of parameters you might want to use. The parameters in this section affect the kernel and therefore apply regardless of which boot loader you use. As always with Unix systems, there are a number of choices for the boot process itself. If you are using Loadlin, you can pass parameters to the kernel on the command line or in a file. If LILO is your boot loader, you can add to or override the parameters specified in /etc/lilo.conf during the boot process as follows: l If prompt is set in /etc/lilo.conf, LILO always presents the boot prompt and waits for input. At the prompt, you can choose the operating system to be booted. If you choose Linux, you also can specify parameters. l If prompt isn’t set, when the word “LILO” appears, press Control, Shift, or Alt, and the boot prompt appears. You also can press the Scroll Lock key before LILO is printed and not have to wait poised over the keyboard for the right moment. l At the boot prompt, specify the system you want to boot or press Tab to get a list of the available choices. You then can enter the name of the image to boot. For example: LILO boot:
linux test dosboot: linux You also can add boot command options: boot: linux single l If you don't provide any input, LILO waits the amount of time specified in the delay parameter and then boots the default operating system with the default parameters as #BREAK# set in /etc/lilo.conf. Some of the boot parameters have been mentioned earlier. Many of the others are hardware- specific and are too numerous to mention here. For a complete list of parameters and a discussion of the booting process, see the BootPrompt-HOWTO. Some of the parameters not shown earlier that you might find useful are listed next; many more are covered in the HOWTO. Most of the following parameters are used to provide information or instructions for the kernel, rather than to LILO. debug Prints all kernel messages to the console. hd=cylinders,heads,sectors Specifies the hard drive geometry to the kernel. Useful if Linux has trouble recognizing the geometry of your drive, especially if it's an IDE drive with more than 1024 cylinders. load_ramdisk=n Tells the kernel whether to load a RAM disk image for use during Linux installation. Values of n are: 0 Don't try to load the image. This is the default. 1 Load the image from a floppy disk to the RAM disk. mem=size Specifies the amount of system memory installed. Useful if your BIOS reports memory only up to 64 MB and your system has more memory installed. Specify as a number with M or k (case-insensitive) appended: mem=128M Because mem would have to be included on the command line for every boot, it often is specified on a command line saved with lock or with append to be added to the parameters passed to the kernel. #BREAK# noinitrd When set, disables the two-stage boot and preserves the contents of /dev/initrd so the data is available after the kernel has booted. /dev/initrd can be read only once, and then its contents are returned to the system. number Starts Linux at the runlevel specified by number. A runlevel is an operating state that the system can be booted to, such as a multiuser system or a system configuration running the X Window System. A runlevel is generally one of the numbers from 1 to 6; the default usually is 3. The runlevels and their corresponding states are defined in the file /etc/inittab. See the manpage for /etc/inittab for more information. ro Mounts the root filesystem read-only. Used for doing system maintenance, such as checking the filesystem integrity, when you don't want anything written to the filesystem. rw Mounts the root filesystem read-write. If neither ro nor rw is specified, the default value (usually rw) stored in the kernel image is used. single Starts Linux in single-user mode. This option is used for system administration and recovery. It gives you a root prompt as soon as the system boots, with minimal initialization. No other logins are allowed. PreviousHomeNext4.4. Dual Booting Linux and Windows NT/2000 Book Index4.6. initrd: Using a RAM Disk Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 4.6. initrd: Using a RAM Disk Modern Linux distributions use a modular kernel, which allows modules to be added without requiring that the kernel be rebuilt. If your root filesystem is on a device whose driver is a module, as is frequently true of SCSI disks, you can use the initrd facility, which provides a two-stage boot process, to first set up a temporary root filesystem in a RAM disk containing the modules you need to add (e.g., the SCSI driver) and then load the modules and mount the real root filesystem. The RAM disk containing the temporary filesystem is the special device file /dev/initrd. Before you can use initrd, both RAM disk support (CONFIG_BLK_DEV_RAM=y) and initial RAM disk support (CONFIG_BLK_DEV_INITRD=y) must be compiled into the Linux kernel. Then you need to prepare the normal root filesystem and create the RAM disk image. Your Linux distribution may have utilities to do some of the setup for you; for example, the Red Hat distribution comes with the mkinitrd command, which builds the initrd image. For detailed information, see the initrd manpage and the file initrd.txt (the path may vary but is usually something like /usr/src/linux/Documentation/initrd.txt). Once your Linux system has been set up for initrd, you can do one of the following, depending on which boot loader you are using: l If LILO is your boot loader, add the initrd option to the appropriate image section: image = /vmlinuz initrd = /boot/initrd # The file to load as the contents of /dev/initrd ... Run the /sbin/lilo command, and you can reboot with initrd. l If you are using Loadlin, add the initrd option to the command line: loadlin c:linuxvmlinuz initrd=c:linuxinitrd PreviousHomeNext4.5. Boot-time Kernel Options Book Index5. Red Hat and Debian Package Managers Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 5. Red Hat and Debian Package Managers Contents: The Red Hat Package ManagerThe Debian Package Manager This chapter describes the two major Linux packaging systems, the Red Hat Package Manager (RPM) and the Debian GNU/Linux Package Manager. When you want to install applications on your Linux system, most often you'll find a binary or a source package containing the application you want, instead of (or in addition to) a .tar.gz file. A package is a file containing the files necessary to install an application. But note that while the package contains the files you need for installation, the application might require the presence of other files or packages that are not included, such as particular libraries (and even specific versions of the libraries), in order to be able to run. Such requirements are known as dependencies. Package management systems offer many benefits. As a user, you may find you want to query the package database to find out what packages are installed on the system and their versions. As a system administrator, you need tools to install and manage the packages on your system. And, if you are also a developer, you need to know how to build a package for distribution. Among other things, package managers: l Provide tools for installing, updating, removing, and managing the software on your system. l Let you install new or upgraded software directly across a network. l Tell you what software package a particular file belongs to or what files a package contains. l Maintain a database of packages on the system and their state, so you can find out what packages or versions are installed on your system. l Provide dependency checking, so you don't mess up your system with incompatible software. l Provide PGP, MD5, or other signature verification tools. l Provide tools for building packages. Any user can list or query packages. However, installing, upgrading, or removing packages generally requires superuser privileges. This is because the packages normally are installed in systemwide directories that are #BREAK# writable only by root. Sometimes you can specify an alternate directory, to install, for example, a package into your home directory or into a project directory where you have write permission. Both RPM and the Debian Package Manager back up old files before installing an updated package. Not only does this let you go back if there is a problem, but also if you've made changes (to configuration files, for example), they aren't completely lost. 5.1. The Red Hat Package Manager The Red Hat Package Manager (RPM) is a freely available packaging system for software distribution and installation. In addition to Red Hat and Red Hat-based distributions, both SuSE and Caldera are among the Linux distributions that use RPM. Using RPM is straightforward. A single command, rpm, has options to perform all the package functions. For example, to find out if the Emacs editor is installed on your system, you could say: % rpm -q emacsemacs-20.4-4 In addition, the GNOME-RPM program provides an X-based graphical frontend to RPM (that can be run even if you are not running GNOME). This section describes the rpm command and then the gnorpm command that runs GNOME-RPM. 5.1.1. The rpm Command RPM packages are built, installed, and queried with the rpm command. RPM package names usually end with a .rpm extension. rpm has a set of modes, each with its own options. The format of the rpm command is: rpm [options] [packages] With a few exceptions, as noted in the lists of options that follow, the first option specifies the rpm mode (e.g., install, query, update, build, etc.), and any remaining options affect that mode. In the option descriptions that refer to packages, you'll sometimes see them specified as package-name and sometimes as package-file. The package name is the name of the program or application, such as gif2png. The package file is the name of the RPM file: gif2png-2.2.5-1.i386.rpm. RPM provides a configuration file for specifying frequently used options. The system configuration file is usually /etc/rpmrc, and users can set up their own $HOME/.rpmrc file. You can use the --showrc option to show the values RPM will use for all the options that may be set in an rpmrc file: rpm --showrc The rpm command includes FTP and HTTP clients, so you can specify an ftp:// or http:// URL to install or query a package across the Internet. You can use an FTP or HTTP URL wherever package-file is specified in the commands presented here. Any user can query the RPM database. Most of the other functions require superuser privileges. 5.1.1.1. General options The following options can be used with all modes: #BREAK# --dbpath path Use path as the path to the RPM database. --ftpport port Use port as the FTP port. --ftpproxy host Use host as a proxy server for all transfers. Specified if you are FTPing through a firewall system that uses a proxy. --help Print a long usage message (running rpm with no options gives a shorter usage message). --justdb Update only the database; don't change any files. --pipe command Pipe the rpm output to command. --quiet Display only error messages. --rcfile filename Use filename as the configuration file instead of the system configuration file /etc/rpmrc or $HOME/.rpmrc. --root dir Perform all operations within directory dir. --version Print the version number of rpm. -vv Print debugging information. 5.1.1.2. Install, upgrade, and freshen options Install or upgrade an RPM package. The syntax of the install command is: rpm -i [install-options] package_file ... #BREAK# rpm --install [install-options] package_file ... To install a new version of a package and remove an existing version at the same time, use the upgrade command instead: rpm -U [install-options] package_file ... rpm --upgrade [install-options] package_file ... One feature of -U is that if the package doesn't already exist on the system, it acts like -i and installs it. To prevent that behavior, you can freshen a package instead; in that case, rpm upgrades the package only if an earlier version is already installed. The freshen syntax is: rpm -F [install-options] package_file ... rpm --freshen [install-options] package_file ... Installation and upgrade options are: --allfiles Install or upgrade all files. --badreloc Used with --relocate to force relocation even if the package is not relocatable. --excludedocs Don't install any documentation files. --excludepath path Don't install any file whose filename begins with path. --force Force the installation. Equivalent to using --replacepkgs, --replacefiles, and --oldpackage. -h, --hash Print 50 hash marks as the package archive is unpacked. Use with --version for a nicer display. --ignorearch Install even if the binary package is intended for a different architecture. --ignoreos Install binary package even if the operating systems don't match. --ignoresize Don't check disk space availability before installing. #BREAK# --includedocs Install documentation files. This is needed only if excludedocs: 1 is specified in an rpmrc file. --nodeps Don't check whether this package depends on the presence of other packages. --noorder Don't reorder packages to satisfy dependencies before installing. --noscripts Don't execute any preinstall or postinstall scripts. --notriggers Don't execute any scripts triggered by package installation. --oldpackage Allow an upgrade to replace a newer package with an older one. --percent Print percent-completion messages as files are unpacked. --prefix path Set the installation prefix to path for relocatable packages. --replacefiles Install the packages even if they replace files from other installed packages. --replacepkgs Install the packages even if some of them are already installed. --test Go through the installation to see what it would do, but don't actually install the package. 5.1.1.3. Query options The syntax for the query command is: rpm -q[information-options] [package-options] rpm --query[information-options] [package-options] #BREAK# There are two subsets of query options: package selection options that determine what packages to query and information selection options that determine what information to provide. 5.1.1.3.1. Package selection options package_name Query the installed package package_name. -a, --all Query all installed packages. -f file, --file file Find out what package owns file. -g group, --group group Find out what packages have group group. -p package_file Query the uninstalled package package_file. --querybynumber num Query the numth database entry. Primarily useful for debugging. -qf, --queryformat num Specify the format for displaying the query output, using tags to represent different types of data (e.g., NAME, FILENAME, DISTRIBUTION). The format specification is a variation of the standard printf formatting. (Use --querytags in Section 5.1.1.8, "Miscellaneous options" to view a list of available tags. --specfile specfile Query specfile as if it were a package. --triggeredby pkg List packages that trigger installation of package pkg. --whatrequires capability List packages that require the given capability to function. --whatprovides capability List packages that provide the given capability. 5.1.1.3.2. Information selection options -c, --configfiles List configuration files in the package. #BREAK# --changelog Display the log of change information for the package. -d, --docfiles List documentation files in the package. --dump Dump information for each file in the package. This option must be used with at least one of -l, -c, or -d. The output includes the following information in this order: path size mtime md5sum mode owner group isconfig isdoc rdev symlink --filesbypkg List all files in each package. -i Display package information, including the name, version, and description. -l, --list List all files in the package. --last List packages by install time, with the latest packages listed first. --provides List the capabilities this package provides. -R, --requires List any packages this package depends on. -s, --state List each file in the package and its state. The possible states are normal, not installed, or replaced. --scripts List any package-specific shell scripts used during installation and uninstallation of the package. 5.1.1.4. Uninstall options The syntax for the uninstall command is: #BREAK# rpm -e package_namerpm --erase package_name The uninstall options are: --allmatches Remove all versions of the package. Only one package should be specified; otherwise, an error results. --nodeps Don't check dependencies before uninstalling the package. --noscripts Don't execute any preuninstall or postuninstall scripts. --notriggers Don't execute any scripts triggered by the removal of this package. --test Don't really uninstall anything; just go through the motions. 5.1.1.5. Verify options The syntax for the verify command is: rpm -V|-y| -- verify[package-selection-options] Verify mode compares information about the installed files in a package with information about the files that came in the original package and displays any discrepancies. The information compared includes the size, MD5 sum, permissions, type, owner, and group of each file. Uninstalled files are ignored. The package selection options include those available for query mode, as well as the following: --nofiles Ignore missing files. --nomd5 Ignore MD5 checksum errors. --nopgp Ignore PGP checking errors. The output is formatted as an eight-character string, possibly followed by a "c" to indicate a configuration file, and then the filename. Each of the eight characters in the string represents the result of comparing one file attribute to the value of that attribute from the RPM database. A period (.) indicates that the file passed that test. The #BREAK# following characters indicate failure of the corresponding test: 5 MD5 sum D Device G Group L Symlink M Mode (includes permissions and file type) S File size T Mtime U User 5.1.1.6. Database rebuild options The syntax of the command to rebuild the RPM database is: rpm --rebuilddb [options] You also can build a new database: rpm --initdb [options] The options available with the database rebuild mode are the --dbpath and --root options described earlier under Section 5.1.1.1, "General options". 5.1.1.7. Signature check options RPM packages may have a PGP signature built into them. PGP configuration information is read from /etc/rpmrc. The syntax of the signature-check mode is: rpm --checksig package_file... rpm -K package_file... The signature-checking options are: --nogpg Don't check any GPG signatures. --nomd5 Don't check any MD5 signatures. --nopgp Don't check any PGP signatures. #BREAK# Two other options let you add signatures to packages: --addsign binary-pkgfile... Generate and append new signatures to those that already exist in the specified binary packages. --resign binary-pkgfile... Generate and insert new signatures in the specified binary packages, removing any existing signatures. 5.1.1.8. Miscellaneous options Several additional rpm options are available: --querytags Print the tags available for use with the --queryformat option in query mode. --setgids packages Set file owner and group of the specified packages to those in the database. --setperms packages Set file permissions of the specified packages to those in the database. --showrc Show the values rpm will use for all options that can be set in an .rpmrc file. 5.1.1.9. FTP/HTTP options The following options are available for use with ftp:// and http:// URLs in install, update, and query modes: --ftpport port Use port for making an FTP connection on the proxy FTP server instead of the default port. Same as specifying the macro _ftpport. --ftpproxy host Use host as the proxy server for FTP transfers through a firewall that uses a proxy. Same as specifying the macro _ftpproxy. --httpport port Use port for making an HTTP connection on the proxy HTTP server instead of the default port. Same as specifying the macro _httpport. --httpproxy host #BREAK# Use host as a proxy server for HTTP transfers. Same as specifying the macro _httpproxy. 5.1.1.10. Build options The syntax for the build options is: rpm -[b|t]step [build-options] spec-file ... Specify -b to build a package directly from a spec file or -t to open a tarred gzipped file and use its spec file. Both forms take the following single-character step arguments: p Perform the prep stage, unpacking source files and applying patches. l Do a list check, expanding macros in the files section of the spec file and verifying that each file exists. c Perform the build stage. Done after the prep stage; generally equivalent to doing a make. i Perform the install stage. Done after the prep and build stages; generally equivalent to doing a make install. b Build a binary package. Done after prep, build, and install. s Build a source package. Done after prep, build, and install. a Build both binary and source packages. Done after prep, build, and install. The following additional options can be used when building an rpm file: --buildarch arch --buildos os For use with pre-3.0 versions of RPM. Build the package for architecture arch or the operating system os. Replaced in 3.0 with --target. --buildroot dir Override the BuildRoot tag with dir when building the package. #BREAK# --clean Clean up (remove) the build files after the package has been made. --rmsource Remove the source files and the spec file when the build is done. Can be used as a standalone option with rpm to clean up files separately from creating the packages. --short-circuit Can be used with -bc and -bi to skip previous stages. --sign Add a PGP signature to the package. --target platform When building the package, set the macros _target, _target_arch, and _target_os to the value indicated by platform. --test Go through the motions, but don't execute any build stages. Used for testing spec files. --timecheck Set the timecheck age (the maximum age in seconds of a file being packaged). Set to 0 to disable. Two other options can be used standalone with rpm to recompile or rebuild a package: --rebuild source-pkgfile... Like --recompile, but also build a new binary package. Remove the build directory, the source files, and the spec file once the build is complete. --recompile source-pkgfile... Install the named source package, and prep, compile, and install the package. 5.1.1.11. RPM examples Query the RPM database to find Emacs-related packages: % rpm -q -a | grep emacs Query an uninstalled package, printing information about the package, and list the files it contains: % rpm -qpil ~/downloads/bash2-doc-2.03-8.i386.rpm Install a package (assumes superuser privileges): #BREAK# % rpm -i sudo-1.5.3-6.i386.rpm 5.1.2. GNOME-RPM GNOME-RPM is a graphical user frontend to rpm that runs under X. You can run gnorpm even if you are not running GNOME. When you run gnorpm, it opens a window that lets you manage your rpm packages via a graphical interface. The format of the gnorpm command is: gnorpm [options] 5.1.2.1. gnorpm options The gnorpm options are: --geometry=geom Specify the geometry of the main window in standard X geometry format (i.e., w h+x+y). -i pkgfiles, --install pkgfiles Install the specified packages. -p pkgs, --packages pkgs The packages are in files, not in the rpm database (i.e., they haven't been installed yet). -q pkgs, --query pkgs Display a query window for the specified installed packages. -qp pkgfiles, --query --packages pkgfiles Display a query window for the specified package files. This is the same as specifying the -q and -p options. -U pkgfiles, --upgrade pkgfiles Upgrade the specified packages. -K pkgfiles, --checksig pkgfiles Check the signatures on the specified packages. -y pkgs, --verify pkgs Verify the specified packages. -?, --help Display a help message and exit. #BREAK# --root=dir Specify the filesystem root to use. --usage Display a brief usage message and exit. 5.1.2.2. The GNOME-RPM window The GNOME-RPM main window has five parts. At the top is a menu bar with three buttons: Packages Menu options are Query, Uninstall, and Verify. Operations Menu options are Find, Web find, Install, and Preferences. Help Provides online help for GNOME-RPM. Below the menu bar is a toolbar, with buttons to Install, Unselect, Uninstall, Query, Verify, Find, and Web find. At the very bottom of the window is a status bar. The rest of the window is the main panel. On the left is the package panel, which displays package folders in a tree structure. Clicking on a folder selects it; double-click to display the contents of the folder (i.e., the packages in that folder) on the righthand panel. Clicking on a package selects it; you then can use the menus and the toolbar buttons to operate on the package. You can select several packages at the same time and operate on them as a group. Right-clicking on a package icon selects the package if it isn't already and presents a menu with Query, Uninstall, and Verify options. See the GNOME-RPM documentation and online help for full details. PreviousHomeNext4.6. initrd: Using a RAM Disk Book Index5.2. The Debian Package Manager Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 5.2. The Debian Package Manager Debian GNU/Linux provides several package management tools, primarily intended to facilitate the building, installation, and management of binary packages. Debian package names generally end in .deb. The Debian package management tools include: dpkg Until recently, the most important of the Debian packaging tools and still the primary package management program. Used to install or uninstall packages or as a frontend to dpkg-deb. dpkg-deb Lower-level packaging tool. Used to create and manage the Debian package archives. Accepts and executes commands from dpkg or can be called directly. dselect An interactive frontend to dpkg. apt-get The currently available piece of the Advanced Package Tool (APT), which is still being developed and is intended to be a modern, user-friendly package management tool. Can be run from the command line or selected as a method from dselect. One of the features of apt-get is that you can use it to get and install packages across the Internet by specifying an ftp:// or http:// URL. Another feature is that you can use it to upgrade all packages currently installed on your system in a single operation. 5.2.1. Files Some important files used by the Debian package management tools are: control Comes with each package; documents dependencies; contains the name and version of the package, a description, maintainer, installed size, and so on. conffiles Comes with each package and contains a list of the configuration files associated with the package. preinst, postinst, prerm, postrm Scripts that can be included in a package to be run before installation, after installation, before removal, or after removal of the package. /var/lib/dpkg/available #BREAK# Contains information about packages available on the system. /var/lib/dpkg/status Contains information about the status of packages available on the system. /etc/apt/sources.list A list for APT of package sources, used to locate packages. The sources are listed one per line, in order of preference. /etc/apt/apt.conf The main APT configuration file. 5.2.2. Package States and Selection States The possible states that a package can be in are: config-files Only the configuration files for the package are present on the system. half-configured The package is unpacked and configuration was started but not completed. half-installed Installation was started but not completed. installed The package is unpacked and configured. not-installed The package is not installed. unpacked The package is unpacked but not configured. The possible package selection states are: deinstall The package has been selected for deinstallation (i.e., for removal of everything but configuration files). install The package has been selected for installation. purge The package has been selected to be purged (i.e., for removal of everything including the configuration files). #BREAK# 5.2.3. Package Flags There are two possible package flags that can be set for a package. They are: hold The package is not to be handled by dpkg, unless forced with the --force-hold option. reinst-required The package is broken and needs to be reinstalled. Such a package cannot be removed, unless forced with the --force- reinstreq option. 5.2.4. Scripts In addition to the commands described in the next subsection, several shell and Perl scripts are included with the package manager for use in building packages: dpkg-buildpackage Help automate package building. Shell script. dpkg-distaddfile Add an entry for a file to debian/files. Perl script. dpkg-genchanges Generate an upload control file from the information in an unpacked, built, source tree and the files it has generated. Perl script. dpkg-gencontrol Read information from an unpacked source tree and display a binary package control file on standard output. Perl script. dpkg-name Rename Debian packages to their full package names. Shell script. dpkg-parsechangelog Read and parse the changelog from an unpacked source tree and write the information to standard output in machine- readable form. Perl script. dpkg-scanpackages Create a Packages file from a tree of binary packages. The Packages file is used by dselect to provide a list of packages available for installation. Perl script. dpkg-shlibdeps Calculate shared library dependencies for named executables. Perl script. dpkg-source #BREAK# Pack and unpack Debian source archives. Perl script. 5.2.5. Debian Package Manager Command Summary apt- cdrom apt-cdrom [options] command Add a new CD-ROM to APT's list of available sources. Currently, the only command is add, which is required (except with the --help option). The database of CD-ROM IDs that APT maintains is /var/state/apt/cdroms.list. Options Options can be specified on the command line or they may be set in the configuration file. Boolean options set in the configuration file can be overridden on the command line in a number of different ways, a couple of which are --no-opt and -opt=no, where opt is the single-character or full name of the option. -a, --thorough Do a thorough package scan. May be needed with some old Debian CD-ROMs. -c, --config-file Specify a configuration file to be read after the default configuration file. -d, --cdrom Specify the CD-ROM mount point, which must be listed in /etc/fstab. The configuration option is Acquire::cdrom::mount. -f, --fast Do a fast copy, assuming the files are valid and don't all need checking. Specify this only if this disk has been run before without error. The configuration option is APT::CDROM::Fast. -h, --help Print help message and exit. -m, --no-mount Don't mount or unmount the mount point. The configuration option is APT::CDROM::NoMount. -n, --just-print, --recon, --no-act Check everything, but don't actually make any changes. The configuration option is APT::CDROM::NoAct. -o, --option Set a configuration option. Syntax is -o group::tool=option (e.g., APT::CDROM=Fast). -r, --rename Prompt for a new label and rename the disk to the new value. The configuration option is APT::CDROM::Rename. -v, --version #BREAK# Print the version information and exit. apt-get apt-get [options] command [package...] A command-line tool for handling packages. Will eventually be a backend to APT. Commands autoclean Like clean, but remove only package files that can no longer be downloaded. clean Clear the local repository of retrieved package files. check Update the package cache and check for broken packages. dist-upgrade Like upgrade but also handle dependencies intelligently. dselect-upgrade Used together with dselect. Track the changes made by dselect to the Status field of available packages and take actions necessary to realize that status. install package... Install one or more packages. Specify the package name, not the full filename. Other required packages also are retrieved and installed. With a hyphen appended to the package name, the package is removed if it is already installed. remove package... Remove one or more packages. Specify the package name, not the full filename. With a plus sign appended to the name, the package is installed. source package... Find source packages and download them into the current directory. If specified with --compile, the source packages are compiled into binary packages. With --download-only, the source packages are not unpacked. update Resynchronize the package overview files from their sources. Must be done before an upgrade or dist- upgrade. upgrade Install the latest versions of all packages currently installed. Run update first. Options #BREAK# Options can be specified on the command line or they may be set in the configuration file. Boolean options set in the configuration file can be overridden on the command line in one of several ways, a couple of which are --no- opt and -opt=no, where opt is the single-character or full name of the option. -b, --compile, --build Compile source packages after download. -c, --config-file Specify a configuration file to read after the default. -d, --download-only Retrieve package files, but don't unpack or install them. The configuration option is APT::Get::Download- only. -f, --fix-broken Try to fix a system with broken dependencies. Can be used alone or with a command. The configuration option is APT::Get::Fix-Broken. --force-yes Force yes. Causes APT to continue without prompting if it is doing something that could damage your system. Use with great caution and only if absolutely necessary. The configuration option is APT::Get::force-yes. -h, --help Display a help message and exit. --ignore-hold Ignore a hold placed on a package. Use with dist-upgrade to override many undesired holds. The configuration option is APT::Get::Ignore-Hold. -m, --ignore-missing, --fix-missing Ignore missing or corrupted packages or packages that cannot be retrieved. Can cause problems when used with -f. --no-download Disable package downloading; use with --ignore-missing to force APT to use only the packages that have already been downloaded. --no-upgrade Do not upgrade packages. Use with install to prevent upgrade of packages that are already installed. The configuration option is APT::Get::no-upgrade. -o, --option Set a configuration option. Syntax is -o group::tool=option (e.g., APT::Get=force-yes). #BREAK# --print-uris Print URIs of files instead of fetching them. Print path, destination filename, size, and expected MD5 hash. The configuration option is APT::Get::Print-URIs. -q, --quiet Quiet. Omit progress indicators, produce only logging output. Add a q to make even quieter. -s, --simulate, --just-print, --dry-run, --recon, --no-act Go through the motions, but don't actually make any changes to the system. The configuration option is APT::Get::Simulate. -u, --show-upgraded Print a list of all packages to be upgraded. The configuration option is APT::Get::Show-Upgraded. -v, --version Display the version and exit. -y, --yes, --assume-yes Automatically reply "yes" to prompts and run noninteractively. Abort if there is an error. The configuration option is APT::Get::Assume-Yes. dpkg dpkg [options] action A tool for installing, managing, and building packages. Serves as a frontend to dpkg-deb. dpkg actions These actions are carried out by dpkg itself: -i pkgfile, --install pkgfile Install the package specified as pkgfile. With -R or --recursive, pkgfile must be a directory. --unpack pkgfile Unpack the package, but don't configure it. With -R or --recursive, pkgfile must be a directory. --configure [packages|-a|--pending] Reconfigure one or more unpacked packages. If -a or --pending is given instead of packages, configure all packages that are unpacked but not configured. -r, --remove [packages|-a|--pending] --purge [packages|-a|--pending] Remove or purge one or more installed packages. Removal gets rid of everything except the configuration files listed in debian/conffiles; purging also removes the configuration files. If -a or -- pending is given instead of packages, dpkg removes or purges all packages that are unpacked and marked (in /var/lib/dpkg/status) for removing or purging. #BREAK# --print-avail package Print the details about package from /var/lib/dpkg/available. --update-avail pkgs-file --merge-avail pkgs-file Update the record of available files kept in /var/lib/dpkg/available. This information is used by dpkg and dselect to determine what packages are available. Update will replace the information with the contents of the pkgs-file, distributed as Packages. Merge combines the information from Packages with the existing information. -A pkgfile, --record-avail pkgfile Update the record of available files kept in /var/lib/dpkg/available with information from pkgfile. This information is used by dpkg and dselect to determine what packages are available. With -R or -- recursive, pkgfile must be a directory. --forget-old-unavail Forget about uninstalled unavailable packages. --clear-avail Remove existing information about what packages are available. -l, --list [pkg-name-pattern] List all packages whose names match the specified pattern. With no pattern, list all packages in /var/lib/dpkg/available. The pattern can include standard shell wildcard characters and may have to be quoted to prevent the shell from doing filename expansion. -s packages, --status packages Report the status of one or more packages by displaying the entry in the status database /var/lib/dpkg/status. -C, --audit Search for partially installed packages and suggest how to get them working. --get-selections [pattern] Get list of package selections and write to standard output. With pattern specified, write selections that match the pattern. --set-selections Set package selections based on input file read from standard input. --yet-to-unpack Search for uninstalled packages that have been selected for installation. -L packages, --listfiles packages List installed files that came from the specified package or packages. #BREAK# -S filename-pattern, --search filename-pattern Search installed packages for a filename. The pattern can include standard shell wildcard characters and may have to be quoted to prevent the shell from doing filename expansion. --print-architecture Print target architecture. --print-gnu-build-architecture Print the GNU version of the target architecture. --print-installation-architecture Print host architecture for installation. --compare-versions ver1 op ver2 Perform a binary comparison of two version numbers. The operators lt le eq ne ge gt treat a missing version as earlier. The operators lt-nl le-nl ge-nl gt-nl treat a missing version as later (where nl is "not later"). There is a third set of operators (< << <= = >= >> >) that is provided for compatibility with control-file syntax. dpkg returns zero for success (i.e., the condition is satisfied) and nonzero otherwise. –help Print help message and exit. –force-help Print help message about the –force-list options and exit. -Dh, –debug=help Print debugging help message and exit. –license Print dpkg license information and exit. Accepts the spelling –licence in addition to –license. –version Print dpkg version information and exit. dpkg-deb actions The following actions can be specified for dpkg and are passed to dpkg-deb for execution. Also see dpkg-deb. -b dir [archive], –build dir [archive] Build a package. -c archive, –contents archive List the contents of a package. #BREAK# -e, –control archive dir Extract control information from a package. -f archive [control-fields], –field archive [control-fields] Display the control field or fields of a package. -I archive [control-files], –info archive [control-files] Show information about a package. –fsys-tarfile archive Display the filesystem tar- file contained by a package. -x archive dir, –extract archive dir Extract the files from a package. -X archive dir, –vextract archive dir Extract and display the filenames from a package. Options –abort-after=num Abort processing after num errors. Default is 50. -B, –auto-deconfigure When a package is removed, automatically deconfigure any other package that depended on it. -Doctal, –debug=octal Turn on debugging, with the octal value specifying the desired level of debugging information. Use -Dh or –debug=help to display the possible values. You can OR the values to get the desired output. -E, –skip-same-version Don’t install the package if this version is already installed. –force-list, –no-force-list, –refuse-list Force or refuse to force an operation. list is specified as a comma-separated item of options. With –force, a warning is printed, but processing continues. –refuse and –no-force cause processing to stop with an error. The force/refuse options are: architecture Process even if intended for a different architecture. auto-select Select or deselect packages to install or remove them. Forced by default. #BREAK# bad-path Some programs are missing from the path. configure-any Configure any unconfigured package that the package depends on. conflicts Permit installation of conflicting packages. Can result in problems from files being overwritten. depends Turn dependency problems into warnings. depends-version Warn of version problems when checking dependencies, but otherwise ignore. downgrade Install even if a newer version is already installed. Forced by default. hold Process packages even if they are marked to be held. not-root Try to install or remove even when not logged on as root. overwrite Overwrite a file from one package with the same file from another package. Forced by default. overwrite-dir Overwrite one package’s directory with a file from another package. overwrite-diverted Overwrite a diverted file with an undiverted version. remove-essential Remove an essential package. Note that this can cause your system to stop working. remove-reinstreq Remove packages that are broken and are marked to require reinstallation. -G Don’t install a package if a newer version is already installed. The same as –refuse-downgrade. #BREAK# –ignore-depends=pkglist Dependency problems result only in a warning for the packages in pkglist. –largemem Specify that dpkg can use as much memory as it needs. –new New binary package format. This is a dpkg-deb option. –no-act Go through the motions, but don’t actually write any changes. Used for testing. Be sure to specify before the action; otherwise changes might be written. –nocheck Ignore the contents of the control file when building a package. This is a dpkg-deb option. -O, –selected-only Process only packages that are marked as selected for installation. –old Old binary package format. This is a dpkg-deb option. -R, –recursive Recursively handle .deb files found in the directories specified with -A, –install, –unpack, and –avail and their subdirectories. -R, –root=dir, –admindir=dir, –instdir=dir Change default directories. admindir contains administrative files with status and other information about packages; it defaults to /var/lib/dpkg. instdir is the directory in which packages are installed and defaults to /. Changing the root directory to dir automatically changes instdir to dir and admindir to /dir/var/lib/dpkg. –smallmem Specify that dpkg should try to preserve memory. dpkg- deb dpkg-deb action [options] Backend command for building and managing Debian package archives. Also see dpkg; you’ll often want to use dpkg to pass commands through to dpkg-deb, rather than call dpkg-deb directly. Actions -b dir [archive], –build dir [archive] Create an archive from the filesystem tree starting with directory dir. The directory must have a DEBIAN subdirectory containing the control file and any other control information. If archive is specified and is a #BREAK# filename, the package is written to that file; if no archive is specified, the package is written to dir.deb. If the archive already exists, it is replaced. If archive is the name of a directory, the dpkg-deb looks in the control file for the information it needs to generate the package name. (Note that for this reason, you cannot use –no-check with a directory name.) -c archive, –contents archive List the filesystem-tree portion of archive. -e, –control archive dir Extract control information from archive into the directory dir, which is created if it doesn’t exist. -f archive [control-fields], –field archive [control-fields] Extract information about one or more fields in the control file for archive. If no fields are provided, print the entire control file. -h, –help Print help information and exit. -I archive [control-files], –info archive [control-files] Provide information about binary package archive. If no control files are provided, print a summary of the package contents; otherwise, print the control files in the order they were specified. An error message is printed to standard error for any missing components. –fsys-tarfile archive Extract the filesystem tree from archive, and send it to standard output in tar format. Can be used with tar to extract individual files from an archive. –license Print the license information and exit. Accepts the spelling –licence in addition to –license. –version Print the version number and exit. -x archive dir, –extract archive dir -X archive dir, –vextract archive dir Extract the filesystem tree from archive into the specified directory, creating dir if it doesn’t already exist. -x (–extract) works silently, while -X (–vextract) lists the files as it extracts them. Do not use this option to install packages; use dpkg instead. Options -D Turn on debugging. –new Build a new-style archive format (this is the default). #BREAK# –no-check Don’t check the control file before building an archive. This lets you build a broken archive. –old Build an old-style archive format. dpkg- split dpkg-split [action] [options] Split a binary package into smaller pieces and reassemble the pieces, manually or in automatic mode. The automatic mode maintains a queue of parts for reassembling. Useful for transferring to and from floppy disks. Actions -a -o output part, –auto -o output part Add part to the queue for automatic reassembly and if all the parts are available, reassemble the package as output. -d [packages], –discard [packages] Discard parts from the automatic-assembly queue. If any packages are specified, discard only parts from those packages. Otherwise, empty the queue. -I parts, –info parts Print information about the part file or files specified. -j parts, –join parts Join the parts of a package file together from the parts specified. The default output file is package- version.deb. -l, –listq List the contents of the queue of parts waiting for reassembly, giving the package name, the parts that are on the queue, and the number of bytes. -s full-package [prefix], –full-package [prefix] Split the package full-package into parts, named prefixNofM.deb. The prefix defaults to the full-package name without the .deb extension. -h, –help Print help message and exit. –license Print the license information and exit. Accepts the spelling –licence in addition to –license. –version Print the version information and exit. #BREAK# Options –depotdir Specify an alternate directory depotdir for the queue of parts waiting for reassembly. Default is /var/lib/dpkg. –msdos Force –split output filenames to be MS-DOS-compatible. -Q, –npquiet Do not print an error message for a part that doesn’t belong to a binary package when doing automatic queuing or reassembly. -o output, –output output Use output as the filename for a reassembled package. -S num, –partsize num When splitting, specify the maximum part size (num) in kilobytes. Default is 450 KB. dselect dselect [options] [action] A screen-oriented user frontend to dpkg. The primary user interface for installing and managing packages. See dpkg and dpkg-deb for information on building packages. Actions If dselect is run with no action specified on the command line, it displays the following menu: * 0. [A]ccess Choose the access method to use. 1. [U]pdate Update list of available packages, if possible. 2. [S]elect Request which packages you want on your system. 3. [I]nstall Install and upgrade wanted packages. 4. [C]onfig Configure any packages that are unconfigured. 5. [R]emove Remove unwanted software. 6. [Q]uit Quit dselect. The asterisk (on the first line here) shows the currently selected option. Any of the menu items can be specified directly on the command line as an action (access, update, select, install, config, remove, quit) to go directly to the desired activity. For example: % dselect access If you enter quit on the command line, dselect exits immediately without doing anything. An additional command-line action is menu, which displays the menu and is equivalent to omitting the action. Options –admindir dir Change the directory that holds internal data files to dir. Default is /var/lib/dpkg. #BREAK# -D [file], –debug [file] Turn on debugging. Send output to file if specified. –help Print help message and exit. –license Print the license information and exit. Accepts the spelling –licence in addition to –license. –version Print version information and exit. PreviousHomeNext5. Red Hat and Debian Package Managers Book Index6. The Linux Shells: An Overview Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 6. The Linux Shells: An Overview Contents: Purpose of the ShellShell FlavorsCommon FeaturesDiffering Features The shell is a program that acts as a buffer between you and the operating system. In its role as a command interpreter, it should (for the most part) act invisibly. It also can be used for simple programming. This section introduces three shells commonly used on Linux systems — the Bourne-Again shell (bash), the C shell (csh), and csh’s enhanced version, tcsh — and summarizes the major differences between them. Details on them are provided in Chapter 7, “bash: The Bourne- Again Shell”, and Chapter 8, “csh and tcsh”. (Some Linux distributions also offer the Korn shell, ksh, another popular version of the Bourne shell with some of the same features as bash.) The following topics are presented in this chapter: l Purpose of the shell l Shell flavors l Common features l Differing features 6.1. Purpose of the Shell There are three main uses for the shell: #BREAK# l Interactive use l Customization of your Linux session l Programming 6.1.1. Interactive Use When the shell is used interactively, it waits for you to issue commands, processes them (to interpret special characters, such as wildcards), and executes them. Shells also provide a set of commands, known as built-ins, to supplement Linux commands. 6.1.2. Customization of Your Linux Session A Linux shell defines variables, such as the locations of your home directory and mail spool, to control the behavior of your session. Some variables are preset by the system; you can define others in startup files that your shell reads when you log in. Startup files also can contain Linux or shell commands, for execution immediately after login. 6.1.3. Programming A series of individual commands (be they shell or other Linux commands available on the system) combined into one executable file is called a shell script. Batch files in MS-DOS are a similar concept. bash is considered a powerful programming shell, while scripting in csh is rumored to be hazardous to your health. PreviousHomeNext5.2. The Debian Package Manager Book Index6.2. Shell Flavors Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 6.2. Shell Flavors Many different Linux shells are available. This book describes the three most popular shells: l The Bourne-Again shell (bash), which is based on the Bourne shell (sh) and is standard for Linux l The C shell (csh), which uses C syntax and has many conveniences l tcsh, an extension of csh that appears instead of csh in many Linux distributions Most systems have more than one shell, and people will often use one shell for writing shell scripts and another for interactive use. When you log in, the system determines which shell to run by consulting your entry in /etc/passwd. The last field of each entry calls a program to run as the default shell. For example: Program Name Shell /bin/sh Bourne-Again shell /bin/bash Bourne-Again shell /bin/csh C shell (or tcsh) /bin/tcsh tcsh You can change to another shell by typing the program name at the command line. For example, to change from bash to tcsh, type: $ exec tcsh PreviousHomeNext6. The Linux Shells: An Overview Book Index6.3. Common Features #BREAK# Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 6.3. Common Features The following table is a sampling of features that are common to bash, csh, and tcsh. Note that tcsh is an enhanced version of csh; therefore, tcsh includes all features of csh, plus some others. Symbol/Command Meaning/Action > Redirect output. >> Append output to file. < Redirect input. << ``Here'' document (redirect input). | Pipe output. & Run process in background. ; Separate commands on same line. * Match any character(s) in filename. ? Match single character in filename. !n Repeat command number n. [ ] Match any characters enclosed. ( ) Execute in subshell. ` ` Substitute output of enclosed command. " " Partial quote (allows variable and command expansion). Quote following character. $var Use value for variable. #BREAK# $$ Process ID. $0 Command name. $n nth argument (0| >! >! Force append. >>! >>! Variable assignment. var=val set var=val set var=val Set environment variable. export var=val setenv var val setenv var val Number of arguments. $# $#argv $#argv Exit status. $? $status $? Execute commands in file. . file source file source file End a loop statement. done end end End case or switch. esac endsw endsw Loop through variables. for/do foreach foreach Sample if statement. if [ $i -eq 5 ] if ($i==5) if ($i==5) End if statement. fi endif endif Set resource limits. ulimit limit limit Read from terminal. read $< $< Make a variable read-only. readonly set -r #BREAK# File inquiry operator; tests for nonzero size. -s Complete current word. Tab Tab Ignore interrupts. trap 2 onintr onintr Begin until loop. until/do until until Begin while loop. while/do while while PreviousHomeNext6.3. Common Features Book Index7. bash: The Bourne-Again Shell Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 7. bash: The Bourne-Again Shell Contents: Overview of FeaturesInvoking the ShellSyntaxVariablesArithmetic ExpressionsCommand HistoryBuilt-in CommandsJob Control This chapter presents the following topics: l Overview of features l Invoking the shell l Syntax l Variables l Arithmetic expressions l Command history l Built-in commands l Job control 7.1. Overview of Features bash is the GNU version of the standard Bourne shell -- the original Unix shell -- and incorporates many popular features from other shells such as csh, tcsh, and the Korn shell (ksh). Both tcsh, which is described in the following chapter, and ksh, which offers many of the features in this chapter, also are available on most distributions of Linux. But bash is the standard Linux shell, loaded by default when most user accounts are created. If executed as part of the user's login, bash starts by executing any commands found in /etc/profile. Then it executes the commands found in ~/.bash_profile, ~/.bash_login, or ~/.profile (searching for each file only if the previous file is not found). Many distributions change shell defaults in /etc/profile for all users, even changing the behavior of common commands like ls. In addition, every time it starts (as a subshell or a login shell), bash looks for a file named ~/.bashrc. Many system administration utilities create a small ~/.bashrc automatically, and many users create quite large startup files. Any commands that can be executed from the shell can be included. A small sample file may look like this; each feature can be found either in this chapter or in Chapter 3, "Linux Commands": #BREAK# # Set bash variable to keep 50 commands in history. HSTSIZE=50# # Set prompt to show current working directory and history number of command. PS1='w: Command !$ ' # # Set path to search for commands in my personal directories, then standard ones. PATH=~/bin:~/scripts:$PATH# # Keep group and others from writing my newly created files. umask 022# # Show color-coded file types. alias ls='ls --color=yes' # # Make executable and .o files ugly yellow so I can find and delete them. export LS_COLORS="ex=43:*.o=43" # # Quick and dirty test of a single-file program. function gtst () { g++ -o $1 $1.C && ./$1} # # Remove .o files. alias clean='find ~ -name *.o -exec rm {} ;' bash provides the following features: l Input/output redirection l Wildcard characters (metacharacters) for filename abbreviation l Shell variables for customizing your environment l Powerful programming capabilities l Command-line editing (using vi- or Emacs-style editing commands) l Access to previous commands (command history) l Integer arithmetic l Arithmetic expressions l Command name abbreviation (aliasing) l Job control l Integrated programming features l Control structures l Directory stacking (using pushd and popd) l Brace/tilde expansion l Key bindings PreviousHomeNext6.4. Differing Features Book Index7.2. Invoking the Shell Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 7.2. Invoking the Shell The command interpreter for bash can be invoked as follows: bash [options] [arguments] bash can execute commands from a terminal (when -i is specified), from a file (when the first argument is an executable script), or from standard input (if no arguments remain or if -s is specified). 7.2.1. Options Options that appear here with double hyphens also work when entered with single hyphens, but the double-hyphen versions are recommended because they are standard. -, -- Treat all subsequent strings as arguments, not options. --dump-po-strings Same as --dump-strings but uses a special "portable object" format suitable for scripting. --dump-strings For execution in non-English locales, dump all strings that bash translates. -c str Read commands from string str. -i Create an interactive shell (prompt for input). --help Print information about which version of bash is installed, plus a list of options. #BREAK# --login Behave like a login shell; try to process /etc/profile on startup. Then process ~/.bash_profile, ~/.bash_login, or ~/.profile (searching for each file only if the previous file is not found). --nobraceexpansion Disable brace expansion. --noediting Disable line editing with arrow and control keys. --noprofile Do not process /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile on startup. --norc Do not process ~/.bashrc on startup. -p Start up as a privileged user; don't process $HOME/.profile. --posix Conform to POSIX standard. -r Restrict users to a very secure, limited environment; for instance, they cannot change out of the startup directory or use the > sign to redirect output. –rcfile file Substitute file for .bashrc on startup. –restricted Same as -r. -s Read commands from standard input; output from built-in commands goes to file descriptor 1; all other shell output goes to file descriptor 2. -v Print each line as it is executed (useful for tracing scripts). –verbose Same as -v. –version #BREAK# Print information about which version of bash is installed. -x Turn on debugging, as described under the -x option to the set built-in command. -D For execution in non-English locales, dump all strings that bash translates. The remaining options to bash are listed under the set built-in command. 7.2.2. Arguments Arguments are assigned, in order, to the positional parameters $1, $2, and so forth. If the first argument is an executable script, commands are read from it and remaining arguments are assigned to $1, $2, and so on. PreviousHomeNext7. bash: The Bourne-Again Shell Book Index7.3. Syntax Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 7.3. Syntax This subsection describes the many symbols peculiar to bash. The topics are arranged as follows: l Special files l Filename metacharacters l Command-line editing l Quoting l Command forms l Redirection forms l Coprocesses 7.3.1. Special Files File Purpose /etc/profile Executed automatically at login $HOME/.bash_profile Executed automatically at login $HOME/.bashrc Executed automatically at shell startup $HOME/.bash_logout Executed automatically at logout $HOME/.bash_history Record of last session’s commands /etc/passwd Source of home directories for ~name abbreviations 7.3.2. Filename Metacharacters Characters Meaning * Match any string of zero or more characters. ? Match any single character. [abc…] Match any one of the enclosed characters; a hyphen can be used to specify a range (e.g., a-z, A-Z, 0-9). [!abc…] Match any character not among the enclosed characters. {str1,…} Brace expansion: match any of the enclosed strings. ~name HOME directory of user name. ~+ Current working directory (PWD). ~- Previous working directory from directory stack (OLDPWD, see also the pushd built-in command). ~+n The nth entry in the directory stack, counting from the start of the list with the first entry being 0. ~-n The nth entry in the directory stack, counting from the end of the list with the last entry being 0. Patterns can be a sequence of patterns separated by |; if any of the subpatterns match, the entire sequence is considered matching. #BREAK# This extended syntax resembles that available to egrep and awk. 7.3.2.1. Examples $ ls new* List new and new.1$ cat ch? Match ch9 but not ch10$ vi [D-R]* Match files that begin with uppercase D through R 7.3.3. Command-line Editing Command lines can be edited like lines in either Emacs or vi. Emacs is the default. See Section 7.6.1, “Line-Edit Mode” later in this chapter for more information. vi mode has two submodes, insert mode and command mode. The default mode is insert; you can go to command mode by pressing Esc. In command mode, typing a (append) or i (insert) will return you to insert mode. Some users discover that the Del or Backspace key on the terminal does not delete the character before the cursor, as it should. Sometimes the problem can be solved by issuing one of the following commands (or placing it in your .bashrc file): stty erase ^? stty erase ^H See the stty command in Chapter 3, “Linux Commands” for more information. On the X Window System, an alternative solution is to use the xmodmap command, but this cannot be described easily here because it requires you to do some research about your terminal. Table 7-1 through Table 7-14 show various Emacs and vi commands. Table 7-1. Basic Emacs-Mode Commands Command Description Ctrl-B Move backward one character (without deleting). Ctrl-F Move forward one character. Del Delete one character backward. Ctrl-D Delete one character forward. Table 7-2. Emacs-Mode Word Commands Command Description Esc b Move one word backward. Esc f Move one word forward. Esc Del Kill one word backward. Esc d Kill one word forward. Ctrl-Y Retrieve (yank) last item killed. Table 7-3. Emacs-Mode Line Commands Command Description Ctrl-A Move to beginning of line. #BREAK# Ctrl-E Move to end of line. Ctrl-K Kill forward to end of line. Table 7-4. Emacs-Mode Commands for Moving Through the History File Command Description Ctrl-P Move to previous line. Ctrl-N Move to next line. Ctrl-R Search backward. Esc < Move to first line of history file. Esc > Move to last line of history file. Table 7-5. Completion Commands Command Description Tab Attempt to perform general completion of the text. Esc ? List the possible completions. Esc / Attempt filename completion. Ctrl-X / List the possible filename completions. Esc ~ Attempt username completion. Ctrl-X ~ List the possible username completions. Esc $ Attempt variable completion. Ctrl-X $ List the possible variable completions. Esc @ Attempt hostname completion. Ctrl-X @ List the possible hostname completions. Esc ! Attempt command completion. Ctrl-X ! List the possible command completions. Esc Tab Attempt completion from previous commands in the history list. Table 7-6. Emacs-Mode Miscellaneous Commands Command Description Ctrl-J Same as Return. Ctrl-L Clear the screen, placing the current line at the top of the screen. Ctrl-M Same as Return. Ctrl-O Same as Return, then display next line in command history. Ctrl-T Transpose character left of and under the cursor. Ctrl-U Kill the line from the beginning to point. Ctrl-V Insert keypress instead of interpreting it as a command. #BREAK# Ctrl-[ Same as Esc (most keyboards). Esc c Capitalize word under or after cursor. Esc u Change word under or after cursor to all capital letters. Esc l Change word under or after cursor to all lowercase letters. Esc . Insert last word in previous command line after point. Esc _ Same as Esc. Table 7-7. Editing Commands in vi Input Mode Command Description Del Delete previous character. Ctrl-W Erase previous word (i.e., erase until a blank). Ctrl-V Insert keypress instead of interpreting it as a command. Esc Enter control mode (see Table 7-8). Table 7-8. Basic vi Control Mode Commands Command Description h Move left one character. l Move right one character. b Move left one word. w Move right one word. B Move to beginning of preceding nonblank word. W Move to beginning of next nonblank word. e Move to end of current word. E Move to end of current nonblank word. 0 Move to beginning of line. ^ Move to first nonblank character in line. $ Move to end of line. Table 7-9. Commands for Entering vi Input Mode Command Description i Insert text before current character (insert). a Insert text after current character (append). I Insert text at beginning of line. A Insert text at end of line. r Replace current character with this text. R Overwrite existing text. #BREAK# Table 7-10. Some vi-Mode Deletion Commands Command Description dh Delete one character backward. dl Delete one character forward. db Delete one word backward. dw Delete one word forward. dB Delete one nonblank word backward. dW Delete one nonblank word forward. d$ Delete to end-of-line. d0 Delete to beginning of line. Table 7-11. Abbreviations for vi-Mode Delete Commands Command Description D Delete to end of line (equivalent to d$). dd Delete entire line (equivalent to 0d$). C Delete to end of line; enter input mode (equivalent to c$). cc Delete entire line; enter input mode (equivalent to 0c$). X Delete character backward (equivalent to dl). x Delete character forward (equivalent to dh). Table 7-12. vi Control Mode Commands for Searching the Command History Command Description k or - Move backward one line. j or + Move forward one line. G Move to line given by repeat count. /string Search backward for string. ?string Search forward for string. n Repeat search in same direction as previous. N Repeat search in opposite direction of previous. Table 7-13. vi-Mode Character-Finding Commands Command Description fx Move right to next occurrence of x. Fx Move left to previous occurrence of x. tx Move right to next occurrence of x, then back one space. #BREAK# Tx Move left to previous occurrence of x, then forward one space. ; Redo last character-finding command. , Redo last character-finding command in opposite direction. Table 7-14. Miscellaneous vi-Mode Commands Command Description ~ Invert (toggle) case of current character(s). - Append last word of previous command; enter input mode. Ctrl-L Clear the screen and redraw the current line on it; good for when your screen becomes garbled. # Prepend # (comment character) to the line and send it to the history file; useful for saving a command to be executed later, without having to retype it. 7.3.4. Quoting Quoting disables a character’s special meaning and allows it to be used literally, as itself. The following characters have special meaning to bash: Character Meaning ; Command separator & Background execution ( ) Command grouping (enter a subshell) { } Command block | Pipe > < & Redirection symbols * ? [ ] ~ ! Filename metacharacters " ' Used in quoting other characters ` Command substitution $ Variable substitution (or command substitution) newline space tab Word separators # Comment The following characters can be used for quoting: Character Action #BREAK# " " Everything between " and " is taken literally, except for the following characters that keep their special meaning: $ Variable substitution will occur. ` Command substitution will occur. " This marks the end of the double quote. ' ' Everything between ' and ' is taken literally, except for another '. The character following a is taken literally. Use within " " to escape ", $, and `. Often used to escape itself, spaces, or newlines. 7.3.4.1. Examples $ echo 'Single quotes "protect" double quotes'Single quotes "protect" double quotes$ echo "Well, isn't that "special"?" Well, isn't that "special"? $ echo "You have `ls|wc -l` files in `pwd`" You have 43 files in /home/bob$ echo "The value of $x is $x" The value of $x is 100 7.3.5. Command Forms Syntax Effect cmd & Execute cmd in background. cmd1 ; cmd2 Command sequence; execute multiple cmds on the same line. (cmd1 ; cmd2) Subshell; treat cmd1 and cmd2 as a command group. cmd1 | cmd2 Pipe; use output from cmd1 as input to cmd2. cmd1 `cmd2` Command substitution; use cmd2 output as arguments to cmd1. cmd1 $(cmd2) Command substitution; nesting is allowed. cmd1 && cmd2 AND; execute cmd2 only if cmd1 succeeds. cmd1 || cmd2 OR; execute cmd2 only if cmd1 fails. { cmd1 ; cmd2 } Execute commands in the current shell. 7.3.5.1. Examples $ nroff file & Format in the background$ cd; ls Execute sequentially$ (date; who; pwd) > logfile All output is redirected$ sort file | pr -3 | lp Sort file, page output, then print$ vi `grep -l ifdef *.c` Edit files found by grep$ egrep ‘(yes|no)’ `cat list` Specify a list of files to search #BREAK# $ egrep ‘(yes|no)’ $(cat list) Same as previous using bash command substitution$ egrep ‘(yes|no)’ $(
file Direct standard output to file. < file Take standard input from file. cmd1 | cmd2 Pipe; take standard output of cmd1 as standard input to cmd2. >> file Direct standard output to file; append to file if it already exists. >| file Force standard output to file even if noclobber is set. n>| file Force output from the file descriptor n to file even if noclobber is set. <> file Use file as both standard input and standard output. << text Read standard input up to a line identical to text (text can be stored in a shell variable). Input is usually typed on the screen or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is enclosed in quotes, standard input will not undergo variable substitution, command substitution, etc. n> file Direct file descriptor n to file. n< file Set file as file descriptor n. >&n Duplicate standard output to file descriptor n. <&n Duplicate standard input from file descriptor n. &>file Direct standard output and standard error to file. <&- Close the standard input. >&- Close the standard output. n>&- Close the output from file descriptor n. n<&- Close the input from file descriptor n. 7.3.6.1. Examples $ cat part1 > book$ cat part2 part3 >> book$ mail tim < report #BREAK# $ grep Chapter part* 2> error_file$ sed ’s/^/XX /’ << END_ARCHIVE> This is often how a shell archive is “wrapped”, > bundling text for distribution. You would normally> run sed from a shell program, not from the command line. > END_ARCHIVEXX This is often how a shell archive is “wrapped”, XX bundling text for distribution. You would normallyXX run sed from a shell program, not from the command line. To redirect standard output to standard error: $ echo “Usage error: see administrator” 1>&2 The following command sends output (files found) to filelist and sends error messages (inaccessible files) to file no_access: $ find / -print > filelist 2>no_access 7.3.7. Coprocesses Coprocesses are a feature of bash and do not appear in other shells. Syntax Effect cmd1 | cmd2 |& Coprocess; execute the pipeline in the background. The shell sets up a two-way pipe, allowing redirection of both standard input and standard output. read -p var Read coprocess input into variable var. print -p string Write string to the coprocess. cmd <&p Take input for cmd from the coprocess. cmd >&p Send output of cmd to the coprocess. 7.3.7.1. Examples cat memo Print contents of fileSufficient unto the day is A word to the wise. ed - memo |& Start coprocessprint -p /word/ Send ed command to coprocessread -p search Read output of ed command into variable searchprint “$search” Show the line on standard outputA word to the wise. PreviousHomeNext7.2. Invoking the Shell Book Index7.4. Variables Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 7.4. Variables Variables are prefaced by a dollar sign ($) and optionally enclosed in braces ({}). You can assign a value to a variable through an equals sign (=); no whitespace can appear on either side of the equals sign: $ TMP=temp.file By default, variables are seen only within the shell itself; to pass variables to other programs invoked within the shell, see the export built-in command. If subscripted by brackets ([]), the variable is considered an array variable. For instance: $ DIR_LIST[0]=src$ DIR_LIST[1]=headers$ ls ${DIR_LIST[1]} The contents of headers are listed. Many substitutions and commands in this chapter handle arrays by operating on each element separately. This subsection describes: l Variable substitution l Built-in shell variables 7.4.1. Variable Substitution In the following substitutions, braces ({ }) are optional, except when needed to separate a variable name from following characters that would otherwise be considered part of the name. Variable Meaning ${var} The value of variable var. #BREAK# $0 Name of the program. ${n} Individual arguments on command line (positional parameters); 1 n 9. $# Number of arguments on command line. $* All arguments on command line. $@ Same as $* but contents are split into words when the variable is enclosed in double quotes. $$ Process number of current shell; useful as part of a filename for creating temporary files with unique names. $? Exit status of last command (normally 0 for success). $! Process number of most recently issued background command. $- Current execution options (see the set built-in command). By default, hB for scripts and himBH for interactive shells. $_ Initially set to name of file invoked for this shell, then set for each command to the last word of the previous command. Table 7-16 through Table 7-18 show various types of operators that can be used with bash variables. Table 7-16. Substitution Operators Operator Substitution ${varname:-word} If varname exists and isn’t null, return its value; otherwise, return word. Purpose: Returning a default value if the variable is undefined. Example: ${count:-0} evaluates to 0 if count is undefined. ${varname:=word} If varname exists and isn’t null, return its value; otherwise set it to word and then return its value. Positional and special parameters cannot be assigned this way. #BREAK# Purpose: Setting a variable to a default value if it is undefined. Example: ${count:=0} sets count to 0 if it is undefined. ${varname:?message} If varname exists and isn’t null, return its value; otherwise, print varname: followed by message, and abort the current command or script (noninteractive shells only). Omitting message produces the default message “parameter null or not set.” Purpose: Catching errors that result from variables being undefined. Example: {count:?”undefined!”} prints “count: undefined!'’ and exits if count is undefined. ${varname:+word} If varname exists and isn’t null, return word; otherwise, return null. Purpose: Testing for the existence of a variable. Example: ${count:+1} returns 1 (which could mean true) if count is defined. ${#varname} Return the number of characters in varname. Purpose: Preparing for substitution or extraction of substrings. Example: If ${USER} currently expands to root, ${#USER} expands to 4. Table 7-17. Pattern-Matching Operators Operator Meaning ${variable#pattern} If the pattern matches the beginning of the variable’s value, delete the shortest part that matches and return the rest. ${variable##pattern} If the pattern matches the beginning of the variable’s value, delete the longest part that matches and return the rest. ${variable%pattern} If the pattern matches the end of the variable’s value, delete the shortest part that matches and return the rest. ${variable%%pattern} If the pattern matches the end of the variable’s value, delete the longest part that matches and return the rest. ${var/pat/sub} Return var with the first occurrence of pat replaced by sub. Can be applied to $* or $@, in which case each word is treated separately. If pat starts with # it can match only the start of var; if pat ends with % it can match only the end of var. #BREAK# ${var//pat/sub} Return var with the every occurrence of pat replaced by sub. ${variable:n} Truncate the beginning of the variable and return the part starting with character number n, where the first character is 0. ${variable:n:l} Starting with character number n, where the first character is 0, return a substring of length l from the variable. Table 7-18. Expression Evaluation Operator Meaning $((arithmetic-expression)) Return the result of the expression. Arithmetic operators are described under “Arithmetic Expressions.” Example: TODAY=’date +%-d’ ; echo $(($TODAY+7)) stores the number of the current day in $TODAY and then prints that number plus 7 (the number of the same day next week). [[$condition]] Return 1 if condition is true and 0 if it is false. Conditions are described under the test built-in command. 7.4.2. Built-in Shell Variables Built-in variables are set automatically by the shell and typically are used inside shell scripts. Built-in variables can make use of the variable substitution patterns already shown earlier. When setting variables, you do not include dollar signs, but when referencing their values later, the dollar signs are necessary. Tables Table 7-19 through Table 7-22 show the commonly used built-in variables in bash. Table 7-19. Behavior-Altering Variables Variable Meaning auto_resume Allows a background job to be brought to the foreground simply by entering a substring of the job’s command line; values can be substring (resume if the user’s string matches part of the command); exact (string must exactly match command); or another value (string must match at beginning of command). BASH_ENV Startup file of commands to execute, if bash is invoked to run a script. #BREAK# CDPATH Colon-separated list of directories to search for the directory passed in a cd command. EDITOR Pathname of your preferred text editor. IFS Word separator; used by shell to parse commands into their elements. IGNOREEOF If nonzero, don’t allow use of a single Ctrl-D (the end-of-file or EOF character) to log off; use the exit command to log off. PATH Colon-separated list of directories to search for each command. PROMPT_COMMAND Command that bash executes before issuing a prompt for a new command. PS1 Prompt displayed before each new command; see the later section Section 7.6.4, “Variables in Prompt” for ways to introduce dynamically changing information such as the current working directory or command history number into the prompt. PS2 Prompt displayed before a new line if a command is not finished. PS3 Prompt displayed by select built-in command. PS4 Prompt displayed by -x debugging (see Section 7.2, “Invoking the Shell”). and the set built-in command). Table 7-20. History Variables Variable Meaning FCEDIT Pathname of editor to use with the fc command. HISTCMD The history number of the current command. HISTCONTROL If HISTCONTROL is set to the value of ignorespace, lines beginning with a space are not entered into the history list. If set to ignoredups, lines matching the last history line are not entered. Setting it to ignoreboth enables both options. HISTFILE Name of history file, on which the editing modes operate. HISTFILESIZE The maximum number of lines to store in the history file. The default is 500. #BREAK# HISTSIZE The maximum number of commands to remember in the command history. The default is 500. Table 7-21. Mail Variables Variable Meaning MAIL Name of file to check for incoming mail. MAILCHECK How often, in seconds, to check for new mail (default is 60 seconds). MAILPATH List of filenames, separated by colons (:), to check for incoming mail. Table 7-22. Status Variables Variable Meaning BASH Pathname of this instance of the shell you are running. BASH_VERSION The version number of the shell you are running. COLUMNS The number of columns your display has. DIRSTACK List of directories manipulated by pushd and popd commands. EUID Effective user ID of process running this shell, in the form of the number recognized by the system. GROUPS Groups to which user belongs, in the form of the numbers recognized by the system. HOME Name of your home (login) directory. HOSTNAME Host the shell is running on. HOSTTYPE Short name indicating the type of machine the shell is running on; for instance, i486. LINES The number of lines your display has. MACHTYPE Long string indicating the machine the shell is running on; for instance, i486-pc-linux-gnu. OLDPWD Previous directory before the last cd command. OSTYPE Short string indicating the operating system; for instance, “linux- gnu.” #BREAK# PPID Process ID of parent process that invoked this shell. PWD Current directory. SECONDS Number of seconds since the shell was invoked. SHELL Pathname of the shell you are running. SHLVL Depth to which running shells are nested. TERM The type of terminal that you are using. UID Real user ID of process running this shell, in the form of the number recognized by the system. PreviousHomeNext7.3. Syntax Book Index7.5. Arithmetic Expressions Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 7.5. Arithmetic Expressions The let command performs integer arithmetic. bash provides a way to substitute integer values (for use as command arguments or in variables); base conversion is also possible: Expression Meaning (( expr)) Use the value of the enclosed arithmetic expression. 7.5.1. Operators bash uses arithmetic operators from the C programming language; the following list is in decreasing order of precedence. Use parentheses to override precedence. Operator Meaning - Unary minus ! ~ Logical negation; binary inversion (one’s complement) * / % Multiplication; division; modulus (remainder) + - Addition; subtraction << >> Bitwise left shift; bitwise right shift <= >= Less than or equal to; greater than or equal to < > Less than; greater than == != Equality; inequality (both evaluated left to right) & Bitwise AND ^ Bitwise exclusive OR | Bitwise OR #BREAK# && Logical AND || Logical OR = Assign value. += -= Reassign after addition/subtraction *= /= %= Reassign after multiplication/division/remainder &= ^= |= Reassign after bitwise AND/XOR/OR <<= >>= Reassign after bitwise shift left/right 7.5.2. Examples See the let built-in command for more information and examples. let “count=0″ “i = i + 1″ Assign i and countlet “num % 2″ Test for an even number PreviousHomeNext7.4. Variables Book Index7.6. Command History Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 7.6. Command History bash lets you display or modify previous commands. This is similar to the C shell’s history mechanism. Commands in the history list can be modified using: l Line-edit mode l The fc command In addition, the command substitutions described in Chapter 8, “csh and tcsh”, also work in bash. 7.6.1. Line-Edit Mode Line-edit mode lets you emulate many features of the vi or Emacs editors. The history list is treated like a file. When the editor is invoked, you type editing keystrokes to move to the command line you want to execute. Arrow keys work on most terminals in both Emacs mode and vi command mode. You also can change the line before executing it. See Table 7-23 for some examples of common line-edit commands. When you’re ready to issue the command, press Return. The default line-edit mode is Emacs. To enable vi mode, enter: $ set -o vi Note that vi starts in input mode; to type a vi command, press Esc first. The mode you use for editing bash commands is an entirely separate choice from the editor that is invoked for you automatically within many commands (for instance, the editor mail readers invoke when you ask them to create a new mail message). To change the default editor, set the VISUAL or EDITOR variable to the filename or full pathname of your favorite editor: $ export EDITOR=emacs Table 7-23. Common Editing Keystrokes vi Emacs Result k Ctrl-P Get previous command. j Ctrl-N Get next command. /string Ctrl-R string Get previous command containing string. h Ctrl-B Move back one character. l Ctrl-F Move forward one character. b Esc B Move back one word. w Esc F Move forward one word. X Del Delete previous character. x Ctrl-D Delete one character. dw Esc D Delete word forward. db Esc H Delete word back. #BREAK# xp Ctrl-T Transpose two characters. 7.6.2. The fc Command Use fc -l to list history commands and fc -e to edit them. See the entry under built-in commands for more information. 7.6.2.1. Examples $ history List the last 16 commands$ fc -l 20 30 List commands 20 through 30$ fc -l -5 List the last five commands$ fc -l cat List the last command beginning with cat$ fc -ln 5 > doit Save command 5 to file doit$ fc -e vi 5 20 Edit commands 5 through 20 using vi$ fc -e emacs Edit previous command using Emacs$ !! Reexecute previous command$ !cat Reexecute last cat command$ !cat foo-file Reexecute last command, adding foo-file to the end of the argument list 7.6.3. Command Substitution Syntax Meaning ! Begin a history substitution. !! Previous command. !N Command number N in history list. !-N Nth command back from current command. !string Most recent command that starts with string. !?string? Most recent command that contains string. !?string?% Most recent command argument that contains string. !$ Last argument of previous command. !# The current command up to this point. !!string Previous command, then append string. !N string Command N, then append string. !{s1}s2 Most recent command starting with string s1, then append string s2. ^old^new^ Quick substitution; change string old to new in previous command; execute modified command. 7.6.4. Variables in Prompt Using the following variables, you can display information about the current state of the shell or the system in your bash prompt. Set the PS1 variable to a string including the desired variables. For instance, the following command sets PS1 to a string that includes the w variable in order to display the current working directory and the ! variable in order to display the number of the current command. The next line is the prompt displayed by the change. $ PS1=’w: Command !$ ‘ ~/book/linux: Command 504$ Some of the prompt variables are relatively new, such as j and l, so they may not be supported in your version of bash. #BREAK# Variable Meaning a Alarm (bell) d Date in the format “Mon May 8″ e Escape character (terminal escape, not backslash) h Hostname j Number of background jobs (active or stopped) l Current terminal name n Newline inserted in the prompt r Carriage return inserted in the prompt s Current shell t Time in 24-hour format, where 3:30 p.m. appears as 15:30:00 u User’s account name v Version and release of bash w Current working directory H Like h T Time in 12-hour format, where 3:30 p.m. appears as 03:30:00 V Version, release, and patch level of bash W Last element (following last slash) of current working directory \ Single backslash inserted in the prompt ! Number of current command in the command history # Number of current command, where numbers started at 1 when the shell started @ Time in 12-hour format, where 3:30 P.M. appears as 03:30 p.m. $ Indicates whether you are root: displays # for root, $ for other users [ Starts a sequence of nonprinting characters, to be ended by ] ] Ends the sequence of nonprinting characters started by [ nnn The character in the ASCII set corresponding to the octal number nnn inserted into the prompt PreviousHomeNext7.5. Arithmetic Expressions Book Index7.7. Built-in Commands Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 7.7. Built-in Commands Examples to be entered as a command line are shown with the $ prompt. Otherwise, examples should be treated as code fragments that might be included in a shell script. For convenience, some of the reserved words used by multiline commands also are included. # # Ignore all text that follows on the same line. # is used in shell scripts as the comment character and is not really a command. #! #!shell Used as the first line of a script to invoke the named shell (with optional arguments). Some older, non-Linux systems do not support scripts starting with this line. For example: #!/bin/bash #BREAK# : : Null command. Returns an exit status of 0. Sometimes used as the first character in a file to denote a bash script. Shell variables can be placed after the : to expand them to their values. Example Check whether someone is logged in: if who | grep $1 > /dev/nullthen : # do nothing # if pattern is foundelse echo “User $1 is not logged in” fi . . file [arguments] Same as source. alias alias [-p] [name[=’cmd’]] Assign a shorthand name as a synonym for cmd. If =’cmd’ is omitted, print the alias for name; if name also is omitted or if [-p] is specified, print all aliases. See also unalias. #BREAK# bg bg [jobIDs] Put current job or jobIDs in the background. See Section 7.8, “Job Control” later in this chapter. bind bind [options] bind [options] key:function Print or set the bindings that allow keys to invoke functions such as cursor movement and line editing. Typical syntax choices for keys are “C-t” for Ctrl-T and “M-t” or “et” for Esc T (quoting is needed to escape the sequences from the shell). Function names can be seen though the -l option. Options -f filename Consult filename for bindings, which should be in the same format as on the bind command line. -l Print all Readline functions, which are functions that can be bound to keys. -m keymap Specify a keymap for this and further bindings. Possible keymaps are emacs, emacs-standard, emacs-meta, emacs- ctlx, vi, vi-move, vi-command, and vi-insert. -p #BREAK# Display all functions and the keys that invoke them, in the format by which keys can be set. -q function Display the key bindings that invoke function. -r key Remove the binding attached to key, so it no longer works. -s Display all macros and the keys that invoke them, in the format by which keys can be set. -u function Remove all the bindings attached to function, so no keys will invoke it. -v Display all Readline variables (settings that affect history and line editing) and their current settings, in the format by which variables can be set. -x key:command Bind key to a shell command (recent; not in all bash versions in common use). -P Display all bound keys and the functions they invoke. #BREAK# -S Display all macros and the keys that invoke them. -V Display all Readline variables (settings that affect history and line editing) and their current settings. Example Bind Ctrl-T to copy-forward-word, the function that copies the part of the word following the cursor so it can be repasted: $ bind “C-t”:copy-forward-word break break [n] Exit from the innermost (most deeply nested) for, while, or until loop, or from the n innermost levels of the loop. Also exits from a select list. builtin builtin command [arguments] Execute command, which must be a shell built-in. Useful for invoking built-ins within scripts of the same name. #BREAK# case case string in regex) commands ;; … esac If string matches regular expression regex, perform the following commands. Proceed down the list of regular expressions until one is found (to catch all remaining strings, use * as regex at the end). cd cd [dir&] With no arguments, change to home directory of user. Otherwise, change working directory to dir. If dir is a relative pathname but is not in the current directory, then the CDPATH variable is searched. #BREAK# command command [options] command [arguments] Execute command; do not perform function look-up (i.e., refuse to run any command that is neither in PATH nor a built-in). Set exit status to that returned by command, unless command cannot be found, in which case exit with a status of 127. -p Search default path, ignoring the PATH variable’s value. — Treat everything that follows as an argument, not an option. continue continue [n] Skip remaining commands in a for, while, or until loop, resuming with the next iteration of the loop (or skipping n loops). declare declare [options] [name[=value]] typeset [options] [name[=value]] Print or set variables. Options prefaced by + instead of - are inverted in meaning. -a Treat the following names as array variables. -f #BREAK# Treat the following names as functions. -i Expect variable to be an integer, and evaluate its assigned value. -p Print names and settings of all shell variables and functions; take no other action. -r Do not allow variables to be reset later. -x Mark variables for subsequent export. -F Print names of all shell functions; take no other action. #BREAK# dirs dirs [options] Print directories currently remembered for pushd/popd operations. Options +entry Print entryth entry (list starts at 0). -entry Print entryth entry from end of list. -l Long listing. disown disown [options] [jobIDs] Let job run, but disassociate it from the shell. By default, do not even list the job as an active job; commands like jobs and fg will no longer recognize it. When -h is specified, the job is recognized but simply is kept from being killed when the shell dies. Options -a Act on all jobs. #BREAK# -h Do not pass a SIGHUP signal received by the shell on to the job. echo echo [options] [string] Write string to standard output, terminated by a newline. If no string is supplied, echo a newline. In bash, echo is just an alias for print -. (See also echo in Chapter 3, “Linux Commands”). -e Enable interpretation of escape characters: a Audible alert b Backspace c Suppress the terminating newline (same as -n) f Form feed n Newline r #BREAK# Carriage return t Horizontal tab v Vertical tab \ Backslash nnn The character in the ASCII set corresponding to the octal number nnn. xnnn The character in the ASCII set corresponding to the hexadecimal number nnn. -n Do not append a newline to the output. -E Disable interpretation of escape characters. #BREAK# enable enable [options] [built-in …] Enable (or when -n is specified, disable) built-in shell commands. Without built-in argument or with -p option, print enabled built-ins. With -a, print the status of all built-ins. You can disable shell commands in order to define your own functions with the same names. Options -a Display all built-ins, both enabled and disabled. -n builtin Disable builtin. -p Display enabled built-ins. -s Restrict display to special built-ins defined by POSIX standard. eval eval [command args…] Perform command, passing args. #BREAK# exec exec [options] [command] Execute command in place of the current process (instead of creating a new process). exec also is useful for opening, closing, or copying file descriptors. Options -a name Tell command that it was invoked as name. -c Remove all environment variables from the process when the new command runs. -l Treat the new process as if the user were logging in. Examples $ trap ‘exec 2>&-’ 0 Close standard error when shell script exits (signal 0) $ exec /bin/tcsh Replace current shell with extended C shell$ exec < infile Reassign standard input to infile #BREAK# exit exit [n] Exit a shell script with status n (e.g., exit 1). n can be zero (success) or nonzero (failure). If n is not given, exit status will be that of the most recent command. exit can be issued at the command line to close a window (log out). Example if [ $# -eq 0 ]; then echo "Usage: $0 [-c] [-d] file(s)" exit 1 # Error statusfi export export [options] [variables] export [options] [name=[value]]... Pass (export) the value of one or more shell variables, giving global meaning to the variables (which are local by default). For example, a variable defined in one shell script must be exported if its value will be used in other programs called by the script. If no variables are given, export lists the variables exported by the current shell. If name and value are specified, assign value to a variable name. Options -- Treat all subsequent strings as arguments, not options. -f #BREAK# Expect variables to be functions. -n Unexport variable. -p List variables exported by current shell. fc fc [options] [first] [last] fc -e - [old=new] [command] Display or edit commands in the history list. (Use only one of -l or -e.) fc provides capabilities similar to the C shell's history and ! syntax. first and last are numbers or strings specifying the range of commands to display or edit. If last is omitted, fc applies to a single command (specified by first). If both first and last are omitted, fc edits the previous command or lists the last 16. The second form of fc takes a history command, replaces old string with new string, and executes the modified command. If no strings are specified, command is just reexecuted. If no command is given either, the previous command is reexecuted. command is a number or string like first. See examples under Section 7.6, "Command History". Options -e [editor] Invoke editor to edit the specified history commands. The default editor is set by the shell variable FCEDIT. -l [first last] List the specified command or range of commands, or list the last 16. #BREAK# -n Suppress command numbering from the -l listing. -r Reverse the order of the -l listing. -s pattern=newpattern Edit command(s), replacing all occurrences of pattern with newpattern. Then reexecute. fg fg [jobIDs] Bring current job or jobIDs to the foreground. See Section 7.8, "Job Control". for for x [in list] do commands done Assign each word in list to x in turn and execute commands. If list is omitted, $@ (positional parameters) is assumed. Examples Paginate all files in the current directory; save each result: #BREAK# for file in * do pr $file > $file.tmpdone Search chapters for a list of words (like fgrep -f): for item in `cat program_list` do echo “Checking chapters for” echo “references to program $item…” grep -c “$item.[co]” chap* done function function command { … } Define a function. Refer to arguments the same way as positional parameters in a shell script ($1, etc.) and terminate with }. #BREAK# getopts getopts string name [args] Process command-line arguments (or args, if specified) and check for legal options. getopts is used in shell script loops and is intended to ensure standard syntax for command-line options. string contains the option letters to be recognized by getopts when running the shell script. Valid options are processed in turn and stored in the shell variable name. If an option letter is followed by a colon, the option must be followed by one or more arguments. hash hash [-r] [commands] Search for commands and remember the directory in which each command resides. Hashing causes the shell to remember the association between a “name” and the absolute pathname of an executable, so that future executions don’t require a search of PATH. With no arguments, hash lists the current hashed commands. The display shows hits (the number of times the command is called by the shell) and command (the full pathname). help help [-s] [string] Print help text on all built-in commands or those matching string. With -s, display only brief syntax, otherwise display summary paragraph also. #BREAK# history history [options] history [lines] Print a numbered command history, denoting modified commands with a *. Include commands from previous sessions. You may specify how many lines of history to print. Options -a [file] bash maintains a file called .bash_history in the user’s home directory, a record of previous sessions’ commands. Ask bash to append the current session’s commands to .bash_history or to file. -c Clear history list: remove all previously entered commands from the list remembered by the shell. -n [file] Append to the history list those lines in the .bash_history file or in file that have not yet been included. -r [file] Use .bash_history or file as the history list, instead of the working history list. -s command Add command to working history list without executing it. -w [file] Overwrite .bash_history or file with working history list. #BREAK# if if test-cmds Begin a conditional statement. Possible formats are: if test-cmds if test-cmds if test-cmdsthen then then cmds1 cmds1 cmds1fi else elif test-cmds cmds2 then fi cmds2 … else cmdsn fi Usually, the initial if and any elif lines execute one test or [] command (although any series of commands is permitted). When if succeeds (that is, the last of its test-cmds returns 0), cmds1 are performed; otherwise each succeeding elif or else line is tried. jobs jobs [options] [jobIDs] List all running or stopped jobs, or those specified by jobIDs. For example, you can check whether a long compilation or text format is still running. Also useful before logging out. See also Section 7.8, “Job Control” later in this chapter. Options -l List job IDs and process group IDs. -n #BREAK# List only jobs whose status changed since last notification. -p List process group IDs only. -r List active, running jobs only. -s List stopped jobs only. -x command [arguments] Execute command. If jobIDs are specified, replace them with command. kill kill [options] IDs Terminate each specified process ID or job ID. You must own the process or be a privileged user. See also Section 7.8, “Job Control”. Options -signal The signal number (from ps -f) or name (from kill -l). With a signal number of 9, the kill cannot be caught. The default is TERM. — Consider all subsequent strings to be arguments, not options. -l #BREAK# List the signal names. (Used by itself.) -s signal Specify signal. May be a name. let let expressions Perform arithmetic as specified by one or more integer expressions. expressions consist of numbers, operators, and shell variables (which don’t need a preceding $). Expressions must be quoted if they contain spaces or other special characters. For more information and examples, see Section 7.5, “Arithmetic Expressions” earlier in this chapter. See also expr in Chapter 3, “Linux Commands”. Examples Both of the following examples add 1 to variable i: let i=i+1let “i = i + 1″ local local [options] [variable[=value]] [variable2[=value]] … Without arguments, print all local variables. Otherwise, create (and set, if specified) one or more local variables. See the declare built-in command for options. #BREAK# logout logout [status] Exit the shell, returning status as exit status to invoking program if specified. Can be used only in a login shell. Otherwise, use exit. popd popd [options] Manipulate the directory stack. By default, remove the top directory and cd to it. Options +n Remove the nth directory in the stack, counting from 0. -n Remove nth entry from the bottom of the stack, counting from 0. #BREAK# printf printf string [arguments] Format a string like the C library printf function. Standard percent-sign formats are recognized in string, such as %i for integer. Escape sequences such as n can be included in string and are automatically recognized; if you want to include them in arguments, specify a string of %b. You can escape characters in arguments to output a string suitable for input to other commands by specifying a string of %q. Examples $ printf “Previous command: %in” “$(($HISTCMD-1))” Previous command: 534$ echo $PAGERless -E$ printf “%qn” “t$PAGER” \tless -E The last command probably would be used to record a setting in a file where it could be read and assigned by another shell script. pushd pushd directory pushd [options] By default, switch top two directories on stack. If specified, add a new directory to the top of the stack instead, and cd to it. Options +n Rotate the stack to place the nth (counting from 0) directory at the top. #BREAK# -n Rotate the stack to place the nth directory from the bottom of the stack at the top. pwd pwd [-P] Display the current working directory’s absolute pathname. By default, any symbolic directories used when reaching the current directory are displayed, but with the -P option the real names are displayed instead. read read [options] variable1 [variable2 …] Read one line of standard input, and assign each word (as defined by IFS) to the corresponding variable, with all leftover words assigned to the last variable. If only one variable is specified, the entire line will be assigned to that variable. The return status is 0 unless EOF is reached, a distinction that is useful for running loops over input files. If no variable names are provided, read the entire string into the environment variable REPLY. Options -a var Read each word into an element of var, which is treated as an array variable. -d char Stop reading the line at char instead of at the newline. -e #BREAK# Line editing and command history are enabled during input. -n num Read only num characters from the line. -p string Display the prompt string to the user before reading each line, if input is interactive. -r Raw mode; ignore as a line continuation character. -s Do not echo the characters entered by the user (useful for reading a password). -t seconds Time out and return without setting any variables if input is interactive and no input has been entered for seconds seconds. Examples $ read first last addressSarah Caldwell 123 Main Street$ echo “$last, $firstn$address” Caldwell, Sarah123 Main Street #BREAK# The following commands, which read a password into the variable $user_pw and then display its value, use recently added options that are not in all versions of bash in current use. $ read -sp “Enter password (will not appear on screen)” user_pwEnter password (will not appear on screen) $ echo $user_pwYou weren’t supposed to know! The following script reads input from the system’s password file, which uses colons to delimit fields (making it a popular subject for examples of input parsing). IFS=: cat /etc/passwd | whileread account pw user group gecos home shelldoecho “Account name $account has user info: $gecos” done readonly readonly [options] [variable1 variable2 …] Prevent the specified shell variables from being assigned new values. Variables can be accessed (read) but not overwritten. In bash, the syntax variable=value can be used to assign a new value that cannot be changed. Options -a Treat the following names as array variables. #BREAK# -f Treat the following names as functions, and set them read-only so that they cannot be changed. -p Display all read-only variables (default). return return [n] Used inside a function definition. Exit the function with status n or with the exit status of the previously executed command. select select name [ in wordlist ; ] do commands done Choose a value for name by displaying the words in wordlist to the user and prompting for a choice. Store user input in the variable REPLY and the chosen word in name. Then execute commands repeatedly until they execute a break or return. Default prompt can be changed by setting the PS3 shell variable. #BREAK# set set [options] [arg1 arg2 …] With no arguments, set prints the values of all variables known to the current shell. Options can be enabled (-option) or disabled (+option). Options also can be set when the shell is invoked, via bash. Arguments are assigned in order to $1, $2, and so on. Options - Turn off -v and -x, and turn off option processing. — Used as the last option; — turns off option processing so that arguments beginning with - are not misinterpreted as options. (For example, you can set $1 to -1.) If no arguments are given after –, unset the positional parameters. -a From now on, automatically mark variables for export after defining or changing them. -b Report background job status at termination, instead of waiting for next shell prompt. -e Exit if a command yields a nonzero exit status. -f #BREAK# Do not expand filename metacharacters (e.g., * ? [ ]). Wildcard expansion is sometimes called globbing. -h Locate commands as they are defined, and remember them. -k Assignment of environment variables (var=value) will take effect regardless of where they appear on the command line. Normally, assignments must precede the command name. -m Monitor mode. Enable job control; background jobs executes in a separate process group. -m usually is set automatically. -n Read commands, but don’t execute. Useful for checking errors, particularly for shell scripts. -o [m] List shell modes, or turn on mode m. Many modes can be set by other options. The modes can be turned off through the +o option. Modes are: allexport Same as -a. braceexpand #BREAK# Same as -B. emacs Enter Emacs editing mode (on by default). errexit Same as -e. hashall Same as -h. histexpand Same as -H. history Default. Preserve command history. ignoreeof Don’t allow use of a single Ctrl-D (the end-of-file or EOF character) to log off; use the exit command to log off. This has the same effect as setting the shell variable IGNOREEOF=1. interactive-comments Treat all words beginning with #, and all subsequent words, as comments. #BREAK# keyword Same as -k. monitor Same as -m. noclobber Same as -C. noexec Same as -n. noglob Same as -f. notify Same as -b. nounset Same as -u. onecmd #BREAK# Same as -t. physical Same as -P. posix Match POSIX standard. privileged Same as -p. verbose Same as -v. vi Enable vi-style command-line editing. xtrace Same as -x. +o [m] Display modes or turn off mode m. See the -o option for a list of modes. #BREAK# -p Start up as a privileged user; don’t process $HOME/.profile. -t Exit after one command is executed. -u Indicate an error when user tries to use a variable that is undefined. -v Show each shell command line when read. -x Show commands and arguments when executed, preceded by a + or the prompt defined by the PS4 shell variable. This provides step-by-step debugging of shell scripts. (Same as -o xtrace.) -B Default. Enable brace expansion. -C Don’t allow output redirection (>) to overwrite an existing file. -H #BREAK# Default. Enable ! and !! commands. -P Print absolute pathnames in response to pwd. By default, bash includes symbolic links in its response to pwd. Examples set — “$num” -20 -30 Set $1 to $num, $2 to -20, $3 to -30set -vx Read each command line; show it; execute it; show it again (with arguments) set +x Stop command tracingset -o noclobber Prevent file overwritingset +o noclobber Allow file overwriting again shift shift [n] Shift positional arguments (e.g., $2 becomes $1). If n is given, shift to the left n places. source source file [arguments] Read and execute lines in file. file does not have to be executable but must reside in a directory searched by PATH. #BREAK# suspend suspend [-f] Same as Ctrl-Z. Often used to stop an su command. Option -f Force suspend, even if shell is a login shell. test test condition or [ condition ] Evaluate a condition and, if its value is true, return a zero exit status; otherwise, return a nonzero exit status. An alternate form of the command uses [] rather than the word test. condition is constructed using the following expressions. Conditions are true if the description holds true. File conditions -a file file exists. -b file file is a block special file. #BREAK# -c file file is a character special file. -d file file is a directory. -e file file exists. -f file file is a regular file. -g file file has the set-group-ID bit set. -h file file is a symbolic link. -k file file has its sticky bit (no longer used) set. -p file #BREAK# file is a named pipe (FIFO). -r file file is readable. -s file file has a size greater than 0. -t [n] The open file descriptor n is associated with a terminal device; default n is 1. -u file file has its set-user-ID bit set. -w file file is writable. -x file file is executable. -G file file’s group is the process’s effective group ID. #BREAK# -L file file is a symbolic link. -N file file has been modified since its last time of access. -O file file’s owner is the process’s effective user ID. -S file file is a socket. f1 -ef f2 Files f1 and f2 are linked (refer to same file through a hard link). f1 -nt f2 File f1 is newer than f2. f1 -ot f2 File f1 is older than f2. String conditions #BREAK# -n s1 String s1 has nonzero length. -o s1 Shell option s1 is set. Shell options are described under the set built-in command. -z s1 String s1 has 0 length. s1 = s2 Strings s1 and s2 are identical. s1 == s2 Strings s1 and s2 are identical. s1 != s2 Strings s1 and s2 are not identical. s1 < s2 String s1 is lower in the alphabet (or other sort in use) than s2. By default, the check is performed character-by-character against the ASCII character set. s1 > s2 #BREAK# String s1 is higher in the alphabet (or other sort in use) than s2. string string is not null. Integer comparisons n1 -eq n2 n1 equals n2. n1 -ge n2 n1 is greater than or equal to n2. n1 -gt n2 n1 is greater than n2. n1 -le n2 n1 is less than or equal to n2. n1 -lt n2 n1 is less than n2. n1 -ne n2 #BREAK# n1 does not equal n2. Combined forms ! condition True if condition is false. condition1 -a condition2 True if both conditions are true. condition1 -o condition2 True if either condition is true. Examples Each of the following examples shows the first line of various statements that might use a test condition: while test $# -gt 0 While there are arguments . . . while [ -n “$1″ ] While the first argument is nonempty . . . if [ $count -lt 10 ] If $count is less than 10 . . . if [ -d RCS ] If the RCS directory exists . . . if [ “$answer” != “y” ] If the answer is not y . . . if [ ! -r “$1″ -o ! -f “$1″ ] If the first argument is not a readable file or a regular file . . . #BREAK# times times Print accumulated process times for user and system. trap trap [-l] [ [commands] signals] Execute commands if any of signals is received. Common signals include 0, 1, 2, and 15. Multiple commands should be quoted as a group and separated by semicolons internally. If commands is the null string (i.e., trap “”signals), then signals will be ignored by the shell. If commands is omitted entirely, reset processing of specified signals to the default action. If both commands and signals are omitted, list current trap assignments. See examples at the end of this entry and under exec. Option -l List signals. Signals Signals are listed along with what triggers them. 0 Exit from shell (usually when shell script finishes). 1 Hang up (usually logout). #BREAK# 2 Interrupt (usually through Ctrl-C). 3 Quit. 4 Illegal instruction. 5 Trace trap. 6 Abort. 7 Unused. 8 Floating-point exception. 9 #BREAK# Termination. 10 User-defined. 11 Reference to invalid memory. 12 User-defined. 13 Write to a pipe without a process to read it. 14 Alarm timeout. 15 Software termination (usually via kill). 16 Unused. #BREAK# 17 Termination of child process. 18 Continue (if stopped). 19 Stop process. 20 Process suspended (usually through Ctrl-Z). 21 Background process has tty input. 22 Background process has tty output. 23 Unused. 24 #BREAK# Unused. 25 Unused. 26 Unused. 27 Unused. 28 Unused. 29 I/O possible on a channel. Examples trap “” 2 Ignore signal 2 (interrupts) trap 2 Obey interrupts again Remove a $tmp file when the shell program exits or if the user logs out, presses Ctrl-C, or does a kill: trap “rm -f $tmp; exit” 0 1 2 15 #BREAK# type type [options] commands Report absolute pathname of programs invoked for commands and whether or not they are hashed. — Consider all subsequent strings to be arguments, not options. -a, -all Print all occurrences of command, not just that which would be invoked. -p, -path Print the hashed value of command, which may differ from the first appearance of command in the PATH. -t, -type Determine and state if command is an alias, keyword, function, built-in, or file. Example $ type mv readmv is /bin/mvread is a shell built-in #BREAK# typeset typeset See declare. ulimit ulimit [options] [n] Print the value of one or more resource limits, or, if n is specified, set a resource limit to n. Resource limits can be either hard (- H) or soft (-S). By default, ulimit sets both limits or prints the soft limit. The options determine which resource is acted on. Options — Consider all subsequent strings to be arguments, not options. -a Print all current limits. -H Hard resource limit. -S Soft resource limit. Specific limits #BREAK# These options limit specific resource sizes. -c Core files. -d Size of processes’ data segments. -f Size of shell-created files. -l Size of memory that the process can lock. -m Resident set size. -n Number of file descriptors. On many systems, this cannot be set. -p Pipe size, measured in blocks of 512 bytes. #BREAK# -s Stack size. -t Amount of CPU time, counted in seconds. -u Number of processes per user. -v Virtual memory used by shell. umask umask [nnn] umask [-p] [-S] Display file creation mask or set file creation mask to octal value nnn. The file creation mask determines which permission bits are turned off (e.g., umask 002 produces rw-rw-r–). Options -p Display mask within an umask command so that a caller can read and execute it. #BREAK# -S Display umask symbolically, rather than in octal. unalias unalias [-a] names Remove names from the alias list. See also alias. Option -a Remove all aliases. unset unset [options] names Erase definitions of functions or variables listed in names. Options -f Expect name to refer to a function. -v Expect name to refer to a variable (default). #BREAK# until until test-commands do commands done Execute test-commands (usually a test or [ ] command), and if the exit status is nonzero (that is, the test fails), perform commands; repeat. wait wait [ID] Pause in execution until all background jobs complete (exit status 0 will be returned), or pause until the specified background process ID or job ID completes (exit status of ID is returned). Note that the shell variable $! contains the process ID of the most recent background process. If job control is not in effect, ID can be only a process ID number. See Section 7.8, “Job Control”. Example wait $! Wait for last background process to finish #BREAK# while while test-commands do commands done Execute test-commands (usually a test or [] command) and if the exit status is 0, perform commands; repeat. PreviousHomeNext7.6. Command History Book Index7.8. Job Control Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 7.8. Job Control Job control lets you place foreground jobs in the background, bring background jobs to the foreground, or suspend (temporarily stop) running jobs. Job control is enabled by default. Once disabled, it can be reenabled by any of the following commands: bash -m -iset -mset -o monitor Many job control commands take jobID as an argument. This argument can be specified as follows: %n Job number n %s Job whose command line starts with string s %?s Job whose command line contains string s %% Current job %+ Current job (same as preceding) %- #BREAK# Previous job bash provides the following job control commands. For more information on these commands, see Section 7.7, “Built-in Commands” earlier in this chapter. bg Put a job in the background. fg Put a job in the foreground. jobs List active jobs. kill Terminate a job. stop Suspend a background job. stty tostop Stop background jobs if they try to send output to the terminal. wait Wait for background jobs to finish. Ctrl-Z Suspend a foreground job. Then use bg or fg to restart it in the background or foreground. (Your terminal may use something other than Ctrl-Z as the suspend character.) PreviousHomeNext7.7. Built-in Commands Book Index8. csh and tcsh Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 8. csh and tcsh Contents: Overview of FeaturesInvoking the ShellSyntaxVariablesExpressionsCommand HistoryCommand-Line ManipulationJob ControlBuilt-in csh and tcsh Commands This chapter describes the C shell and its enhancement, tcsh. On some versions of Linux, tcsh is used as the C shell; in that case, the tcsh features described in this chapter work even when you run csh. The C shell was so named because many of its programming constructs and symbols resemble those of the C programming language. The default shell on Linux systems is bash. If you want to use csh or tcsh, you first need to change your default. Each user’s shell preference is kept in the password table. If you are creating an account, you can set the default shell when you add the user. If the account already exists, use the chsh command to change the shell (see the command descriptions in Chapter 3, “Linux Commands”). The following topics are presented in this chapter: l Overview of features l Invoking the shell l Syntax #BREAK# l Variables l Expressions l Command history l Command-line manipulation l Job control l Built-in commands 8.1. Overview of Features Features of the C shell include: l Input/output redirection l Wildcard characters (metacharacters) for filename abbreviation l Shell variables for customizing your environment l Integer arithmetic l Access to previous commands (command history) l Command-name abbreviation (aliasing) l A built-in command set for writing shell programs l Job control The tcsh shell includes all of the C shell features. In addition, it includes the following extensions to the C shell: l Command-line editing and editor commands l Word completion (tab completion) l Spell checking l Extended history commands #BREAK# l Extended handling of directory manipulation l Scheduled events — such as logout or terminal locking after a set idle period and delayed commands l Additional shell built-ins l Additional shell variables and environment variables l New formatting sequences for the prompt string, as well as two new prompts (in loops and spelling correction) l Read-only variables PreviousHomeNext7.8. Job Control Book Index8.2. Invoking the Shell Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.2. Invoking the Shell A shell command interpreter can be invoked as follows: csh [options] [arguments] tcsh [options] [arguments] csh and tcsh use syntax resembling C and execute commands from a terminal or a file. Options -n, -v, and -x are useful when debugging scripts. 8.2.1. Options -b Allow the remaining command-line options to be interpreted as options to a specified command, rather than as options to csh itself. -c Execute command specified following the argument. -d Load directory stack from ~/.cshdirs even if not a login shell. (tcsh) -e Exit if a command produces errors. -f Fast startup; start without executing .cshrc or .tcshrc. -i Invoke interactive shell (prompt for input). -l Login shell (must be the only option specified). #BREAK# -m Load ~/.tcshrc even if effective user is not the owner of the file. (tcsh) -n Parse commands, but do not execute. -s Read commands from the standard input. -t Exit after executing one command. -v Display commands before executing them; expand history substitutions, but not other substitutions (e.g., filename, variable, and command). Same as setting verbose. -V Same as -v, but also display .cshrc. -x Display commands before executing them, but expand all substitutions. Same as setting echo. -X Same as -x, but also display .cshrc. 8.2.2. Arguments Arguments are assigned, in order, to the positional parameters $1, $2, and so on. If the first argument is an executable script, commands are read from it, and remaining arguments are assigned to $1, $2, and so forth. PreviousHomeNext8. csh and tcsh Book Index8.3. Syntax Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.3. Syntax This section describes the many symbols peculiar to csh and tcsh. The topics are arranged as follows: l Special files l Filename metacharacters l Quoting l Command forms l Redirection forms 8.3.1. Special Files Filename Description ~/.cshrc or ~/.tcshrc Executed at each instance of shell startup. For tcsh, if no ~/.tcshrc, uses ~/.cshrc if present. ~/.login Executed by login shell after .cshrc at login. ~/.cshdirs Executed by login shell after .login (tcsh). ~/.logout Executed by login shell at logout. /etc/passwd Source of home directories for ~name abbreviations. 8.3.2. Filename Metacharacters Characters Meaning * Match any string of 0 or more characters. ? Match any single character. [abc…] Match any one of the enclosed characters; a hyphen can be used to specify a range (e.g., a-z, A-Z, 0-9). {abc,xxx,…} Expand each comma-separated string inside braces. ~ Home directory for the current user. ~name Home directory of user name. 8.3.2.1. Examples % ls new* Match new and new.1% cat ch? Match ch9 but not ch10% vi [D-R]* Match files that begin with uppercase D through R% ls {ch,app}? Expand, then match ch1, ch2, app1, app2% cd ~tom Change to tom’s home directory 8.3.3. Quoting Quoting disables a character’s special meaning and allows it to be used literally, as itself. The following characters have special meaning to the C shell: #BREAK# Characters Description ; Command separator & Background execution ( ) Command grouping | Pipe * ? [ ] ~ Filename metacharacters { } String expansion characters (usually don’t require quoting) > < & ! Redirection symbols ! ^ History substitution, quick substitution " ' Used in quoting other characters ` Command substitution $ Variable substitution newline space tab Word separators The characters that follow can be used for quoting: " " Everything between " and " is taken literally, except for the following characters, which keep their special meaning: $ Variable substitution will occur. ` Command substitution will occur. " This marks the end of the double quote. Escape next character. ! The history character. newline The newline character. ' ' Everything between ' and ' is taken literally except for ! (history) and another ', and newline. The character following a is taken literally. Use within " " to escape ", $, and `. Often used to escape itself, spaces, or newlines. Always needed to escape a history character (usually !). #BREAK# 8.3.3.1. Examples % echo 'Single quotes "protect" double quotes'Single quotes "protect" double quotes% echo "Well, isn't that """special?"" Well, isn't that "special"? % echo "You have `ls|wc -l` files in `pwd`" You have 43 files in /home/bob% echo The value of $x is $xThe value of $x is 100 8.3.4. Command Forms Command Action cmd & Execute cmd in background. cmd1 ; cmd2 Command sequence; execute multiple cmds on the same line. (cmd1 ; cmd2) Subshell; treat cmd1 and cmd2 as a command group. cmd1 | cmd2 Pipe; use output from cmd1 as input to cmd2. cmd1 `cmd2` Command substitution; run cmd2 first and use its output as arguments to cmd1. cmd1 || cmd2 OR; execute either cmd1 or (if cmd1 fails) cmd2. cmd1 && cmd2 AND; execute cmd1 and then (if cmd1 succeeds) cmd2. 8.3.4.1. Examples % nroff file > output & Format in the background% cd; ls Execute sequentially% (date; who; pwd) > logfile All output is redirected% sort file | pr -3 | lp Sort file, page output, then print% vi `grep -l ifdef *.c` Edit files found by grep% egrep ‘(yes|no)’ `cat list` Specify a list of files to search% grep XX file && lp file Print file if it contains the pattern% grep XX file || echo XX not found Echo an error message if XX not found 8.3.5. Redirection Forms File Descriptor Name Common Abbreviation Typical Default 0 Standard input stdin Keyboard 1 Standard output stdout Screen 2 Standard error stderr Screen The usual input source or output destination can be changed with redirection commands listed in the following sections. 8.3.5.1. Simple redirection Command Action cmd > file Send output of cmd to file (overwrite). #BREAK# cmd >! file Same as preceding, even if noclobber is set. cmd >> file Send output of cmd to file (append). cmd>>! file Same as preceding, even if noclobber is set. cmd < file Take input for cmd from file. cmd << text Read standard input up to a line identical to text (text can be stored in a shell variable). Input usually is typed on the screen or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is enclosed in quotes, standard input will not undergo variable substitution, command substitution, etc. 8.3.5.2. Multiple redirection Command Action cmd >& file Send both standard output and standard error to file. cmd >&! file Same as preceding, even if noclobber is set. cmd >>& file Append standard output and standard error to end of file. cmd >>&! file Same as preceding, even if noclobber is set. cmd1 |& cmd2 Pipe standard error together with standard output. (cmd> f1) >& f2 Send standard output to file f1 and standard error to file f2. cmd | tee files Send output of cmd to standard output (usually the screen) and to files. (See the example in Chapter 3, “Linux Commands” under tee.) 8.3.5.3. Examples % cat part1 > book Copy part1 to book% cat part2 part3 >> book Append parts 2 and 3 to same file as part1% mail tim < report Take input to message from report% cc calc.c >& error_out Store all messages, including errors% cc newcalc.c >&! error_out Overwrite old file% grep Unix ch* |& pr Pipe all messages, including errors% (find / -print > filelist) >& no_access Separate error messages from list of files% sed ’s/^/XX /’ << "END_ARCHIVE" Supply text right after commandThis is often how a shell archive is "wrapped", bundling text for distribution. You would normallyrun sed from a shell program, not from the command line. "END_ARCHIVE" PreviousHomeNext8.2. Invoking the Shell Book Index8.4. Variables Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.4. Variables This subsection describes the following: l Variable substitution l Variable modifiers l Predefined shell variables l Formatting for the prompt variable l Sample .cshrc file l Environment variables 8.4.1. Variable Substitution In the following substitutions, braces ({}) are optional, except when needed to separate a variable name from following characters that would otherwise be considered part of the name: Variable Description ${var} The value of variable var. ${var[i]} Select word or words in position i of var. i can be a single number, a range m- n, a range -n (missing m implies 1), a range m- (missing n implies all remaining words), or * (select all words). i also can be a variable that expands to one of these values. ${#var} The number of words in var. ${#argv} The number of arguments. $0 Name of the program. #BREAK# ${argv[n]} Individual arguments on command line (positional parameters); 1 n 9. ${n} Same as ${argv[n]}. ${argv[*]} All arguments on command line. $* Same as {$argv[*]}. $argv[$#argv] The last argument. ${?var} Return 1 if var is set, 0 if var is not set. $$ Process number of current shell; useful as part of a filename for creating temporary files with unique names. ${?name} Return 1 if name is set, 0 if not. $?0 Return 1 if input filename is known, 0 if not. 8.4.1.1. Examples Sort the third through last arguments and save the output in a file whose name is unique to this process: sort $argv[3-] > tmp.$$ Process .cshrc commands only if the shell is interactive (i.e., the prompt variable must be set): if ($?prompt) then set commands, alias commands, etc. endif 8.4.2. Variable Modifiers Except for $?var, $$, and $?0, the variable substitutions in the preceding section may be followed by one of these modifiers (when braces are used, the modifier goes inside them): :r Return the variable’s root (the portion before the last dot). :e #BREAK# Return the variable’s extension. :h Return the variable’s header (the directory portion). :t Return the variable’s tail (the portion after the last slash). :gr Return all roots. :ge Return all extensions. :gh Return all headers. :gt Return all tails. :q Quote a wordlist variable, keeping the items separate. Useful when the variable contains filename metacharacters that should not be expanded.
Quote a pattern, expanding it into a wordlist. 8.4.2.1. Examples using pathname modifiers The following table shows the use of pathname modifiers on the following variable: set aa=(/progs/num.c /book/chap.ps) Variable Portion Specification Output Result Normal variable echo $aa /progs/num.c /book/chap.ps Second root echo $aa[2]:r /book/chap #BREAK# Second header echo $aa[2]:h /book Second tail echo $aa[2]:t chap.ps Second extension echo $aa[2]:e ps Root echo $aa:r /progs/num /book/chap.ps Global root echo $aa:gr /progs/num /book/chap Header echo $aa:h /progs /book/chap.ps Global header echo $aa:gh /progs /book Tail echo $aa:t num.c /book/chap.ps Global tail echo $aa:gt num.c chap.ps Extension echo $aa:e c /book/chap.ps Global extension echo $aa:ge c ps 8.4.2.2. Examples using quoting modifiers Unless quoted, the shell expands variables to represent files in the current directory: % set a=”[a-z]*” A=”[A-Z]*” % echo “$a” “$A” [a-z]* [A-Z]* % echo $a $Aat cc m4 Book Doc% echo $a:x $A[a-z]* Book Doc% set d=($a:q $A:q) % echo $dat cc m4 Book Doc% echo $d:q[a-z]* [A-Z]* % echo $d[1] +++ $d[2] at cc m4 +++ Book Doc% echo $d[1]:q[a-z]* 8.4.3. Predefined Shell Variables Variables can be set in one of two ways, by assigning a value: #BREAK# set var=value or by simply turning the variable on: set var In the following list, variables that accept values are shown with the equals sign followed by the type of value they accept; the value then is described. (Note, however, that variables such as argv, cwd, or status are never explicitly assigned.) For variables that are turned on or off, the table describes what they do when set. tcsh automatically sets (and, in some cases, updates) the variables addsuffix, argv, autologout, cwd, dirstack, echo-style, edit, gid, home, loginsh, logout, oid, owd, path, prompt, prompt2, prompt3, shell, shlvl, status, tcsh, term, tty, uid, user, and version. Variables in italics are specific to tcsh. Variable Description addsuffix Append / to directories and a space to files during tab completion to indicate a precise match. ampm Display all times in 12-hour format. argv=(args) List of arguments passed to current command; default is (). autocorrect Check spelling before attempting to complete commands. autoexpand Expand history (such as ! references) during command completion. autolist[=ambiguous] Print possible completions when correct one is ambiguous. If ambiguous is specified, print possible completions only when completion adds no new characters. autologout=logout-minutes [locking-minutes] Log out after logout-minutes of idle time. Lock the terminal after locking-minutes of idle time, requiring a password before continuing. Not used if the DISPLAY environment variable is set. backslash_quote Always allow backslashes to quote , ‘, and <">. #BREAK# cdpath=dirs List of alternate directories to search when locating arguments for cd, popd, or pushd. color Turn on color for ls-F, ls, or both. Setting to nothing is equivalent to setting for both. command If set, holds the command passed to the shell with the -c option. complete=enhance When enhance, ignore case in completion, treat ., -, and _ as word separators, and consider _ and - to be the same. correct={cmd|complete|all} When cmd, spellcheck commands. When complete, complete commands. When all, spellcheck whole command line. cwd=dir Full pathname of current directory. dextract When set, the pushd command extracts the desired directory and puts it at the top of the stack, instead of rotating the stack. dirsfile=file History file consulted by dirs -S and dirs - L. Default is ~/.cshdirs. dirstack Directory stack, in array format. dirstack[0] is always equivalent to cwd. The other elements can be artificially changed. dspmbyte=code Enable use of multibyte code; for use with Kanji. See the tcsh manpage for details. dunique Make sure that each directory exists only once in the stack. echo Redisplay each command line before execution; same as csh -x command. #BREAK# echo_style={bsd|sysv|both|none} Don’t echo a newline with -n option (bsd) | parse escaped characters (sysv) | do both | do neither. edit Enable command-line editor. ellipsis For use with prompt variable. Represent skipped directories with …. fignore=chars List of filename suffixes to ignore during filename completion (see filec). filec If set, a filename that is partially typed on the command line can be expanded to its full name when Esc is pressed. If more than one filename would match, type EOF to list possible completions. Ignored in tcsh. gid User’s group ID. group User’s group name. histchars=ab A two-character string that sets the characters to use in history-substitution and quick- substitution (default is !^). histdup={all|prev} Maintain a record only of unique history events (all), or do not enter new event when it is the same as the previous one (prev). histfile=file History file consulted by history -S and history -L. Default is ~/.history. histlit Do not expand history lines when recalling them. history=n format The first word indicates the number of commands to save in the history list. The second indicates the format with which to display that list (tcsh only; see the prompt section for possible formats). #BREAK# home=dir Home directory of user, initialized from HOME. The ~ character is shorthand for this value. ignoreeof Ignore an end-of-file (EOF) from terminals; prevents accidental logout. implicitcd If directory name is entered as a command, cd to that directory. Can be set to verbose to echo the cd to standard output. inputmode={insert|overwrite} Control editor’s mode. listflags=flags One or more of the x, a, or A options for the ls-F built-in command. Second word can be set to path for ls command. listjobs=long When a job is suspended, list all jobs (in long format, if specified). listlinks In ls -F command, include type of file to which links point. listmax=num Do not allow list-choices to print more than num choices before prompting. listmaxrows=num Do not allow list-choices to print more than num rows of choices before prompting. loginsh Set if shell is a login shell. logout Indicates status of an imminent logout (normal, automatic, or hangup). mail=(n files) One or more files checked for new mail every 5 minutes or (if n is supplied) every n seconds. #BREAK# matchbeep={never|nomatch|ambiguous|notunique} Specifies circumstances under which completion should beep: never, if no match exists, if multiple matches exist, or if multiple matches exist and one is exact. nobeep Disable beeping. noclobber Don’t redirect output to an existing file; prevents accidental destruction of files. noglob Turn off filename expansion; useful in shell scripts. nokanji Disable Kanji (if supported). nonomatch Treat filename metacharacters as literal characters, if no match exists (e.g., vi ch* creates new file ch* instead of printing “No match”). nostat=directory-list Do not stat directory-list during completion. notify Declare job completions when they occur. owd Old working directory. path=(dirs) List of pathnames in which to search for commands to execute. Initialized from PATH; the default is: . /usr/ucb /usr/bin printexitvalue Print all nonzero exit values. prompt=’str’ String that prompts for interactive input; default is %. See Section 8.4.4, “Formatting for the Prompt Variable” later in this chapter for formatting information. prompt2=’str’ String that prompts for interactive input in foreach and while loops and continued lines (those with escaped newlines). See Section 8.4.4, “Formatting for the Prompt Variable” for formatting information. #BREAK# prompt3=’str’ String that prompts for interactive input in automatic spelling correction. See Section 8.4.4, “Formatting for the Prompt Variable” for formatting information. promptchars=cc Use the two characters specified as cc with the %# prompt sequence to indicate normal users and the superuser, respectively. pushdsilent Do not print directory stack when pushd and popd are invoked. pushdtohome Change to home directory when pushd is invoked without arguments. recexact Consider completion to be concluded on first exact match. recognize_only_executables When command completion is invoked, print only executable files. rmstar Prompt before executing the command rm *. rprompt=string The string to print on the right side of the screen while the prompt is displayed on the left. Specify as for prompt. savedirs Execute dirs -S before exiting. savehist=max [merge] Execute history -S before exiting. Save no more than max lines of history. If specified, merge those lines with previous history saves, and sort by time. sched=string Format for sched’s printing of events. See Section 8.4.4, “Formatting for the Prompt Variable” for formatting information. shell=file Pathname of the shell program currently in use; default is /bin/csh. #BREAK# shlvl Number of nested shells. status=n Exit status of last command. Built-in commands return 0 (success) or 1 (failure). symlinks= {chase|ignore|expand} Specify manner in which to deal with symbolic links. Expand them to real directory name in cwd (chase), treat them as real directories (ignore), or expand arguments that resemble pathnames (expand). tcsh Version of tcsh. term Terminal type. time=’n %c’ If command execution takes more than n CPU seconds, report user time, system time, elapsed time, and CPU percentage. Supply optional %c flags to show other data. tperiod Number of minutes between executions of periodic alias. tty Name of tty, if applicable. uid User ID. user Username. verbose Display a command after history substitution; same as the command csh -v. version Shell’s version and additional information, including options set at compile time. visiblebell Flash screen instead of beeping. watch=([n] user terminal…) Watch for user logging in at terminal, where terminal can be a tty name or any. Check every n minutes or 10 by default. who=string Specify information to be printed by watch. #BREAK# wordchars=chars List of all nonalphanumeric characters that may be part of a word. Default is *?_-.[]~=. 8.4.4. Formatting for the Prompt Variable tcsh provides a list of substitutions that can be used in formatting the prompt. (csh allows only plain- string prompts and the ! history substitution shown in the following list.) The list of available substitutions includes: %% Literal % %/ The present working directory %~ The present working directory, in ~ notation %# # for the superuser, > for others %? Previous command’s exit status %b End boldfacing %c[[0]n], %.[[0]n] The last n (default 1) components of the present working directory; if 0 is specified, replace removed components with / %d Day of the week (e.g., Mon, Tue) %h, %!, ! Number of current history event #BREAK# %l Current tty %m First component of hostname %n Username %p Current time, with seconds (12-hour mode) %s End standout mode (reverse video) %t, %@ Current time (12-hour format) %u End underlining %w Month (e.g., Jan, Feb) %y Year (e.g., 99, 00) %B Begin boldfacing %C Similar to %c, but uses full pathnames instead of ~ notation %D #BREAK# Day of month (e.g., 09, 10) %M Fully qualified hostname %P Current time, with seconds (24-hour format) %S Begin standout mode (reverse video) %T Current time (24-hour format) %U Begin underlining %W Month (e.g., 09, 10) %Y Year (e.g., 1999, 2000) 8.4.5. Sample .cshrc File # PREDEFINED VARIABLESset path=(~ ~/bin /usr/ucb /bin /usr/bin . ) set mail=(/usr/mail/tom) if ($?prompt) then # settings for interactive use set echo set noclobber ignoreeof set cdpath=(/usr/lib /usr/spool/uucp) # Now I can type cd macros# instead of cd /usr/lib/macros set history=100 set prompt=’tom !% ‘ # includes history number set time=3 #BREAK# # MY VARIABLES set man1=”/usr/man/man1″ # lets me do cd $man1, ls $man1 set a=”[a-z]*” # lets me do vi $a set A=”[A-Z]*” # or grep string $A# ALIASES alias c “clear; dirs” # use quotes to protect ; or | alias h “history|more” alias j jobs -l alias ls ls -sFC # redefine ls command alias del ‘mv !* ~/tmp_dir’ # a safe alternative to rmendif 8.4.6. Environment Variables The C shell maintains a set of environment variables, which are distinct from shell variables and aren’t really part of the C shell. Shell variables are meaningful only within the current shell, but environment variables are exported automatically, making them available globally. For example, C-shell variables are accessible only to a particular script in which they’re defined, whereas environment variables can be used by any shell scripts, mail utilities, or editors you might invoke. Environment variables are assigned as follows: setenv VAR value By convention, environment variable names are all uppercase. You can create your own environment variables, or you can use the predefined environment variables that follow. The following environment variables have corresponding C-shell variables. When either one changes, the value is copied to the other (italics means the variable is specific to tcsh): GROUP User’s group name; same as group. HOME Home directory; same as home. PATH Search path for commands; same as path. SHLVL #BREAK# Number of nested shell levels; same as shlvl. TERM Terminal type; same as term. USER User’s login name; same as user. Other environment variables, which do not have corresponding shell variables, include the following (italics means the variable is specific to tcsh): COLUMNS Number of columns on terminal. DISPLAY Identifies user’s display for the X Window System. If set, the shell doesn’t set autologout. EDITOR Pathname to default editor. See also VISUAL. HOST Name of machine. HOSTTYPE Type of machine. Obsolete; will be removed eventually. HPATH Colon-separated list of directories to search for documentation. LANG Preferred language. Used for native language support. LC_CTYPE The locale, as it affects character handling. Used for native language support. LINES #BREAK# Number of lines on the screen. LOGNAME Another name for the USER variable. MACHTYPE Type of machine. MAIL The file that holds mail. Used by mail programs. This is not the same as the C-shell mail variable, which only checks for new mail. NOREBIND Printable characters not rebound. Used for native language support. OSTYPE Operating system. PWD The current directory; the value is copied from cwd. REMOTEHOST Machine name of remote host. SHELL Undefined by default; once initialized to shell, the two are identical. TERMCAP The file that holds the cursor-positioning codes for your terminal type. Default is /etc/termcap. VENDOR The system vendor. VISUAL Pathname to default full-screen editor. See also EDITOR. #BREAK# PreviousHomeNext8.3. Syntax Book Index8.5. Expressions Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.5. Expressions Expressions are used in @, if, and while statements to perform arithmetic, string comparisons, file testing, and so on. exit and set also specify expressions, as can the tcsh built-in command filetest. Expressions are formed by combining variables and constants with operators that resemble those in the C programming language. Operator precedence is the same as in C but can be remembered as follows: 1. * / % 2. + - Group all other expressions inside parentheses. Parentheses are required if the expression contains <, >, &, or |. 8.5.1. Operators Operators can be one of the following types: 8.5.1.1. Assignment operators Operator Description = Assign value. += -= Reassign after addition/subtraction. *= /= %= Reassign after multiplication/division/remainder. &= ^= |= Reassign after bitwise AND/XOR/OR. ++ Increment. — Decrement. 8.5.1.2. Arithmetic operators #BREAK# Operator Description * / % Multiplication; integer division; modulus (remainder) + - Addition; subtraction 8.5.1.3. Bitwise and logical operators Operator Description ~ Binary inversion (one’s complement). ! Logical negation. << >> Bitwise left shift; bitwise right shift. & Bitwise AND. ^ Bitwise exclusive OR. | Bitwise OR. && Logical AND. || Logical OR. { command } Return 1 if command is successful, 0 otherwise. Note that this is the opposite of command’s normal return code. The $status variable may be more practical. 8.5.1.4. Comparison operators Operator Description == != Equality; inequality <= >= Less than or equal to; greater than or equal to < > Less than; greater than 8.5.1.5. File inquiry operators Command substitution and filename expansion are performed on file before the test is performed. tcsh permits operators to be combined (e.g., -ef). The following is a list of the valid file inquiry operators: #BREAK# Operator Description -d file The file is a directory. -e file The file exists. -f file The file is a plain file. -o file The user owns the file. -r file The user has read permission. -w file The user has write permission. -x file The user has execute permission. -z file The file has 0 size. ! Reverse the sense of any preceding inquiry. Some additional operators specific to tcsh are: Operator Description -b file The file is a block special file. -c file The file is a character special file. -g file The file’s set-group-ID bit is set. -k file The file’s sticky bit is set. -l file The file is a symbolic link. -L file Apply any remaining operators to symbolic link, not the file it points to. -p file The file is a named pipe (FIFO). -s file The file has nonzero size. -S file The file is a socket special file. -t file file is a digit and is an open file descriptor for a terminal device. -u file The file’s set-user-ID bit is set. -X file The file is executable and is in the path or is a shell built-in. Finally, tcsh provides the following operators, which return other kinds of information: #BREAK# Operator Description -A[:] file Last time file was accessed, as the number of seconds since the Epoch. With a colon (:), the result is in timestamp format. -C[:] file Last time inode was modified. With a colon (:), the result is in timestamp format. -D file Device number. -F file Composite file identifier, in the form device:inode. -G[:] file Numeric group ID for the file. With a colon (:), the result is the group name if known; otherwise, the numeric group ID. -I file Inode number. -L file The name of the file pointed to by symbolic link file. -M[:] file Last time file was modified. With a colon (:), the result is in timestamp format. -N file Number of hard links. -P[:] file Permissions in octal, without leading 0. With a colon (:), the result includes a leading 0. -Pmode[:] file Equivalent to -P file ANDed to mode. With a colon (:), the result includes a leading 0. -U[:] file Numeric user ID of the file’s owner. With a colon (:), the result is the username if known, otherwise the numeric user ID. -Z file The file’s size, in bytes. 8.5.2. Examples The following examples show @ commands and assume n = 4: Expression Value of $x @ x = ($n > 10 || $n < 5) 1 @ x = ($n >= 0 && $n < 3) 0 @ x = ($n << 2) 16 #BREAK# @ x = ($n >> 2) 1 @ x = $n % 2 0 @ x = $n % 3 1 The following examples show the first line of if or while statements: Expression Meaning while ($#argv != 0) While there are arguments . . . if ($today[1] == <">Fri<">) If the first word is “Fri”. . . if (-f $argv[1]) If the first argument is a plain file. . . if (! -d $tmpdir) If tmpdir is not a directory. . . PreviousHomeNext8.4. Variables Book Index8.6. Command History Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.6. Command History Previously executed commands are stored in a history list. The C shell lets you access this list so you can verify commands, repeat them, or execute modified versions of them. The history built-in command displays the history list; the predefined variables histchars and history also affect the history mechanism. There are four ways to use the history list: l Rerun a previous command l Make command substitutions l Make argument substitutions (replace specific words in a command) l Extract or replace parts of a command or word The following subsections describe the csh tools for editing and rerunning commands. If you are running tcsh, you can use any of these features. In addition, you can use the arrow keys to move around in the command line and then use the editing features described in Section 8.7.5, “Command-Line Editing with tcsh” to modify the command. The tcsh arrow keys are: Key Description Up arrow Previous command. Down arrow Next command. Left arrow Move left in command line. Right arrow Move right in command line. 8.6.1. Command Substitution Command Description #BREAK# ! Begin a history substitution. !! Previous command. !N Command number N in history list. !-N Nth command back from current command. !string Most recent command that starts with string. !?string? Most recent command that contains string. !?string?% Most recent command argument that contains string. !$ Last argument of previous command. !!string Previous command, then append string. !N string Command N, then append string. !{s1}s2 Most recent command starting with string s1, then append string s2. ^old^new^ Quick substitution; change string old to new in previous command; execute modified command. 8.6.2. Command Substitution Examples The following command is assumed: %3 vi cprogs/01.c ch002 ch03 Event Command Command Number Typed Executed 4 ^00^0 vi cprogs/01.c ch02 ch03 5 nroff !* nroff cprogs/01.c ch02 ch03 6 nroff !$ nroff ch03 7 !vi vi cprogs/01.c ch02 ch03 8 !6 nroff ch03 9 !?01 vi cprogs/01.c ch02 ch03 10 !{nr}.new nroff ch03.new #BREAK# 11 !!|lp nroff ch03.new | lp 12 more !?pr?% more cprogs/01.c 8.6.3. Word Substitution Colons may precede any word specifier. Specifier Description :0 Command name :n Argument number n ^ First argument $ Last argument :n-m Arguments n through m -m Words 0 through m same as :0-m :n- Arguments n through next-to-last :n* Arguments n through last; same as n-$ * All arguments; same as ^-$ or 1-$ # Current command line up to this point; fairly useless 8.6.4. Word Substitution Examples The following command is assumed: %13 cat ch01 ch02 ch03 biblio back Event Command Command Number Typed Executed 14 ls !13^ ls ch01 15 sort !13:* sort ch01 ch02 ch03 biblio back 16 lp !cat:3* more ch03 biblio back #BREAK# 17 !cat:0-3 cat ch01 ch02 ch03 18 vi !-5:4 vi biblio 8.6.5. History Modifiers Command and word substitutions can be modified by one or more of the following modifiers: 8.6.5.1. Printing, substitution, and quoting Modifier Description :p Display command, but don’t execute. :s/old/new Substitute string new for old, first instance only. :gs/old/new Substitute string new for old, all instances. :& Repeat previous substitution (:s or ^ command), first instance only. :g& Repeat previous substitution, all instances. :q Quote a wordlist.
Quote separate words. 8.6.5.2. Truncation Modifier Description :r Extract the first available pathname root (the portion before the last period). :gr Extract all pathname roots. :e Extract the first available pathname extension (the portion after the last period). :ge Extract all pathname extensions. :h Extract the first available pathname header (the portion before the last slash). :gh Extract all pathname headers. :t Extract the first available pathname tail (the portion after the last slash). :gt Extract all pathname tails. :u Make first lowercase letter uppercase (tcsh only). #BREAK# :l Make first uppercase letter lowercase (tcsh only). :a Apply modifier(s) following a as many times as possible to a word. If used with g, a is applied to all words (tcsh only). 8.6.6. History Modifier Examples From the preceding, command number 17 is: %17 cat ch01 ch02 ch03 Event Command Command Number Typed Executed 19 !17:s/ch/CH/ cat CH01 ch02 ch03 20 !17g& cat CH01 CH02 CH03 21 !more:p more cprogs/01.c (displayed only) 22 cd !$:h cd cprogs 23 vi !mo:$:t vi 01.c 24 grep stdio !$ grep stdio 01.c 25 ^stdio^include stdio^:q grep <">include stdio<"> 01.c 26 nroff !21:t:p nroff 01.c (is that what I wanted?) 27 !! nroff 01.c (execute it) 8.6.7. Special Aliases in tcsh Certain special aliases can be set in tcsh. The aliases are initially undefined. Once set, they are executed when specific events occur. The following is a list of the special aliases: beepcmd At beep. cwdcmd When the current working directory changes. #BREAK# periodic Every few minutes. The exact amount of time is set by the tperiod shell variable. precmd Before printing a new prompt. shell shell If a script does not specify a shell, interpret it with shell, which should be a full pathname. PreviousHomeNext8.5. Expressions Book Index8.7. Command-Line Manipulation Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.7. Command-Line Manipulation csh and tcsh offer a certain amount of functionality in manipulating the command line. Both shells offer word or command completion, and tcsh allows you to edit a command line. 8.7.1. Completion Both tcsh and csh provide word completion. tcsh automatically completes words and commands when the Tab key is hit; csh does so only when the filec variable is set, after the Esc key is hit. If the completion is ambiguous (i.e., more than one file matches the provided string), the shell completes as much as possible and beeps to notify you that the completion is not finished. You may request a list of possible completions with Ctrl-D. tcsh also notifies you when a completion is finished by appending a space to complete filenames or commands and a / to complete directories. Both csh and tcsh recognize ~ notation for home directories. The shells assume that words at the beginning of a line and subsequent to |, &, ;, ||, or && are commands and modify their search paths appropriately. Completion can be done midword; only the letters to the left of the prompt are checked for completion. 8.7.2. Related Shell Variables l autolist l fignore l listmax l listmaxrows 8.7.3. Related Command-Line Editor Commands l complete-word-back #BREAK# l complete-word-forward l expand-glob l list-glob 8.7.4. Related Shell Built-ins l complete l uncomplete 8.7.5. Command-Line Editing with tcsh tcsh lets you move your cursor around in the command line, editing the line as you type. There are two main modes for editing the command line, based on the two most common text editors: Emacs and vi. Emacs mode is the default; you can switch between the modes with: bindkey -e Select Emacs bindingsbindkey -v Select vi bindings The main difference between the Emacs and vi bindings is that the Emacs bindings are modeless (i.e., they always work). With the vi bindings, you must switch between insert and command modes; different commands are useful in each mode. Additionally: l Emacs mode is simpler; vi mode allows finer control. l Emacs mode allows you to yank cut text and set a mark; vi mode does not. l The command-history-searching capabilities differ. 8.7.5.1. Emacs mode Table 8-1 through Table 8-3 describe the various editing keystrokes available in Emacs mode. Table 8-1. Cursor Positioning Commands (Emacs Mode) Command Description Ctrl-B Move cursor back (left) one character. Ctrl-F Move cursor forward (right) one character. #BREAK# Esc b Move cursor back one word. Esc f Move cursor forward one word. Ctrl-A Move cursor to beginning of line. Ctrl-E Move cursor to end of line. Table 8-2. Text Deletion Commands (Emacs Mode) Command Description Del or Ctrl-H Delete character to left of cursor. Ctrl-D Delete character under cursor. Esc d Delete word. Esc Del or Esc Ctrl-H Delete word backward. Ctrl-K Delete from cursor to end-of-line. Ctrl-U Delete entire line. Table 8-3. Command Control (Emacs Mode) Command Description Ctrl-P Previous command. Ctrl-N Next command. Up arrow Previous command. Down arrow Next command. cmd-fragment Esc p Search history for cmd-fragment, which must be the beginning of a command. cmd-fragment Esc n Like Esc p, but search forward. Esc num Repeat next command num times. Ctrl-Y Yank previously deleted string. 8.7.5.2. vi mode #BREAK# vi mode has two submodes, insert mode and command mode. The default mode is insert. You can toggle modes by pressing Esc; alternatively, in command mode, typing a (append) or i (insert) will return you to insert mode. Tables 8-4 through 8-10 describe the editing keystrokes available in vi mode. Table 8-4. Commands Available (vi’s Insert and Command Mode) Command Description Ctrl-P Previous command Ctrl-N Next command Up arrow Previous command Down arrow Next command Esc Toggle mode Table 8-5. Editing Commands (vi Insert Mode) Command Description Ctrl-B Move cursor back (left) one character. Ctrl-F Move cursor forward (right) one character. Ctrl-A Move cursor to beginning of line. Ctrl-E Move cursor to end-of-line. DEL or Ctrl-H Delete character to left of cursor. Ctrl-W Delete word backward. Ctrl-U Delete from beginning of line to cursor. Ctrl-K Delete from cursor to end-of-line. Table 8-6. Cursor Positioning Commands (vi Command Mode) Command Description h or Ctrl-H Move cursor back (left) one character. l or SPACE Move cursor forward (right) one character. #BREAK# w Move cursor forward (right) one word. b Move cursor back (left) one word. e Move cursor to next word ending. W, B, E Like w, b, and e, but treat just whitespace as word separator instead of any non-alphanumeric character. ^ or Ctrl-A Move cursor to beginning of line (first nonwhitespace character). 0 Move cursor to beginning of line. $ or Ctrl-E Move cursor to end-of-line. Table 8-7. Text Insertion Commands (vi Command Mode) Command Description a Append new text after cursor until Esc. i Insert new text before cursor until Esc. A Append new text after end of line until Esc. I Insert new text before beginning of line until Esc. Table 8-8. Text Deletion Commands (vi Command Mode) Command Description x Delete character under cursor. X or Del Delete character to left of cursor. dm Delete from cursor to end of motion command m. D Same as d$. Ctrl-W Delete word backward. Ctrl-U Delete from beginning of line to cursor. Ctrl-K Delete from cursor to end of line. Table 8-9. Text Replacement Commands (vi Command Mode) #BREAK# Command Description cm Change characters from cursor to end of motion command m until Esc. C Same as c$. rc Replace character under cursor with character c. R Replace multiple characters until Esc. s Substitute character under cursor with characters typed until Esc. Table 8-10. Character-Seeking Motion Commands (vi Command Mode) Command Description fc Move cursor to next instance of c in line. Fc Move cursor to previous instance of c in line. tc Move cursor just before next instance of c in line. Tc Move cursor just after previous instance of c in line. ; Repeat previous f or F command. , Repeat previous f or F command in opposite direction. PreviousHomeNext8.6. Command History Book Index8.8. Job Control Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.8. Job Control Job control lets you place foreground jobs in the background, bring background jobs to the foreground, or suspend (temporarily stop) running jobs. The C shell provides the following commands for job control. For more information on these commands, see Section 8.9, “Built- in csh and tcsh Commands”. bg Put a job in the background. fg Put a job in the foreground. jobs List active jobs. kill Terminate a job. notify Notify when a background job finishes. stop Suspend a background job. Ctrl-Z Suspend the foreground job. Many job control commands take jobID as an argument. This argument can be specified as follows: %n #BREAK# Job number n. %s Job whose command line starts with string s. %?s Job whose command line contains string s. %% Current job. % Current job (same as preceding). %+ Current job (same as preceding). %- Previous job. PreviousHomeNext8.7. Command-Line Manipulation Book Index8.9. Built-in csh and tcsh Commands Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 8.9. Built-in csh and tcsh Commands @ @ [variable[n]=expression] Assign the value of the arithmetic expression to variable or to the nth element of variable if the index n is specified. With no variable or expression specified, print the values of all shell variables (same as set). Expression operators as well as examples are listed under Section 8.5, “Expressions” earlier in this chapter. Two special forms also are valid: @ variable++ Increment variable by 1. @ variable– Decrement variable by 1. # # Ignore all text that follows on the same line. # is used in shell scripts as the comment character and is not really a command. #BREAK# #! #!shell Used as the first line of a script to invoke the named shell (with optional arguments). Not supported in all shells. For example: #!/bin/csh -f : : Null command. Returns an exit status of 0. The colon command often is put as the first character of a Bourne- or Korn-shell script to act as a place-holder to keep a # (hash) from accidentally becoming the first character. alias alias [name [command]] Assign name as the shorthand name, or alias, for command. If command is omitted, print the alias for name; if name also is omitted, print all aliases. Aliases can be defined on the command line, but more often they are stored in .cshrc so that they take effect upon logging in. (See the sample .cshrc file earlier in this chapter.) Alias definitions can reference command-line arguments, much like the history list. Use !* to refer to all command-line arguments, !^ for the first argument, !!:2 for the second, !$ for the last, and so on. An alias name can be any valid Unix command; however, you lose the original command’s meaning unless you type name. See also unalias and the “Special Aliases in tcsh” section. Examples Set the size for xterm windows under the X Window System: alias R ’set noglob; eval `resize` unset noglob’ Show aliases that contain the string ls: alias | grep ls #BREAK# Run nroff on all command-line arguments: alias ms ‘nroff -ms !*’ Copy the file that is named as the first argument: alias back ‘cp !^ !^.old’ Use the regular ls, not its alias: % ls alloc alloc Print totals of used and free memory. bg bg [jobIDs] Put the current job or the jobIDs in the background. Example To place a time-consuming process in the background, you might begin with: 4% nroff -ms report Ctrl-Z and then issue any one of the following: #BREAK# 5% bg5% bg % Current job5% bg %1 Job number 15% bg %nr Match initial string nroff5% % & bindkey bindkey [options] [key] [command] tcsh only. Display all key bindings, or bind a key to a command. Options -a List standard and alternate key bindings. -b key Expect key to be one of the following: a control character (in hat notation — e.g., ^B — or C notation — e.g., C-B); a metacharacter (e.g., M-B); a function key (e.g., F-string); or an extended prefix key (e.g., X-B). -c command Interpret command as a shell, not editor, command. -d key Bind key to its original binding. #BREAK# -e Bind to standard Emacs bindings. -k key Expect key to refer to an arrow (left, right, up, or down). -l List and describe all editor commands. -r key Completely unbind key. -s Interpret command as a literal string and treat as terminal input. -u Print usage message. -v Bind to standard vi bindings. #BREAK# break break Resume execution following the end command of the nearest enclosing while or foreach. breaksw breaksw Break from a switch; continue execution after the endsw. built-ins built-ins tcsh only. Print all built-in shell commands. bye bye tcsh only. Same as logout. case case pattern : Identify a pattern in a switch. #BREAK# cd cd [dir] Change working directory to dir. Default is user’s home directory. If dir is a relative pathname but is not in the current directory, the cdpath variable is searched. See the sample .cshrc file earlier in this chapter. tcsh includes some options for cd: tcsh options - Change to previous directory. -l Explicitly expand ~ notation. -n Wrap entries before end-of-line; implies -p. -p Print directory stack. -v Print entries one per line; implies -p. #BREAK# chdir chdir [dir] Same as cd. Useful if you are redefining cd. complete complete [string [word/pattern/list[:select]/[suffix]]] tcsh only. List all completions, or, if specified, all completions for string (which may be a pattern). Further options can be specified. Options for word c Complete current word only and without referring to pattern. C Complete current word only, referring to pattern. n Complete previous word. N Complete word before previous word. p #BREAK# Expect pattern to be a range of numbers. Perform completion within that range. Options for list Various lists of strings can be searched for possible completions. Some list options include: (string) Members of the list string $variable Words from variable `command` Output from command a Aliases b Bindings c Commands #BREAK# C External (not built-in) commands d Directories D Directories whose names begin with string e Environment variables f Filenames F Filenames that begin with string g Groups j #BREAK# Jobs l Limits n Nothing s Shell variables S Signals t Text files T Text files whose names begin with string u Users #BREAK# v Any variables x Like n but prints select as an explanation with the editor command list-choices X Completions select select should be a glob pattern. Completions are limited to words that match this pattern. suffix is appended to all completions. continue continue Resume execution of nearest enclosing while or foreach. default default : Label the default case (typically last) in a switch. #BREAK# dirs dirs [options] Print the directory stack, showing the current directory first. See also popd and pushd. All options except -l, -n, and -v are tcsh extensions. Options -c Clear the directory stack. -l Expand the home directory symbol (~) to the actual directory name. -n Wrap output. -v Print one directory per line. -L file Re-create stack from file, which should have been created by dirs -S file. -S file Print to file a series of pushd and popd commands, that can be invoked to replicate the stack. #BREAK# echo echo [-n] string Write string to standard output; if -n is specified, the output is not terminated by a newline. Unlike the Unix version (/bin/echo) and the Bourne-shell version, the C shell’s echo doesn’t support escape characters. See also echo in Chapter 3, “Linux Commands”, and Chapter 7, “bash: The Bourne-Again Shell”. echotc echotc [options] arguments tcsh only. Display terminal capabilities, or move cursor on screen, depending on the argument. Options -s Return empty string, not error, if capability doesn’t exist. -v Display verbose messages. Arguments baud Display current baud. cols #BREAK# Display current column. cm column row Move cursor to specified coordinates. home Move cursor to home position. lines Print number of lines per screen. meta Does this terminal have meta capacity (usually the Alt key)? tabs Does this terminal have tab capacity? else else Reserved word for interior of if … endif statement. #BREAK# end end Reserved word that ends a foreach or switch statement. endif endif Reserved word that ends an if statement. endsw endsw Reserved word that ends a switch statement. eval eval args Typically, eval is used in shell scripts, and args is a line of code that may contain shell variables. eval forces variable expansion to happen first and then runs the resulting command. This “double scanning” is useful any time shell variables contain input/output redirection symbols, aliases, or other shell variables. (For example, redirection normally happens before variable expansion, so a variable containing redirection symbols must be expanded first using eval; otherwise, the redirection symbols remain uninterpreted.) Examples The following line can be placed in the .login file to set up terminal characteristics: set noglob eval `tset -s xterm` unset noglob The following commands show the effect of eval: #BREAK# % set b=’$a’ % set a=hello% echo $b Read the command line once$a% eval echo $b Read the command line twicehello Another example of eval can be found under alias. exec exec command Execute command in place of current shell. This terminates the current shell, rather than create a new process under it. exit exit [(expr)] Exit a shell script with the status given by expr. A status of zero means success; nonzero means failure. If expr is not specified, the exit value is that of the status variable. exit can be issued at the command line to close a window (log out). #BREAK# fg fg [jobIDs] Bring the current job or the jobIDs to the foreground. jobID can be %job-number. Example If you suspend a vi editing session (by pressing Ctrl-Z), you might resume vi using any of these commands: % % % fg% fg % % fg %vi Match initial string filetest filetest -op files tcsh only. Apply op file-test operator to files. Print results in a list. See Section 8.5.1.5, “File inquiry operators” for the list of file-test operators. #BREAK# foreach foreach name (wordlist) commands end Assign variable name to each value in wordlist and execute commands between foreach and end. You can use foreach as a multiline command issued at the C-shell prompt (first of the following examples), or you can use it in a shell script (second example). Examples Rename all files that begin with a capital letter: % foreach i ([A-Z]*) ? mv $i $i.new? end Check whether each command-line argument is an option or not: foreach arg ($argv) # does it begin with - ? if (”$arg” =~ -*) then echo “Argument is an option” else echo “Argument is a filename” endifend #BREAK# glob glob wordlist Do filename, variable, and history substitutions on wordlist. No escapes are recognized in its expansion, and words are delimited by null characters. glob typically is used in shell scripts to hardcode a value so that it remains the same for the rest of the script. goto goto string Skip to a line whose first nonblank character is string followed by a colon and continue execution below that line. On the goto line, string can be a variable or filename pattern, but the label branched to must be a literal, expanded value and must not occur within a foreach or while. hashstat hashstat Display statistics that show the hash table’s level of success at locating commands via the path variable. history history [options] Display the list of history events. (History syntax is discussed earlier, in “Command History.”) Options -c tcsh only. Clear history list. -h #BREAK# Print history list without event numbers. -r Print in reverse order; show oldest commands last. n Display only the last n history commands, instead of the number set by the history shell variable. -L file tcsh only. Load series of pushd and popd commands from file in order to re-create a saved stack. -M file tcsh only. Merge the current directory stack and the stack saved in file. Save both, sorted by time, in file, as a series of pushd and popd commands. -S file tcsh only. Print to file a series of pushd and popd commands that can be invoked to replicate the stack. Example To save and execute the last five commands: history -h 5 > do_itsource do_it #BREAK# hup hup [command] tcsh only. Start command but make it exit when sent a hangup signal, which is sent when shell exits. By default, configure shell script to exit on hangup signal. if if Begin a conditional statement. The simple format is: if (expr) cmd There are three other possible formats, shown side-by-side: if (expr) then if (expr) then if (expr) then cmds cmds1 cmds1endif else else if (expr) then cmds2 cmds2 endif else cmds3 endif In the simplest form, execute cmd if expr is true; otherwise do nothing (redirection still occurs; this is a bug). In the other forms, execute one or more commands. If expr is true, continue with the commands after then; if expr is false, branch to the commands after else (or branch to after the else if and continue checking). For more examples, see Section 8.5, “Expressions” earlier in this chapter, shift, or while. Example Take a default action if no command-line arguments are given: #BREAK# if ($#argv == 0) then echo “No filename given. Sending to Report.” set outfile = Reportelse set outfile = $argv[1] endif jobs jobs [-l] List all running or stopped jobs; -l includes process IDs. For example, you can check whether a long compilation or text format is still running. Also useful before logging out. kill kill [options] ID Terminate each specified process ID or job ID. You must own the process or be a privileged user. This built-in is similar to /bin/kill described in Chapter 3, “Linux Commands” but also allows symbolic job names. Stubborn processes can be killed using signal 9. Options -l List the signal names. (Used by itself.) -signal The signal number or name, without the SIG prefix (e.g., HUP, not SIGHUP). The command kill -l prints a list of the available signal names. The list varies by system architecture; for a PC-based system, it looks like this: % kill -l #BREAK# HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR UNUSED The signals and their numbers are defined in /usr/include/asm/signal.h; look in that file to find the signals that apply to your system. Examples If you’ve issued the following command: 44% nroff -ms report & you can terminate it in any of the following ways: 45% kill 19536 Process ID45% kill % Current job45% kill %1 Job number 145% kill %nr Initial string45% kill %?report Matching string #BREAK# limit limit [-h] [resource [limit]] Display limits or set a limit on resources used by the current process and by each process it creates. If no limit is given, the current limit is printed for resource. If resource also is omitted, all limits are printed. By default, the current limits are shown or set; with -h, hard limits are used. A hard limit imposes an absolute limit that can’t be exceeded. Only a privileged user may raise it. See also unlimit. Option -h Use hard, not current, limits. Resource cputime Maximum number of seconds the CPU can spend; can be abbreviated as cpu. filesize Maximum size of any one file. datasize Maximum size of data (including stack). stacksize Maximum size of stack. #BREAK# coredumpsize Maximum size of a core dump file. Limit A number followed by an optional character (a unit specifier). For cputime: nh (for n hours) nm (for n minutes) mm:ss (minutes and seconds) For others: nk (for n kilobytes, the default) nm (for n megabytes) log log tcsh only. Consult the watch variable for list of users being watched. Print list of those who are presently logged in. If - is entered as an option, reset environment as if user had logged in with new group. login login [user|-p] Replace user’s login shell with /bin/login. -p is used to preserve environment variables. #BREAK# logout logout Terminate the login shell. ls-F ls-F [options] [files] tcsh only. Faster alternative to ls -F. If given any options, invokes ls. newgrp newgrp [-] [group] tcsh only. Change user’s group ID to specified group ID, or, if none is specified, to original group ID. If - is entered as an option, reset environment as if user had logged in with new group. Must have been compiled into the shell; see the version variable. nice nice [+n] command Change the execution priority for command, or, if none is given, change priority for the current shell. (See also nice in Chapter 3, “Linux Commands”.) The priority range is -20 to 20, with a default of 4. The range seems backward: -20 gives the highest priority (fastest execution); 20 gives the lowest. Only a privileged user may specify a negative number. +n Add n to the priority value (lower job priority). -n Subtract n from the priority value (raise job priority). Privileged users only. #BREAK# nohup nohup [command] “No hangup signals.” Do not terminate command after terminal line is closed (i.e., when you hang up from a phone or log out). Use without command in shell scripts to keep script from being terminated. (See also nohup in Chapter 3, “Linux Commands”.) notify notify [jobID] Report immediately when a background job finishes (instead of waiting for you to exit a long editing session, for example). If no jobID is given, the current background job is assumed. onintr onintr label onintr - onintr “On interrupt.” Used in shell scripts to handle interrupt signals (similar to bash’s trap 2 and trap “” 2 commands). The first form is like a goto label. The script will branch to label: if it catches an interrupt signal (e.g., Ctrl-C). The second form lets the script ignore interrupts. This is useful at the beginning of a script or before any code segment that needs to run unhindered (e.g., when moving files). The third form restores interrupt handling that was previously disabled with onintr -. Example onintr cleanup Go to “cleanup” on interrupt . . Shell script commands . cleanup: Label for interrupts onintr - Ignore additional interrupts #BREAK# rm -f $tmpfiles Remove any files created exit 2 Exit with an error status popd popd [options] Remove the current entry from the directory stack, or remove the nth entry from the stack and print the stack that remains. The current entry has number 0 and appears on the left. See also dirs and pushd. Options +n Specify nth entry. -l Expand ~ notation. -n Wrap long lines. -p Override the pushdsilent shell variable, which otherwise prevents the printing of the final stack. -v Print precisely one directory per line. #BREAK# printenv printenv [variable] Print all (or one specified) environment variables and their values. pushd pushd name pushd [options] pushd The first form changes the working directory to name and adds it to the directory stack. The second form rotates the nth entry to the beginning, making it the working directory. (Entry numbers begin at 0.) With no arguments, pushd switches the first two entries and changes to the new current directory. The +n, -l, -n, and -v options behave the same as in popd. See also dirs and popd. Examples % dirs/home/bob /usr% pushd /etc Add /etc to directory stack/etc /home/bob /usr% pushd +2 Switch to third directory/usr /etc /home/bob% pushd Switch top two directories/etc /usr /home/bob% popd Discard current entry; go to next/usr /home/bob #BREAK# rehash rehash Recompute the internal hash table for the PATH variable. Use rehash whenever a new command is created during the current session. This allows the PATH variable to locate and execute the command. (If the new command resides in a directory not listed in PATH, add this directory to PATH before rehashing.) See also unhash. repeat repeat n command Execute n instances of command. Examples Print three copies of memo: % repeat 3 pr memo | lp Read 10 lines from the terminal and store in item_list: % repeat 10 line > item_list Append 50 boilerplate files to report: % repeat 50 cat template >> report #BREAK# sched sched [options] sched time command tcsh only. Without options, print all scheduled events. The second form schedules an event. time should be specified in hh:mm form (e.g., 13:00). Options +hh:mm Schedule event to take place hh:mm from now. -n Remove nth item from schedule. set set variable=value set [option] variable[n]=value set Set variable to value, or if multiple values are specified, set the variable to the list of words in the value list. If an index n is specified, set the nth word in the variable to value. (The variable must already contain at least that number of words.) With no arguments, display the names and values of all set variables. See also “Predefined Shell Variables” earlier in this chapter. Option #BREAK# -r tcsh only. List only read-only variables, or set specified variable to read-only. Examples % set list=(yes no maybe) Assign a wordlist% set list[3]=maybe Assign an item in existing wordlist% set quote=”Make my day” Assign a variable% set x=5 y=10 history=100 Assign several variables% set blank Assign a null value to blank setenv setenv [name [value]] Assign a value to an environment variable name. By convention, name is uppercase. value can be a single word or a quoted string. If no value is given, the null value is assigned. With no arguments, display the names and values of all environment variables. setenv is not necessary for the PATH variable, which is automatically exported from path. settc settc capability value tcsh only. Set terminal capability to value. #BREAK# setty setty [options] [+|-mode] tcsh only. Do not allow shell to change specified tty modes. By default, act on the execute set. Options +mode Without arguments, list all modes in specified set that are on. Otherwise, set specified mode to on. -mode Without arguments, list all modes in specified set that are off. Otherwise, set specified mode to on. -a List all modes in specified set. -d Act on the edit set of modes (used when editing commands). -q Act on the quote set of modes (used when entering characters verbatim). -x Act on the execute set of modes (default) (used when executing examples). #BREAK# shift shift [variable] If variable is given, shift the words in a wordlist variable (i.e., name[2] becomes name[1]). With no argument, shift the positional parameters (command-line arguments) (i.e., $2 becomes $1). shift is typically used in a while loop. See additional example under while. Example while ($#argv) While there are arguments if (-f $argv[1]) wc -l $argv[1] else echo “$argv[1] is not a regular file” endif shift Get the next argumentend source source [-h] script [args] Read and execute commands from a C-shell script. With -h, the commands are added to the history list but aren’t executed. For tcsh only, arguments can be passed to the script and are put in argv. Example source ~/.cshrc #BREAK# stop stop [jobIDs] Suspend the current background jobs or the background jobs specified by jobIDs; this is the complement of Ctrl-Z or suspend. suspend suspend Suspend the current foreground job; same as Ctrl-Z. Often used to stop an su command. switch switch Process commands depending on the value of a variable. When you need to handle more than three choices, switch is a useful alternative to an if-then-else statement. If the string variable matches pattern1, the first set of commands is executed; if string matches pattern2, the second set of commands is executed; and so on. If no patterns match, execute commands under the default case. string can be specified using command substitution, variable substitution, or filename expansion. Patterns can be specified using the pattern-matching symbols *, ?, and []. breaksw is used to exit the switch. If breaksw is omitted (which is rarely done), the switch continues to execute another set of commands until it reaches a breaksw or endsw. Following is the general syntax of switch, side-by-side with an example that processes the first command-line argument: switch (string) switch ($argv[1]) case pattern1: case -[nN]: commands nroff $file | lp breaksw breaksw case pattern2: case -[Pp]: commands pr $file | lp breaksw breaksw case pattern3: case -[Mm]: commands more $file breaksw breaksw . case -[Ss]: #BREAK# . sort $file . breaksw default: default: commands echo “Error — no such option” exit 1 breaksw breakswendsw endsw telltc telltc tcsh only. Print all terminal capabilities and their values. time time [command] Execute a command and show how much time it uses. With no argument, time can be used in a shell script to time the script. umask umask [nnn] Display file creation mask or set file creation mask to octal nnn. The file creation mask determines which permission bits are turned off. With no nnn, print the current mask. unalias unalias pattern Remove all aliases whose names match pattern from the alias list. See alias for more information. #BREAK# uncomplete uncomplete pattern tcsh only. Remove completions (specified by complete) whose names match pattern. unhash unhash Remove internal hash table. The shell stops using hashed values and searches the path directories to locate a command. See also rehash. unlimit unlimit [-h] [resource] Remove the allocation limits on resource. If resource is not specified, remove limits for all resources. See limit for more information. With -h, remove hard limits. This command can be run only by a privileged user. unset unset variables Remove one or more variables. Variable names may be specified as a pattern, using filename metacharacters. Does not remove read-only variables. See set. unsetenv unsetenv variable Remove an environment variable. Filename matching is not valid. See setenv. #BREAK# wait wait Pause in execution until all child processes complete, or until an interrupt signal is received. watchlog watchlog tcsh only. Same as log. Must have been compiled into the shell; see the version shell variable. where where command tcsh only. Display all aliases, built-ins, and executables named command. which which command tcsh only. Report which version of command will be executed. Same as the executable which, but faster, and checks tcsh built- ins. #BREAK# while while (expression) commands end As long as expression is true (evaluates to nonzero), evaluate commands between while and end. break and continue can be used to terminate or continue the loop. Example set user = (alice bob carol ted) while ($argv[1] != $user[1]) Cycle through each user, checking for a match shift user If we cycled through with no match… if ($#user == 0) then echo “$argv[1] is not on the list of users” exit 1 endifend PreviousHomeNext8.8. Job Control Book Index9. Pattern Matching Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 9. Pattern Matching Contents: Filenames Versus PatternsMetacharacters, Listed by Linux ProgramMetacharactersExamples of Searching A number of Linux text-editing utilities let you search for and, in some cases change, text patterns rather than fixed strings. These utilities include the editing programs ed, ex, vi, and sed; the awk scripting language; and the commands grep and egrep. Text patterns (also called regular expressions) contain normal characters mixed with special characters (also called metacharacters). Perl’s regular expression support is so rich that it does not fit into the tables in this chapter; you can find a description in the O’Reilly books Perl in a Nutshell, Perl 5 Pocket Reference, or Programming Perl. The Emacs editor also provides regular expressions similar to those shown in this chapter. ed and ex are hardly ever used as standalone, interactive editors nowadays. But ed can be found as a batch processor invoked from shell scripts, and ex commands often are invoked within vi through the colon (:) command. We use vi in this chapter to refer to the regular expression features supported by both vi and the ex editor on which it is based. sed and awk are widely used in shell scripts and elsewhere as filters to alter text. This chapter presents the following information: l Filenames versus patterns l List of metacharacters available to each program l Description of metacharacters #BREAK# l Examples A thorough guide to pattern matching can be found in the Nutshell handbook Mastering Regular Expressions by Jeffrey E. F. Friedl. 9.1. Filenames Versus Patterns Metacharacters used in pattern matching are different from those used for filename expansion. When you issue a command on the command line, special characters are seen first by the shell, then by the program; therefore, unquoted metacharacters are interpreted by the shell for filename expansion. The command: $ grep [A-Z]* chap[12] could, for example, be interpreted by the shell as: $ grep Array.c Bug.c Comp.c chap1 chap2 and grep then would try to find the pattern “Array.c” in files Bug.c, Comp.c, chap1, and chap2. To bypass the shell and pass the special characters to grep, use quotes: $ grep “[A-Z]*” chap[12] Double quotes suffice in most cases, but single quotes are the safest bet. Note also that * and ? have subtly different meanings in pattern matching and filename expansion. PreviousHomeNext8.9. Built-in csh and tcsh Commands Book Index9.2. Metacharacters, Listed by Linux Program Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 9.2. Metacharacters, Listed by Linux Program Some metacharacters are valid for one program but not for another. Those that are available to a given program are marked by a bullet () in the following table. Notes are provided after the table, and full descriptions of metacharacters are in the following section. Symbol ed vi sed awk grep egrep Action . Match any character (can match newline in gawk). * Match zero or more preceding. ^ Match beginning of line or string. $ Match end of line or string. Escape character following. [] Match one from a list or range. () Store pattern for later replay. n Reuse matched text stored in nth ( ). {} Match a range of instances. {} Match a range of instances. <> Match word’s beginning or end. + Match one or more preceding. ? Match zero or one preceding. | Separate choices to match. () Group expressions to match. #BREAK# On some Linux systems, grep is a link to egrep, so whenever you run grep you actually get egrep behavior. In ed, vi, and sed, when you perform a search-and-replace (substitute) operation, the metacharacters in this table apply to the pattern you are searching for but not to the string replacing it. In awk, {} is specified in the POSIX standard and is supported by gawk if you run it with the - Wre-interval option. In ed, vi, and sed, the following additional metacharacters are valid only in a replacement pattern: Symbol ex sed ed Action Escape character following. n Reuse matched text stored in nth ( ). & Reuse previous search pattern. ~ Reuse previous replacement pattern. e Turn off previous L or U. E Turn off previous /L or /U. l Change single following character to lowercase. L Change following characters to lowercase until /E encountered. u Change single following character to uppercase. U Change following characters to uppercase until E encountered. PreviousHomeNext9. Pattern Matching Book Index9.3. Metacharacters Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 9.3. Metacharacters The following characters have special meaning in search patterns: Character Meaning . Match any single character except newline. * Match any number (or none) of the single character that immediately precedes it. The preceding character also can be a regular expression (e.g., since . (dot) means any character, .* means match any number of any character — except newlines). ^ Match the beginning of the line or string. $ Match the end of the line or string. [ ] Match any one of the enclosed characters. A hyphen (-) indicates a range of consecutive characters. A circumflex (^) as the first character in the brackets reverses the sense: it matches any one character not in the list. A hyphen or close bracket (]) as the first character is treated as a member of the list. All other metacharacters are treated as members of the list. [^ ] Match anything except enclosed characters. {n,m} Match a range of occurrences of the single character that immediately precedes it. The preceding character also can be a regular expression. {n} matches exactly n occurrences, {n,} matches at least n occurrences, and {n,m} matches any number of occurrences between n and m. {n,m} Like {n,m}. Available in grep by default and in gawk with the -Wre- interval option. #BREAK# Turn off the special meaning of the character that follows. () Save the matched text enclosed between ( and ) in a special holding space. Up to nine patterns can be saved on a single line. They can be “replayed” in the same pattern or within substitutions by the escape sequences 1 to 9. n Reuse matched text stored in nth ( ). () In egrep and gawk, save the matched text enclosed between ( and ) in a holding space to be replayed in substitutions by the escape sequences 1 to 9. <> Match the beginning (<) or end (>) of a word. + Match one or more instances of preceding regular expression. ? Match zero or one instance of preceding regular expression. | Match the regular expression specified before or after. () Group regular expressions. Many utilities support POSIX character lists, which are useful for matching non-ASCII characters in languages other than English. These lists are recognized only within [] ranges. A typical use would be [[:lower:]], which in English is the same as [a-z]. The following table lists POSIX character lists: Notation Action [:alnum:] Alphanumeric characters [:alpha:] Alphabetic characters, uppercase and lowercase [:blank:] Printable whitespace: spaces and tabs but not control characters [:cntrl:] Control characters, such as ^A through ^Z [:digit:] Decimal digits [:graph:] Printable characters, excluding whitespace [:lower:] Lowercase alphabetic characters [:print:] Printable characters, including whitespace but not control characters [:punct:] Punctuation, a subclass of printable characters [:space:] Whitespace, including spaces, tabs, and some control characters #BREAK# [:upper:] Uppercase alphabetic characters [:xdigit:] Hexadecimal digits The following characters have special meaning in replacement patterns: Character Meaning Turn off the special meaning of the character that follows. n Restore the nth pattern previously saved by ( and ). n is a number from 1 to 9, matching the patterns searched sequentially from left to right. & Reuse the search pattern as part of the replacement pattern. ~ Reuse the previous replacement pattern in the current replacement pattern. e End replacement pattern started by L or U. E End replacement pattern started by L or U. l Convert first character of replacement pattern to lowercase. L Convert replacement pattern to lowercase. u Convert first character of replacement pattern to uppercase. U Convert replacement pattern to uppercase. PreviousHomeNext9.2. Metacharacters, Listed by Linux Program Book Index9.4. Examples of Searching Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 9.4. Examples of Searching When used with grep or egrep, regular expressions normally are surrounded by quotes to avoid interpretation by the shell. (If the pattern contains a $, you must use single quotes, as in ‘$200′, or escape the $, as in “$200″.) When used with ed, vi, sed, and awk, regular expressions usually are surrounded by / (although any delimiter works). Here are some sample patterns: Pattern What does it match? bag The string bag. ^bag “bag” at beginning of line or string. bag$ “bag” at end of line or string. ^bag$ “bag” as the only text on line. [Bb]ag “Bag” or “bag.” b[aeiou]g Second character is a vowel. b[^aeiou]g Second character is not a vowel. b.g Second character is any character except newline. ^…$ Any line containing exactly three characters. ^. Any line that begins with a dot. ^.[a-z][a-z] Same, followed by two lowercase letters (e.g., troff requests). ^.[a-z]{2} Same as previous, grep or sed only. ^[^.] Any line that doesn’t begin with a dot. bugs* “bug,” “bugs”, “bugss”, etc. “word” A word in quotes. “*word”* A word, with or without quotes. [A-Z][A-Z]* One or more uppercase letters. [A-Z]+ Same, egrep or awk only. [A-Z].* An uppercase letter, followed by zero or more characters. [A-Z]* Zero or more uppercase letters. [a-zA-Z] Any letter. #BREAK# [0-9A-Za-z]+ Any alphanumeric sequence. egrep or awk pattern What does it match? [567] One of the numbers 5, 6, or 7 five|six|seven One of the words five, six, or seven 80[23]?86 8086, 80286, or 80386 compan(y|ies) company or companies vi pattern What does it match?
Words like breathe or the
The word the sed or grep pattern What does it match? 0{5,} Five or more zeros in a row [0-9]{3}-[0-9]{2}-[0-9]{4} Social security number (nnn-nn-nnnn) 9.4.1. Examples of Searching and Replacing The following examples show the metacharacters available to sed and vi. We have shown vi commands with an initial colon because that is how they are invoked within vi. A space is marked by a ; a tab is marked by tab. Command Result s/.*/(&)/ Reproduce the entire line, but add parentheses. s/.*/mv & &.old/ Change a wordlist (one word per line) into mv commands. /^$/d Delete blank lines. :g/^$/d Same as previous, in vi editor. /^[tab]*$/d Delete blank lines, plus lines containing spaces or tabs. :g/^[tab]*$/d Same as previous, in vi editor. s/*//g Turn one or more spaces into one space. :%s/*//g Same as previous, in vi editor. :s/[0-9]/Item &:/ Turn a number into an item label (on the current line). :s Repeat the substitution on the first occurrence. :& Same as previous. :sg Same, but for all occurrences on the line. :&g Same as previous. #BREAK# :%&g Repeat the substitution globally. :.,$s/Fortran/U&/g Change word to uppercase, on current line to last line. :%s/.*/L&/ Lowercase entire file. :s/<./u&/g Uppercase first letter of each word on current line. (Useful for titles.) :%s/yes/No/g Globally change a word (yes) to another word (No). :%s/Yes/~/g Globally change a different word to No (previous replacement). Finally, here are some sed examples for transposing words. A simple transposition of two words might look like this: s/die or do/do or die/ Transpose words The real trick is to use hold buffers to transpose variable patterns. For example: s/([Dd]ie) or ([Dd]o)/2 or1/ Transpose, using hold buffers PreviousHomeNext9.3. Metacharacters Book Index10. The Emacs Editor Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 10. The Emacs Editor Contents: IntroductionTypical ProblemsSummary of Commands by GroupSummary of Commands by KeySummary of Commands by Name This chapter presents the following topics: l Introduction l Typical problems l Summary of Emacs commands by group l Summary of Emacs commands by key l Summary of Emacs commands by name 10.1. Introduction Although Emacs is not part of Linux, this text editor is found on many Unix systems because it is a popular alternative to vi. Many versions are available. This book documents GNU Emacs, which is available from the Free Software Foundation in Cambridge, Massachusetts. For more information, see the O'Reilly book Learning GNU Emacs, 2d ed., by Debra Cameron, Bill Rosenblatt, and Eric Raymond. To start an Emacs editing session, type: emacs [file] PreviousHomeNext9.4. Examples of Searching Book Index10.2. Typical Problems #BREAK# Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 10.2. Typical Problems A very common problem is that the Del or Backspace key on the terminal does not delete the character before the cursor, as it should. Instead, it invokes a help prompt. This problem is caused by an incompatible terminal. A fairly robust fix is to create a file named .emacs in your home directory (or edit one that's already there) and add the following lines: (keyboard-translate ?C-h ?C-?) (keyboard-translate ?C-\ ?C-h) Now the Del or Backspace kill should work, and you can invoke help by pressing C- (an arbitrarily chosen key sequence). Another potential problem is that on some systems, C-s causes the terminal to hang. This is due to an old-fashioned handshake protocol between the terminal and the system. You can restart the terminal by pressing C-q, but that doesn't help you enter commands that contain the sequence C-s. The solution (aside from using a more modern dial-in protocol) is to create new key bindings that replace C-s or to enter those commands as M-x command-name. 10.2.1. Notes on the Tables Emacs commands use the Ctrl key and the Meta key. Most modern terminals provide a key named Alt that functions as a Meta key. In this section, the notation C- indicates that you should hold down the Ctrl key and press the character that follows, while M- indicates that the Meta or Alt key is pressed in the same way, along with the character that follows. As an alternative to Meta or Alt, you can press the Esc key, release it, and press the character. You might want to do this if you have any problems with controlling windows capturing the Alt key (which sometimes happens). In the command tables that follow, the first column lists the keystroke and the last column describes it. When there is a middle column, it lists the command name. The command can be executed by typing M-x followed by the command name; you have to do this when the binding is listed as "(none)." If you're unsure of the full command name, you can type a space or a carriage return, and Emacs will list possible completions of what you've typed so far. #BREAK# Because Emacs is such a comprehensive editor, containing hundreds of commands, some commands must be omitted for the sake of preserving a "quick" reference. You can browse the command set by typing C-h (for help) and then b to get a list of the key bindings[6] or M- x followed by a space or Tab to get the command names. [6]If you want to learn to create your own key bindings, see Learning GNU Emacs (O'Reilly). 10.2.2. Modes One of the features that makes Emacs popular is its editing modes. The modes set up an environment designed for the type of editing you are doing, with features like having appropriate key bindings available and automatically indenting according to standard conventions for that type of document. There are modes for various programming languages like C or Perl, for text processing (e.g., SGML or even straight text), and many more. One particularly useful mode is Dired (Directory Editor), which has commands that let you manage directories. For a full discussion of modes, see Learning GNU Emacs, mentioned at the beginning of this chapter, or the Emacs Info documentation system (C-h i). 10.2.3. Absolutely Essential Commands If you're just getting started with Emacs, here's a short list of the most important commands to know: Binding Action C-h Enter the online help system. C-x C-s Save the file. C-x C-c Exit Emacs. C-x u Undo last edit (can be repeated). C-g Get out of current command operation. C-p Up by one line. C-n Down by one line. C-f Forward by one character. C-b Back by one character. C-v Forward by one screen. M-v Backward by one screen. #BREAK# C-s Search forward for characters. C-r Search backward for characters. C-d Delete current character. Del Delete previous character. Backspace Delete previous character. PreviousHomeNext10. The Emacs Editor Book Index10.3. Summary of Commands by Group Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 10.3. Summary of Commands by Group Reminder: Tables list keystrokes, command name, and description. C- indicates the Control key; M- indicates the Meta key. 10.3.1. File-Handling Commands Binding Command Action C-x C-f find-file Find file and read it. C-x C-v find-alternate-file Read another file; replace the one read currently in the buffer. C-x i insert-file Insert file at cursor position. C-x C-s save-buffer Save file. (If terminal hangs, C-q restarts.) C-x C-w write-file Write buffer contents to file. C-x C-c save-buffers-kill-emacs Exit Emacs. C-z suspend-emacs Suspend Emacs (use exit or fg to restart). 10.3.2. Cursor Movement Commands Some words are emphasized in the Action column to help you remember the binding for the command. Binding Command Action C-f forward-char Move forward one character (right). C-b backward-char Move backward one character (left). C-p previous-line Move to previous line (up). #BREAK# C-n next-line Move to next line (down). M-f forward-word Move one word forward. M-b backward-word Move one word backward. C-a beginning-of-line Move to beginning of line. C-e end-of-line Move to end of line. M-a backward-sentence Move backward one sentence. M-e forward-sentence Move forward one sentence. M-{ backward-paragraph Move backward one paragraph. M-} forward-paragraph Move forward one paragraph. C-v scroll-up Move forward one screen. M-v scroll-down Move backward one screen. C-x [ backward-page Move backward one page. C-x ] forward-page Move forward one page. M-> end-of-buffer Move to end-of-file. M-< beginning-of-buffer Move to beginning of file. (none) goto-line Go to line n of file. (none) goto-char Go to character n of file. C-l recenter Redraw screen with current line in the center. M-n digit-argument Repeat the next command n times. C-u n universal-argument Repeat the next command n times. 10.3.3. Deletion Commands Binding Command Action Del backward-delete-char Delete previous character. C-d delete-char Delete character under cursor. M-Del backward-kill-word Delete previous word. #BREAK# M-d kill-word Delete the word the cursor is on. C-k kill-line Delete from cursor to end-of-line. M-k kill-sentence Delete sentence the cursor is on. C-x Del backward-kill-sentence Delete previous sentence. C-y yank Restore what you've deleted. C-w kill-region Delete a marked region (see Section 10.3.4, "Paragraphs and Regions"). (none) backward-kill-paragraph Delete previous paragraph. (none) kill-paragraph Delete from the cursor to the end of the paragraph. 10.3.4. Paragraphs and Regions Binding Command Action C-@ set-mark-command Mark the beginning (or end) of a region. C-Space (Same as preceding) (Same as preceding) C-x C-p mark-page Mark page. C-x C-x exchange-point-and-mark Exchange location of cursor and mark. C-x h mark-whole-buffer Mark buffer. M-q fill-paragraph Reformat paragraph. (none) fill-region Reformat individual paragraphs within a region. M-h mark-paragraph Mark paragraph. M-{ backward-paragraph Move backward one paragraph. M-} forward-paragraph Move forward one paragraph. (none) backward-kill-paragraph Delete previous paragraph. (none) kill-paragraph Delete from the cursor to the end of the paragraph. 10.3.5. Stopping and Undoing Commands #BREAK# Binding Command Action C-g keyboard-quit Abort current command. C-x u advertised-undo Undo last edit (can be done repeatedly). (none) revert-buffer Restore buffer to the state it was in when the file was last saved (or auto-saved). 10.3.6. Transposition Commands Binding Command Action C-t transpose-chars Transpose two letters. M-t transpose-words Transpose two words. C-x C-t transpose-lines Transpose two lines. (none) transpose-sentences Transpose two sentences. (none) transpose-paragraphs Transpose two paragraphs. 10.3.7. Capitalization Commands Binding Command Action M-c capitalize-word Capitalize first letter of word. M-u upcase-word Uppercase word. M-l downcase-word Lowercase word. M- - M-c negative-argument; capitalize-word Capitalize previous word. M- - M-u negative-argument; upcase-word Uppercase previous word. M- - M-l negative-argument; downcase-word Lowercase previous word. (none) capitalize-region Capitalize initial letters in region. C-x C-u upcase-region Uppercase region. C-x C-l downcase-region Lowercase region. 10.3.8. Incremental Search Commands #BREAK# Binding Command Action C-s isearch-forward Start or repeat incremental search forward. C-r isearch-backward Start or repeat incremental search backward. Return (none) Exit a successful search. C-g keyboard-quit Cancel incremental search; return to starting point. Del (none) Delete incorrect character of search string. M-C-r isearch-backward-regexp Incremental search backward for regular expression. M-C-s isearch-forward-regexp Incremental search forward for regular expression. 10.3.9. Word Abbreviation Commands Binding Command Action (none) abbrev-mode Enter (or exit) word abbreviation mode. C-x a - inverse-add- global-abbrev Define previous word as global (mode- independent) abbreviation. C-x a i l inverse-add- mode-abbrev Define previous word as mode-specific abbreviation. (none) unexpand-abbrev Undo the last word abbreviation. (none) write-abbrev-file Write the word abbreviation file. (none) edit-abbrevs Edit the word abbreviations. (none) list-abbrevs View the word abbreviations. (none) kill-all-abbrevs Kill abbreviations for this session. 10.3.10. Buffer Manipulation Commands Binding Command Action C-x b switch-to-buffer Move to specified buffer. C-x C-b list-buffers Display buffer list. C-x k kill-buffer Delete specified buffer. #BREAK# (none) kill-some-buffers Ask about deleting each buffer. (none) rename-buffer Change buffer name to specified name. C-x s save-some-buffers Ask whether to save each modified buffer. 10.3.11. Window Commands Binding Command Action C-x 2 split-window-vertically Divide the current window in two vertically, resulting in one window on top of the other. C-x 3 split-window-horizontally Divide the current window in two horizontally, resulting in two side-by-side windows. C-x > scroll-right Scroll the window right. C-x < scroll-left Scroll the window left. C-x o other-window Move to the other window. C-x 0 delete-window Delete current window. C-x 1 delete-other-windows Delete all windows but this one. (none) delete-windows-on Delete all windows on a given buffer. C-x ^ enlarge-window Make window taller. (none) shrink-window Make window shorter. C-x } enlarge-window- horizontally Make window wider. C-x { shrink-window- horizontally Make window narrower. M-C-v scroll-other-window Scroll other window. C-x 4 f find-file-other-window Find a file in the other window. C-x 4 b switch-to-buffer-other-window Select a buffer in the other window. C-x 5 f find-file-other-frame Find a file in a new frame. C-x 5 b switch-to-buffer-other-frame Select a buffer in another frame. (none) compare-windows Compare two buffers; show first difference. 10.3.12. Special Shell Mode Characters #BREAK# Binding Command Action C-c C-c interrupt-shell-subjob Terminate the current job. C-c C-d shell-send-eof End-of-file character. C-c C-u kill-shell-input Erase current line. C-c C-w backward-kill-word Erase the previous word. C-c C-z stop-shell-subjob Suspend the current job. 10.3.13. Indentation Commands Binding Command Action C-x . set-fill-prefix Prepend each line in paragraph with characters from beginning of line up to cursor column; cancel prefix by typing this command in column 1. (none) indented-text-mode Major mode: each tab defines a new indent for subsequent lines. (none) text-mode Exit indented text mode; return to text mode. M-C- indent-region Indent a region to match first line in region. M-m back-to-indentation Move cursor to first character on line. M-^ delete-indentation Join this line to the previous line. M-C-o split-line Split line at cursor; indent to column of cursor. (none) fill-individual- paragraphs Reformat indented paragraphs, keeping indentation. 10.3.14. Centering Commands Binding Command Action (none) center-line Center line that cursor is on. (none) center-paragraph Center paragraph that cursor is on. (none) center-region Center currently defined region. 10.3.15. Macro Commands #BREAK# Binding Command Action C-x ( start-kbd-macro Start macro definition. C-x ) end-kbd-macro End macro definition. C-x e call-last-kbd-macro Execute last macro defined. M-n C-x e digit-argument and call-last-kbd- macro Execute last macro defined n times. C-u C-x ( start-kbd-macro Execute last macro defined, then add keystrokes. (none) name-last-kbd-macro Name last macro you created (before saving it). (none) insert-last-keyboard- macro Insert the macro you named into a file. (none) load-file Load macro files you've saved. (none) macroname Execute a keyboard macro you've saved. C-x q kbd-macro-query Insert a query in a macro definition. C-u C-x q (none) Insert a recursive edit in a macro definition. M-C-c exit-recursive-edit Exit a recursive edit. 10.3.16. Detail Information Help Commands Binding Command Action C-h a command-apropos What commands involve this concept? (none) apropos What commands, functions, and variables involve this concept? C-h c describe-key-briefly What command does this keystroke sequence run? C-h b describe-bindings What are all the key bindings for this buffer? C-h k describe-key What command does this keystroke sequence run, and what does it do? #BREAK# C-h l view-lossage What are the last 100 characters I typed? C-h w where-is What is the key binding for this command? C-h f describe-function What does this function do? C-h v describe-variable What does this variable mean, and what is its value? C-h m describe-mode Tell me about the mode the current buffer is in. C-h s describe-syntax What is the syntax table for this buffer? 10.3.17. Help Commands Binding Command Action C-h t help-with-tutorial Run the Emacs tutorial. C-h i info Start the Info documentation reader. C-h n view-emacs-news View news about updates to Emacs. C-h C-c describe-copying View the Emacs General Public License. C-h C-d describe-distribution View information on ordering Emacs from the FSF. C-h C-w describe-no-warranty View the (non)warranty for Emacs. PreviousHomeNext10.2. Typical Problems Book Index10.4. Summary of Commands by Key Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 10.4. Summary of Commands by Key Emacs commands are presented next in two alphabetical lists. Tables list keystrokes, command name, and description. C- indicates the Ctrl key; M- indicates the Meta key. 10.4.1. Control-Key Sequences Binding Command Action C-@ set-mark-command Mark the beginning (or end) of a region. C-Space (Same as preceding) (Same as preceding) C-] abort-recursive-edit Exit recursive edit and exit query-replace. C-a beginning-of-line Move to beginning of line. C-b backward-char Move backward one character (left). C-c C-c interrupt-shell-subjob Terminate the current job. C-c C-d shell-send-eof End-of-file character. C-c C-u kill-shell-input Erase current line. C-c C-w backward-kill-word Erase the previous word. C-c C-z stop-shell-subjob Suspend the current job. C-d delete-char Delete character under cursor. C-e end-of-line Move to end of line. C-f forward-char Move forward one character (right). C-g keyboard-quit Abort current command. C-h help-command Enter the online help system. #BREAK# C-h a command-apropos What commands involve this concept? C-h b describe-bindings What are all the key bindings for this buffer? C-h c describe-key-briefly What command does this keystroke sequence run? C-h C-c describe-copying View the Emacs General Public License. C-h C-d describe-distribution View information on ordering Emacs from the FSF. C-h C-w describe-no-warranty View the (non)warranty for Emacs. C-h f describe-function What does this function do? C-h i info Start the Info documentation reader. C-h k describe-key What command does this keystroke sequence run, and what does it do? C-h l view-lossage What are the last 100 characters I typed? C-h m describe-mode Tell me about the mode the current buffer is in. C-h n view-emacs-news View news about updates to Emacs. C-h s describe-syntax What is the syntax table for this buffer? C-h t help-with-tutorial Run the Emacs tutorial. C-h v describe-variable What does this variable mean, and what is its value? C-h w where-is What is the key binding for this command? C-k kill-line Delete from cursor to end-of-line. C-l recenter Redraw screen with current line in the center. C-n next-line Move to next line (down). C-p previous-line Move to previous line (up). C-q quoted-insert Insert next character typed. Useful for inserting a control character. C-r isearch-backward Start or repeat nonincremental search backward. C-r (none) Enter recursive edit (during query replace). #BREAK# C-s isearch-forward Start or repeat nonincremental search forward. C-t transpose-chars Transpose two letters. C-u n universal-argument Repeat the next command n times. C-u C-x ( start-kbd-macro Execute last macro defined, then add keystrokes. C-u C-x q (none) Insert recursive edit in a macro definition. C-v scroll-up Move forward one screen. C-w kill-region Delete a marked region. C-x ( start-kbd-macro Start macro definition. C-x ) end-kbd-macro End macro definition. C-x [ backward-page Move backward one page. C-x ] forward-page Move forward one page. C-x ^ enlarge-window Make window taller. C-x { shrink-window- horizontally Make window narrower. C-x } enlarge-window- horizontally Make window wider. C-x < scroll-left Scroll the window left. C-x > scroll-right Scroll the window right. C-x . set-fill-prefix Prepend each line in paragraph with characters from beginning of line up to cursor column; cancel prefix by typing this command in column 1. C-x 0 delete-window Delete current window. C-x 1 delete-other-windows Delete all windows but this one. C-x 2 split-window-vertically Divide current window in two vertically, resulting in one window on top of the other. C-x 3 split-window-horizontally Divide current window in two horizontally, resulting in two side-by-side windows. C-x 4 b switch-to-buffer-other-window Select a buffer in the other window. C-x 4 f find-file-other-window Find a file in the other window. #BREAK# C-x 5 b switch-to-buffer-other-frame Select a buffer in another frame. C-x 5 f find-file-other-frame Find a file in a new frame. C-x a - inverse-add-global-abbrev Define previous word as global (mode- independent) abbreviation. C-x a i l inverse-add-mode-abbrev Define previous word as mode-specific abbreviation. C-x b switch-to-buffer Move to the buffer specified. C-x C-b list-buffers Display the buffer list. C-x C-c save-buffers-kill-emacs Exit Emacs. C-x C-f find-file Find file and read it. C-x C-l downcase-region Lowercase region. C-x C-p mark-page Place cursor and mark around whole page. C-x C-q (none) Toggle read-only status of buffer. C-x C-s save-buffer Save file. (If terminal hangs, C-q restarts.) C-x C-t transpose-lines Transpose two lines. C-x C-u upcase-region Uppercase region. C-x C-v find-alternate-file Read an alternate file, replacing the one currently in the buffer. C-x C-w write-file Write buffer contents to file. C-x C-x exchange-point-and-mark Exchange location of cursor and mark. C-x Del backward-kill- sentence Delete previous sentence. C-x e call-last-kbd-macro Execute last macro defined. C-x h mark-whole-buffer Place cursor and mark around whole buffer. C-x i insert-file Insert file at cursor position. C-x k kill-buffer Delete the buffer specified. C-x o other-window Move to the other window. C-x q kbd-macro-query Insert a query in a macro definition. C-x s save-some-buffers Ask whether to save each modified buffer. #BREAK# C-x u advertised-undo Undo last edit (can be done repeatedly). C-y yank Restore what you’ve deleted. C-z suspend-emacs Suspend Emacs (use exit or fg to restart). 10.4.2. Meta-Key Sequences Binding Command Action M– M-c negative-argument; capitalize-word Capitalize previous word. M– M-l negative-argument; downcase-word Lowercase previous word. M– M-u negative-argument; upcase-word Uppercase previous word. M-$ spell-word Check spelling of word after cursor. M-< beginning-of-buffer Move to beginning of file. M-> end-of-buffer Move to end-of-file. M-{ backward-paragraph Move backward one paragraph. M-} forward-paragraph Move forward one paragraph. M-^ delete-indentation Join this line to the previous one. M-n digit-argument Repeat the next command n times. M-n C-x e digit-argument; call-last-kbd-macro Execute the last defined macro n times. M-a backward-sentence Move backward one sentence. M-b backward-word Move one word backward. M-c capitalize-word Capitalize first letter of word. M-C- indent-region Indent a region to match first line in region. M-C-c exit-recursive-edit Exit a recursive edit. M-C-o split-line Split line at cursor; indent to column of cursor. M-C-r isearch-backward-regexp Incremental search backward for regular expression. #BREAK# M-C-s isearch-forward-regexp Incremental search forward for regular expression. M-C-v scroll-other-window Scroll other window. M-d kill-word Delete word that cursor is on. M-Del backward-kill-word Delete previous word. M-e forward-sentence Move forward one sentence. M-f forward-word Move one word forward. (none) fill-region Reformat individual paragraphs within a region. M-h mark-paragraph Place cursor and mark around whole paragraph. M-k kill-sentence Delete sentence the cursor is on. M-l downcase-word Lowercase word. M-m back-to-indentation Move cursor to first nonblank character on line. M-q fill-paragraph Reformat paragraph. M-t transpose-words Transpose two words. M-u upcase-word Uppercase word. M-v scroll-down Move backward one screen. M-x (none) Execute a command by typing its name. PreviousHomeNext10.3. Summary of Commands by Group Book Index10.5. Summary of Commands by Name Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 10.5. Summary of Commands by Name The following Emacs commands are presented alphabetically by command name. Use M-x to access the command name. Tables list command name, keystroke, and description. C- indicates the Ctrl key; M- indicates the Meta key. Command Binding Action macroname (none) Execute a keyboard macro you’ve saved. abbrev-mode (none) Enter (or exit) word abbreviation mode. abort-recursive-edit C-] Exit recursive edit and exit query- replace. advertised-undo C-x u Undo last edit (can be done repeatedly). apropos (none) What functions and variables involve this concept? back-to-indentation M-m Move cursor to first nonblank character on line. backward-char C-b Move backward one character (left). backward-delete-char Del Delete previous character. backward-kill-paragraph (none) Delete previous paragraph. backward-kill-sentence C-x Del Delete previous sentence. backward-kill-word C-c C-w Erase previous word. backward-kill-word M-Del Delete previous word. backward-page C-x [ Move backward one page. backward-paragraph M-{ Move backward one paragraph. #BREAK# backward-sentence M-a Move backward one sentence. backward-word M-b Move backward one word. beginning-of-buffer M-< Move to beginning of file. beginning-of-line C-a Move to beginning of line. call-last-kbd-macro C-x e Execute last macro defined. capitalize-region (none) Capitalize region. capitalize-word M-c Capitalize first letter of word. center-line (none) Center line that cursor is on. center-paragraph (none) Center paragraph that cursor is on. center-region (none) Center currently defined region. command-apropos C-h a What commands involve this concept? compare-windows (none) Compare two buffers; show first difference. delete-char C-d Delete character under cursor. delete-indentation M-^ Join this line to previous one. delete-other-windows C-x 1 Delete all windows but this one. delete-window C-x 0 Delete current window. delete-windows-on (none) Delete all windows on a given buffer. describe-bindings C-h b What are all the key bindings for in this buffer? describe-copying C-h C-c View the Emacs General Public License. describe-distribution C-h C-d View information on ordering Emacs from the FSF. describe-function C-h f What does this function do? describe-key C-h k What command does this keystroke sequence run, and what does it do? describe-key-briefly C-h c What command does this keystroke sequence run? #BREAK# describe-mode C-h m Tell me about the mode the current buffer is in. describe-no-warranty C-h C-w View the (non)warranty for Emacs. describe-syntax C-h s What is the syntax table for this buffer? describe-variable C-h v What does this variable mean, and what is its value? digit-argument M-n Repeat next command n times. downcase-region C-x C-l Lowercase region. downcase-word M-l Lowercase word. edit-abbrevs (none) Edit word abbreviations. end-kbd-macro C-x ) End macro definition. end-of-buffer M-> Move to end-of-file. end-of-line C-e Move to end-of-line. enlarge-window C-x ^ Make window taller. enlarge-window-horizontally C-x } Make window wider. exchange-point-and-mark C-x C-x Exchange location of cursor and mark. exit-recursive-edit M-C-c Exit a recursive edit. fill-individual-paragraphs (none) Reformat indented paragraphs, keeping indentation. fill-paragraph M-q Reformat paragraph. fill-region (none) Reformat individual paragraphs within a region. find-alternate-file C-x C-v Read an alternate file, replacing the one currently in the buffer. find-file C-x C-f Find file and read it. find-file-other-frame C-x 5 f Find a file in a new frame. find-file-other-window C-x 4 f Find a file in the other window. forward-char C-f Move forward one character (right). forward-page C-x ] Move forward one page. #BREAK# forward-paragraph M-} Move forward one paragraph. forward-sentence M-e Move forward one sentence. forward-word M-f Move forward one word. goto-char (none) Go to character n of file. goto-line (none) Go to line n of file. help-command C-h Enter the online help system. help-with-tutorial C-h t Run the Emacs tutorial. indent-region M-C- Indent a region to match first line in region. indented-text-mode (none) Major mode: each tab defines a new indent for subsequent lines. info C-h i Start the Info documentation reader. insert-file C-x i Insert file at cursor position. insert-last-keyboard-macro (none) Insert the macro you named into a file. interrupt-shell-subjob C-c C-c Terminate the current job (shell mode). inverse-add-global-abbrev C-x a - Define previous word as global (mode- independent) abbreviation. inverse-add-mode-abbrev C-x a i l Define previous word as mode-specific abbreviation. isearch-backward C-r Start incremental search backward. isearch-backward-regexp M-C-r Same, but search for regular expression. isearch-forward C-s Start incremental search forward. isearch-forward-regexp M-C-s Same, but search for regular expression. kbd-macro-query C-x q Insert a query in a macro definition. keyboard-quit C-g Abort current command. kill-all-abbrevs (none) Kill abbreviations for this session. kill-buffer C-x k Delete the buffer specified. kill-line C-k Delete from cursor to end-of-line. #BREAK# kill-paragraph (none) Delete from cursor to end of paragraph. kill-region C-w Delete a marked region. kill-sentence M-k Delete sentence the cursor is on. kill-shell-input C-c C-u Erase current line. kill-some-buffers (none) Ask about deleting each buffer. kill-word M-d Delete word the cursor is on. list-abbrevs (none) View word abbreviations. list-buffers C-x C-b Display buffer list. load-file (none) Load macro files you’ve saved. mark-page C-x C-p Place cursor and mark around whole page. mark-paragraph M-h Place cursor and mark around whole paragraph. mark-whole-buffer C-x h Place cursor and mark around whole buffer. name-last-kbd-macro (none) Name last macro you created (before saving it). negative-argument; capitalize- word M– M-c Capitalize previous word. negative-argument; downcase- word M– M-l Lowercase previous word. negative-argument; upcase- word M– M-u Uppercase previous word. next-line C-n Move to next line (down). other-window C-x o Move to the other window. previous-line C-p Move to previous line (up). query-replace-regexp (none) Query-replace a regular expression. quoted-insert C-q Insert next character typed. Useful for inserting a control character. #BREAK# recenter C-l Redraw screen, with current line in center. rename-buffer (none) Change buffer name to specified name. replace-regexp (none) Replace a regular expression unconditionally. re-search-backward (none) Simple regular-expression search backward. re-search-forward (none) Simple regular-expression search forward. revert-buffer (none) Restore buffer to the state it was in when the file was last saved (or auto- saved). save-buffer C-x C-s Save file. (If terminal hangs, C-q restarts.) save-buffers-kill-emacs C-x C-c Exit Emacs. save-some-buffers C-x s Ask whether to save each modified buffer. scroll-down M-v Move backward one screen. scroll-left C-x < Scroll the window left. scroll-other-window M-C-v Scroll other window. scroll-right C-x > Scroll the window right. scroll-up C-v Move forward one screen. set-fill-prefix C-x . Prepend each line in paragraph with characters from beginning of line up to cursor column; cancel prefix by typing this command in column 1. set-mark-command C-@ or C-Space Mark the beginning (or end) of a region. shell-send-eof C-c C-d End-of-file character (shell mode). shrink-window (none) Make window shorter. shrink-window-horizontally C-x { Make window narrower. spell-buffer (none) Check spelling of current buffer. #BREAK# spell-region (none) Check spelling of current region. spell-string (none) Check spelling of string typed in minibuffer. spell-word M-$ Check spelling of word after cursor. split-line M-C-o Split line at cursor; indent to column of cursor. split-window-horizontally C-x 3 Divide current window horizontally into two. split-window-vertically C-x 2 Divide current window vertically into two. start-kbd-macro C-x ( Start macro definition. stop-shell-subjob C-c C-z Suspend current job. suspend-emacs C-z Suspend Emacs (use fg to restart). switch-to-buffer C-x b Move to the buffer specified. switch-to-buffer-other-frame C-x 5 b Select a buffer in another frame. switch-to-buffer-other- window C-x 4 b Select a buffer in the other window. text-mode (none) Enter text mode. transpose-chars C-t Transpose two letters. transpose-lines C-x C-t Transpose two lines. transpose-paragraphs (none) Transpose two paragraphs. transpose-sentences (none) Transpose two sentences. transpose-words M-t Transpose two words. unexpand-abbrev (none) Undo the last word abbreviation. universal-argument C-u n Repeat the next command n times. upcase-region C-x C-u Uppercase region. upcase-word M-u Uppercase word. view-emacs-news C-h n View news about updates to Emacs. #BREAK# view-lossage C-h l What are the last 100 characters I typed? where-is C-h w What is the key binding for this command? write-abbrev-file (none) Write the word abbreviation file. write-file C-x C-w Write buffer contents to file. yank C-y Restore what you’ve deleted. PreviousHomeNext10.4. Summary of Commands by Key Book Index11. The vi Editor Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 11. The vi Editor Contents: Review of vi Operationsvi Command-Line Optionsex Command-Line OptionsMovement CommandsEdit CommandsSaving and ExitingAccessing Multiple FilesInteracting with the ShellMacrosMiscellaneous CommandsAlphabetical List of Keys in Command ModeSyntax of ex CommandsAlphabetical Summary of ex Commandsvi Configuration vi is the classic screen-editing program for Unix. A number of enhanced versions exist, including nvi, vim, vile, and elvis. On Linux, the vi command is usually a link to one of these programs. vi is based on an older line editor called ex. Powerful editing capabilities can be invoked within vi by pressing the colon (:), entering an ex command, and pressing the Return key. Furthermore, you can place ex commands in a startup file called ~/.exrc, which vi reads at the beginning of your editing session. Because ex commands are still an important part of vi, they also are described in this chapter. On Linux, ex is sometimes called hex. This chapter, which essentially covers standard vi but reflects nvi extensions, presents the following topics: l Review of vi operations #BREAK# l vi command-line options l ex command-line options l Movement commands l Edit commands l Saving and exiting l Accessing multiple files l Interacting with the shell l Macros l Miscellaneous commands l Alphabetical list of keys in command mode l Syntax of ex commands l Alphabetical summary of ex commands l vi configuration (setting options at startup) For more information, see the O’Reilly book Learning the vi Editor by Linda Lamb and Arnold Robbins. 11.1. Review of vi Operations This section provides a review of the following: l Command-line options l vi modes l Syntax of vi commands l Status-line commands 11.1.1. Command Mode #BREAK# Once the file is opened, you are in command mode. From command mode, you can: l Invoke insert mode l Issue editing commands l Move the cursor to a different position in the file l Invoke ex commands l Invoke a Linux shell l Save or exit the current version of the file 11.1.2. Insert Mode In insert mode, you can enter new text in the file. Press the Esc or Ctrl-[ keys to exit insert mode and return to command mode. The following commands invoke insert mode: a Append after cursor A Append at end-of-line c Begin change operation (must be followed by a movement command) C Change to end-of-line i Insert before cursor I Insert at beginning of line #BREAK# o Open a line below current line O Open a line above current line R Begin overwriting text s Substitute a character S Substitute entire line 11.1.3. Syntax of vi Commands In vi, commands have the following general form: [n] operator [m] object The basic editing operators are: c Begin a change d Begin a deletion y Begin a yank (or copy) If the current line is the object of the operation, then the operator is the same as the object: cc, dd, yy. Otherwise, the editing operators act on objects specified by cursor-movement commands or pattern-matching commands. n and m are the number of times the operation is #BREAK# performed or the number of objects the operation is performed on. If both n and m are specified, the effect is n m. An object can represent any of the following text blocks: word Includes characters up to a space or punctuation mark. A capitalized object is a variant form that recognizes only blank spaces. sentence Extends to ., !, ? followed by two spaces. paragraph Extends to next blank line or nroff/troff paragraph macro (defined by para= option). section Extends to next nroff/troff section heading (defined by sect= option). 11.1.3.1. Examples 2cw Change the next two words d} Delete up to next paragraph d^ Delete back to beginning of line 5yy Copy the next five lines into temporary buffer (for future pasting) y]] Copy up to the next section into temporary buffer (for future pasting) #BREAK# 11.1.4. Status-Line Commands Most commands are not echoed on the screen as you input them. However, the status line at the bottom of the screen is used to echo input for the following commands: / Search forward for a pattern ? Search backward for a pattern : Invoke an ex command ! Pipe the text indicated by a subsequent movement command through the following shell command, and replace the text with the output of the shell command Commands that are input on the status line must be entered by pressing the Return key. In addition, error messages and output from the Ctrl-G command are displayed on the status line. PreviousHomeNext10.5. Summary of Commands by Name Book Index11.2. vi Command-Line Options Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.2. vi Command-Line Options The three most common ways of starting a vi session are: vi filevi + n filevi +/ pattern file You can open file for editing, optionally at line n or at the first line matching pattern. If no file is specified, vi opens with an empty buffer. The command-line options that can be used with vi are: +[num] Start editing at line number num, or the last line of the file if num is omitted. +/pattern Start editing at the first line matching pattern. (Fails if nowrapscan is set in your .exrc startup file.) -c command Run the given vi command upon startup. Only one -c option is permitted. ex commands can be invoked by prefixing them with a :. An older form of this option, +command, is still supported. -e Run as ex (line editing rather than full-screen mode). -l Enter LISP mode for running LISP programs (not supported in all versions). #BREAK# -r [file] Recover and resume editing on file after an aborted editor session or system crash. Without file, list files available for recovery. -t tag Edit the file containing tag and position the cursor at its definition (see ctags in Chapter 3, “Linux Commands” for more information). -v Run in full-screen mode (default). -w rows Set the window size so rows lines at a time are displayed; useful when editing over a slow dial-up line. -x Prompt for a key that will be used to try to encrypt or decrypt a file using crypt (not supported in all versions). -C Same as -x, but assume the file is encrypted already (not supported in all versions). -L List files that were saved due to an aborted editor session or system crash (not supported in all versions). -R Edit files read-only. PreviousHomeNext11. The vi Editor Book Index11.3. ex Command-Line Options Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.3. ex Command-Line Options While most people know ex commands only by their use within vi, the editor exists also as a separate program and can be invoked from the shell (for instance, to edit files as part of a script). Within ex, you can enter the vi or visual command to start vi. Similarly, within vi, you can enter Q to quit the vi editor and enter ex. If you invoke ex as a standalone editor, you can include the following options: +[num] Start editing at line number num, or the last line of the file if num is omitted. +/pattern Start editing at the first line matching pattern. (Fails if nowrapscan is set in your .exrc start-up file.) -c command Run the given ex command upon start-up. Only one -c option is permitted. An older form of this option, +command, is still supported. -e Run as a line editor rather than full-screen vi mode (default). -l Enter LISP mode for running LISP programs (not supported in all versions). -r [file] Recover and resume editing on file after an aborted editor session or system crash. #BREAK# Without file, list files available for recovery. -s Silent; do not display prompts. Useful when running a script. This behavior also can be set through the older - option. -t tag Edit the file containing tag and position the cursor at its definition (see ctags in Chapter 3, “Linux Commands” for more information). -v Run in full-screen mode (same as invoking vi). -w rows Set the window size so rows lines at a time are displayed; useful when editing by a slow dial-up line. -x Prompt for a key that will be used to try to encrypt or decrypt a file using crypt (not supported in all versions). -C Same as -x, but assume the file is encrypted already (not supported in all versions). -L List files that were saved due to an editor of system crash (not supported in all versions). -R Edit files read-only; do not allow changes to be saved. You can exit ex in several ways:
Exit (save changes and quit). :q! Quit without saving changes. :vi #BREAK# Enter the vi editor. PreviousHomeNext11.2. vi Command-Line Options Book Index11.4. Movement Commands Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.4. Movement Commands A number preceding a command repeats the movement. Movement commands are also objects for change, delete, and yank operations. 11.4.1. Character Command Action h, j, k, l Left, down, up, right (, , , ) Spacebar Right Backspace Left Ctrl-H Left 11.4.2. Text Command Action w, b Forward, backward by word (treating punctuation marks as words). W, B Forward, backward by word (recognizing only whitespace, not punctuation, as separators). e End of word (treating a punctuation mark as the end of a word). E End of word (recognizing only whitespace as the end of a word). ), ( Beginning of next, current sentence. }, { Beginning of next, current paragraph. ]], [[ Beginning of next, current section. #BREAK# Ctrl-D Move to previous tab setting. Ctrl-T Move to next tab setting. Ctrl-W Move back one word. 11.4.3. Lines Command Action 0, $ First, last position of current line. ^, _ First nonblank character of current line. +, - First character of next, previous line. Return First nonblank character of next line. n| Column n of current line. H Top line of screen. M Middle line of screen. L Last line of screen. nH n lines after top line. nL n lines before last line. Ctrl-J Move down one line. Ctrl-M Move to first nonblank character of next line. 11.4.4. Screens Command Action Ctrl-F, Ctrl-B Scroll forward, backward one screen. Ctrl-D, Ctrl-U Scroll down, up one-half screen. Ctrl-E, Ctrl-Y Show one more line at bottom, top of window. z Return Reposition line with cursor to top of screen. z. Reposition line with cursor to middle of screen. #BREAK# z- Reposition line with cursor to bottom of screen. Ctrl-L, Ctrl-R Redraw screen (without scrolling). 11.4.5. Searches Command Action /pattern Search forward for pattern. / Repeat previous search forward. /pattern/+n Go to line n after pattern. ?pattern Search backward for pattern. ? Repeat previous search backward. ?pattern?-n Go to line n before pattern. n Repeat previous search. N Repeat previous search in opposite direction. % Find match of current parenthesis, brace, or bracket. fx Move forward to x on current line. Fx Move backward to x on current line. tx Move forward to just before x in current line. Tx Move back to just after x in current line. , Reverse search direction of last f, F, t, or T. ; Repeat last character search (f, F, t, or T). 11.4.5.1. Line numbering Command Action Ctrl-G Display current filename and line number. nG Move to line number n. G Move to last line in file. #BREAK# :n Move to line number n. 11.4.5.2. Marking position Command Action mx Mark current position with character x. `x (backquote) Move cursor to mark x. ‘x (apostrophe) Move to start of line containing x. “ (backquotes) Return to previous mark (or location prior to search). ‘’ (apostrophes) Like preceding, but return to start of line. PreviousHomeNext11.3. ex Command-Line Options Book Index11.5. Edit Commands Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.5. Edit Commands Recall that c, d, and y are the basic editing operators. 11.5.1. Inserting New Text Command Action a Append after cursor. A Append to end of line. i Insert before cursor. I Insert at beginning of line. o Open a line below cursor. O Open a line above cursor. Esc Terminate insert mode. Tab Insert a tab. Backspace Delete previous character (in insert mode). Ctrl-I Insert a tab. Ctrl-U Delete current line. Ctrl-V Insert next character verbatim. Ctrl-[ Terminate insert mode. Some of the control characters listed in the previous table are set by stty. Your terminal settings may differ. 11.5.2. Changing and Deleting Text #BREAK# The following table is not exhaustive but illustrates the most common operations. Command Action cw Change through end of current word. cc Change line. c$ Change text from current position to end-of-line. C Same as c$. dd Delete current line. d$ Delete remainder of line. D Same as d$. ndd Delete n lines. dw Delete a word. d} Delete up to next paragraph. d^ Delete back to beginning of line. d/pattern Delete up to first occurrence of pattern. dn Delete up to next occurrence of pattern. dfa Delete up to and including a on current line. dta Delete up to (not including) a on current line. dL Delete up to last line on screen. dG Delete to end-of-file. p Insert last deleted text after cursor. P Insert last deleted text before cursor. rx Replace character with x. Rtext Replace text beginning at cursor. s Substitute character. ns Substitute n characters. S Substitute entire line. #BREAK# u Undo last change. U Restore current line. x Delete current character. X Delete back one character. nX Delete previous n characters. . Repeat last change. ~ Reverse case. & Repeat last substitution. Y Copy (yank) current line to temporary buffer. yy Same as Y. “xyy Copy current line to buffer x. ye Copy text to end of word into temporary buffer. yw Same as ye. y$ Copy rest of line into temporary buffer. “xdd Delete current line into buffer x. “Xdd Delete current line and append to buffer x. “xp Put contents of buffer x. J Join previous line to current line. :j! Same as J. PreviousHomeNext11.4. Movement Commands Book Index11.6. Saving and Exiting Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.6. Saving and Exiting Writing a file means saving the edits and updating the file’s modification time. Command Action ZZ Quit vi, writing the file only if changes were made.
Same as ZZ. :wq Write and quit file. :w Write file. :w file Save copy to file. :n1,n2w file Write lines n1 to n2 to new file. :n1,n2w >> file Append lines n1 to n2 to existing file. :w! Write file (overriding protection). :w! file Overwrite file with current buffer. :w %.new Write current buffer named file as file.new. :q Quit file. :q! Quit file (discarding edits). Q Quit vi and invoke ex. :vi Return to vi after Q command. % Current filename. # Alternate filename. PreviousHomeNext #BREAK# 11.5. Edit Commands Book Index11.7. Accessing Multiple Files Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.7. Accessing Multiple Files Command Action :e file Edit another file; current file becomes alternate. :e! Restore last saved version of current file. :e+ file Begin editing at end of new file. :e+ n file Open new file at line n. :e# Open to previous position in alternate file. :ta tag Edit file containing tag at the location of the tag. :n Edit next file. :n! Force next file into buffer (don’t save changes to current file). :n files Specify new list of files. :args Display multiple files to be edited. :rew Rewind list of multiple files to top. PreviousHomeNext11.6. Saving and Exiting Book Index11.8. Interacting with the Shell Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.8. Interacting with the Shell Command Action :r file Read in contents of file after cursor. :r !command Read in output from command after current line. :nr !command Like preceding, but place after line n (0 for top of file). :!command Run command, then return. !object command Send object, indicated by a movement command, as input to shell command command; replace object with command output. :n1,n2! command Send lines n1 through n2 to command; replace with output. n!!command Send n lines to command; replace with output. !! Repeat last system command. !!command Replace current line with output of command. :sh Create subshell; return to file with EOF. Ctrl-Z Suspend editor, resume with fg. :so file Read and execute ex commands from file. PreviousHomeNext11.7. Accessing Multiple Files Book Index11.9. Macros Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.9. Macros Command Action :ab in out Use in as abbreviation for out. :unab in Remove abbreviation for in. :ab List abbreviations. :map c sequence Map character c as sequence of commands. :unmap c Disable map for character c. :map List characters that are mapped. :map! c sequence Map character c to input mode sequence. :unmap! c Disable input mode map (you may need to quote the character with Ctrl-V). :map! List characters that are mapped to input mode. The following characters are unused in command mode and can be mapped as user-defined commands: Letters: g K q V v Control keys: ^K ^O ^T ^W ^X Symbols: _ * = #BREAK# NOTE The = is used by vi if LISP mode is set. Different versions of vi may use some of these characters, so test them before using them. PreviousHomeNext11.8. Interacting with the Shell Book Index11.10. Miscellaneous Commands Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.10. Miscellaneous Commands Command Action < Shift line left to position indicated by following movement command. > Shift line right to position indicated by following movement command. << Shift line left one shift width (default is 8 spaces). >> Shift line right one shift width (default is 8 spaces). >} Shift right to end of paragraph. <% Shift left until matching parenthesis, brace, bracket, etc. (Cursor must be on the matching symbol.) ^[ Abort command or end input mode. ^] Perform a tag look-up on the text under the cursor. ^ Enter ex line-editing mode. ^^ (Caret key with Ctrl key pressed) Return to previously edited file. PreviousHomeNext11.9. Macros Book Index11.11. Alphabetical List of Keys in Command Mode Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.11. Alphabetical List of Keys in Command Mode For brevity, control characters are marked by ^. Command Action a Append text after cursor. A Append text at end-of-line. ^A Search for next occurrence of word under cursor. b Back up to beginning of word in current line. B Back up one word, treating punctuation marks as words. ^B Scroll backward one window. c Change text up to target of next movement command. C Change to end of current line. ^C End insert mode; interrupts a long operation. d Delete up to target of next movement command. D Delete to end of current line. ^D Scroll down half-window; in insert mode, unindent to shiftwidth if autoindent is set. e Move to end of word. E Move to end of word, treating punctuation as part of word. ^E Show one more line at bottom of window. f Find next character typed forward on current line. #BREAK# F Find next character typed backward on current line. ^F Scroll forward one window. g Unused. G Go to specified line or end-of-file. ^G Print information about file on status line. h Left arrow cursor key. H Move cursor to home position. ^H Left arrow cursor key; Backspace key in insert mode. i Insert text before cursor. I Insert text before first nonblank character on line. ^I Unused in command mode; in insert mode, same as Tab key. j Down arrow cursor key. J Join previous line to current line. ^J Down arrow cursor key; in insert mode, move down a line. k Up arrow cursor key. K Unused. ^K Unused. l Right arrow cursor key. L Move cursor to last position in window. ^L Redraw screen. m Mark the current cursor position in register (a-z). M Move cursor to middle position in window. ^M Move to beginning of next line. n Repeat the last search command. N Repeat the last search command in reverse direction. ^N Down arrow cursor key. o Open line below current line. #BREAK# O Open line above current line. ^O Unused. p Put yanked or deleted text after or below cursor. P Put yanked or deleted text before or above cursor. ^P Up arrow cursor key. q Unused. Q Quit vi and enter ex line-editing mode. ^Q Unused. (On some terminals, resume data flow.) r Replace character at cursor with the next character you type. R Replace characters. ^R Redraw the screen. s Change the character under the cursor to typed characters. S Change entire line. ^S Unused. (On some terminals, stop data flow.) t Find next character typed forward on current line and position cursor before it. T Find next character typed backward on current line and position cursor after it. ^T Unused in command mode; in insert mode, move to next tab setting. u Undo the last change made. U Restore current line, discarding changes. ^U Scroll the screen upward a half-window. v Unused. V Unused. ^V Unused in command mode; in insert mode, insert next character verbatim. w Move to beginning of next word. W Move to beginning of next word, treating punctuation marks as words. #BREAK# ^W Unused in command mode; in insert mode, back up to beginning of word. x Delete character under cursor. X Delete character before cursor. ^X Unused. y Yank or copy text up to target of following movement command into temporary buffer. Y Make copy of current line. ^Y Show one more line at top of window. z Reposition line containing cursor. z must be followed by Return (reposition line to top of screen), . (reposition line to middle of screen), or - (reposition line to bottom of screen). ZZ Exit the editor, saving changes. PreviousHomeNext11.10. Miscellaneous Commands Book Index11.12. Syntax of ex Commands Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.12. Syntax of ex Commands To enter an ex command from vi, type: :[address] command [options] An initial : indicates an ex command. As you type the command, it is echoed on the status line. Enter the command by pressing Return. address is the line number or range of lines that are the object of command. options and addresses are described in the following sections. ex commands are described in the alphabetical summary. 11.12.1. Options ! Indicates a variant command form, overriding the normal behavior. count The number of times the command is to be repeated. Unlike vi commands, ex commands cannot be preceded by count, because a number preceding an ex command is treated as a line address. For example, d3 deletes 3 lines beginning with the current line; 3d deletes line 3. file The name of a file that is affected by the command. %stands for current file; # stands for previous file. 11.12.2. Addresses If no address is given, the current line is the object of the command. If the address specifies a range of lines, the format is: #BREAK# x,y where x and y are the first and last addressed lines (x must precede y in the buffer). x and y may be line numbers or symbols. Using ; instead of , sets the current line to x before interpreting y. The notation 1,$ addresses all lines in the file, as does %. 11.12.3. Address Symbols Symbol Meaning 1,$ All lines in the file % All lines; same as 1,$ x,y Lines x through y x;y Lines x through y, with current line reset to x 0 Top of file . Current line n Absolute line number n $ Last line x-n n lines before x x+n n lines after x -[n] One or n lines previous +[n] One or n lines ahead 'x Line marked with x '' Previous mark /pattern/ Forward to line matching pattern ?pattern? Backward to line matching pattern See Chapter 9, "Pattern Matching", for more information on using patterns. PreviousHomeNext #BREAK# 11.11. Alphabetical List of Keys in Command Mode Book Index11.13. Alphabetical Summary of ex Commands Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.13. Alphabetical Summary of ex Commands ex commands can be entered by specifying any unique abbreviation. In this listing, the full name appears in the margin, and the shortest possible abbreviation is used in the syntax line. Examples are assumed to be typed from vi, so they include the : prompt. abbrev ab [string text] Define string when typed to be translated into text. If string and text are not specified, list all current abbreviations. Examples Note: ^M appears when you type Ctrl-V followed by Return. :ab ora O'Reilly & Associates, Inc. :ab id Name:^MRank:^MPhone: #BREAK# append [address] a[!] text . Append text at specified address, or at present address if none is specified. Add a ! to switch the autoindent setting that will be used during input (e.g., if autoindent was enabled, ! disables it). Terminate input by entering a line consisting of just a period. args ar Print filename arguments (the list of files to edit). The current argument is shown in brackets ([]). cd cd dir chdir dir Change current directory within the editor to dir. change [address] c[!] text . Replace the specified lines with text. Add a ! to switch the autoindent setting during input of text. Terminate input by entering a line consisting of just a period. #BREAK# copy [address] co destination Copy the lines included in address to the specified destination address. The command t is the same as copy. Example :1,10 co 50 Copy first 10 lines to just after line 50 delete [address] d [buffer] Delete the lines included in address. If buffer is specified, save or append the text to the named buffer. Examples :/Part I/,/Part II/-1d Delete to line above "Part II" :/main/+d Delete line below "main" :.,$d Delete from this line to last line edit e[!] [+n] [file] Begin editing file. Add a ! to discard any changes to the current file. If no file is given, edit another copy of the current file. With the +n argument, begin editing on linen. Examples :e file:e# Return to editing the previous file:e! Discard edits since last save #BREAK# exusage exu [command] Print a brief usage message describing command, or a list of available commands if command is omitted. file f [filename] Change the name of the current file to filename, which is considered "not edited." If no filename is specified, print the current status of the file. Example :f %.new global [address] g[!]/pattern/[commands] Execute commands on all lines that contain pattern or, if address is specified, on all lines within that range. If commands are not specified, print all such lines. If ! is used, execute commands on all lines that don't contain pattern. See v. Examples :g/Unix/p Print all lines containing "Unix" :g/Name:/s/tom/Tom/ Change "tom" to "Tom" on all lines containing "Name:" help h Print a brief help message. Information on particular commands can be obtained through the exusage and viusage commands. #BREAK# insert address i[!] text . Insert text at line before the specified address, or at present address if none is specified. Add a ! to switch the autoindent setting during input of text. Terminate input by entering a line consisting of just a period. join [address] j[!] [count] Place the text in the specified address on one line, with whitespace adjusted to provide two blank characters after a period (.), no blank characters after a ), and one blank character otherwise. Add a ! to prevent whitespace adjustment. Example :1,5j! Join first five lines, preserving whitespace k [address] k char Mark the given address with char. Return later to the line with 'char. list [address] l [count] Print the specified lines so that tabs display as ^I, and the ends of lines display as $. l is a temporary version of :set list. #BREAK# map map[!] [char commands] Define a keyboard macro named char as the specified sequence of commands. char is usually a single character, or the sequence #n, representing a function key on the keyboard. Use a ! to create a macro for input mode. With no arguments, list the currently defined macros. Examples :map K dwwP Transpose two words:map q :w^M:n^M Write current file; go to next:map! + ^[bi(^[ea) Enclose previous word in parentheses mark [address] ma char Mark the specified line with char, a single lowercase letter. Return later to the line with 'char. Same as k. mkexrc mk[!] file Create an .exrc file containing a set command for every ex option, set to defaults. move [address] m destination Move the lines specified by address to the destination address. Example :.,/Note/m /END/ Move text block after line containing "END" #BREAK# next n[!] [[+command] filelist] Edit the next file from the command-line argument list. Use args to list these files. If filelist is provided, replace the current argument list with filelist and begin editing on the first file; if command is given (containing no spaces), execute command after editing the first such file. Add a ! to discard any changes to the current file. Example :n chap* Start editing all "chapter" files number [address] nu [count] Print each line specified by address, preceded by its buffer line number. Use # as an alternate abbreviation for number. count specifies the number of lines to show, starting with address. open [address] o [/pattern/] Enter vi's open mode at the lines specified by address or at the lines matching pattern. Enter and exit open mode with Q. Open mode lets you use the regular vi commands, but only one line at a time. May be useful on slow dial-up lines. preserve pre Save the current editor buffer as though the system had crashed. previous prev[!] Edit the previous file from the command-line argument list. #BREAK# print [address] p [count] [address] P [count] Print the lines specified by address. count specifies the number of lines to print, starting with address. Add a ! to discard any changes to the current file. Example :100;+5p Show line 100 and the next 5 lines put [address] pu [char] Restore the lines that were previously deleted or yanked from named buffer char, and put them after the line specified by address. If char is not specified, restore the last deleted or yanked text. quit q[!] Terminate current editing session. Use ! to discard changes made since the last save. If the editing session includes additional files in the argument list that were never accessed, quit by typing q! or by typing q twice. #BREAK# read [address] r file Copy in the text from file on the line below the specified address. If file is not specified, the current filename is used. Example :0r $HOME/data Read file in at top of current file read [address] r !command Read the output of Linux command into the text after the line specified by address. Example :$r !cal Place a calendar at end-of-file recover rec [file] Recover file from system save area. rewind rew[!] Rewind argument list and begin editing the first file in the list. The ! flag rewinds, discarding any changes to the current file that haven't been saved. #BREAK# script sc[!] [file] Create a new shell in a buffer that can be saved, optionally specifying file where the buffer can be saved. Can be used only in vi. set se parameter1 parameter2 ... Set a value to an option with each parameter, or if no parameter is supplied, print all options that have been changed from their defaults. For Boolean-valued options, each parameter can be phrased as option or nooption; other options can be assigned with the syntax option=value. Specify all to list current settings. Examples :set nows wm=10:set all shell sh Create a new shell. Resume editing when the shell is terminated. source so file Read and execute ex commands from file. Example :so $HOME/.exrc #BREAK# stop st Suspend the editing session. Same as Ctrl-Z. Use fg to resume session. substitute [address] s [/pattern/replacement/] [options] [count] Replace each instance of pattern on the specified lines with replacement. If pattern and replacement are omitted, repeat last substitution. count specifies the number of lines on which to substitute, starting with address. When preceded by the global (g) or v command, this command can be specified with a blank pattern, in which case the pattern from the g or v command is then used. For more examples, see Section 9.4.1, "Examples of Searching and Replacing" in Chapter 9, "Pattern Matching". Options c Prompt for confirmation before each change. g Substitute all instances of pattern on each line. p Print the last line on which a substitution was made. Examples :1,10s/yes/no/g Substitute on first 10 lines:%s/[Hh]ello/Hi/gc Confirm global substitutions:s/Fortran/U&/ 3 Uppercase first instance of "Fortran" on next three lines:g/^[0-9][0-9]*/s//Line &:/ For every line beginning with one or more digits, add the "Line" and a colon #BREAK# suspend su Suspend the editing session. Same as Ctrl-Z. Use fg to resume session. t [address] t destination Copy the lines included in address to the specified destination address. t is an alias for copy. Example :%t$ Copy the file and add it to the end tag [address] ta[!] tag Switch the editing session to the file containing tag. Example Run ctags, then switch to the file containing myfunction: :!ctags *.c:tag myfunction tagnext tagn[!] Find the next occurrence of the current tag. #BREAK# tagpop tagp[!] Forget the current tag and return to the last position of the previous tag found. tagprev tagpr[!] Return to the previous occurrence of the current tag. tagtop tagt[!] Return to the first tag searched for and forget about all tags. unabbreviate una word Remove word from the list of abbreviations. undo u Reverse the changes made by the last editing command. unmap unm[!] char Remove char from the list of keyboard macros. Use ! to remove a macro for input mode. #BREAK# v [address] v/pattern/[commands] Execute commands on all lines not containing pattern. If commands are not specified, print all such lines. v is equivalent to g!. See global. Example :v/#include/d Delete all lines except "#include" lines version ve Print the editor's current version number. vi vi [+n] file Begin editing file, optionally at line n. Can be used only in vi. visual [address] vi [type] [count] Enter visual mode (vi) at the line specified by address. Exit with Q. type can be one of -, ^, or . (See the z command.) count specifies an initial window size. viusage viu [key] Print a brief usage message describing the operation of key, or a list of defined keys if key is omitted. #BREAK# wq wq[!] Write and quit the file in one command. The ! flag forces the editor to write over any current contents of file. write [address] w[!] [[>>] file Write lines specified by address to file, or write full contents of buffer if address is not specified. If file also is omitted, save the contents of the buffer to the current filename. If >>file is used, write contents to the end of an existing file. The ! flag forces the editor to write over any current contents of file. write [address] w !command Write lines specified by address to command. Examples :1,10w name_list Copy first 10 lines to name_list:50w >> name_list Now append line 50 xit x Write the file if it was changed since the last write, then quit. #BREAK# yank [address] ya [char] [count] Place lines specified by address in named buffer char. If no char is given, place lines in general buffer. count specifies the number of lines to yank, starting with address. Example :101,200 ya a z [address] z [type] [count] Print a window of text, with the line specified by address at the top. count specifies the number of lines to be displayed. Type + Place specified line at top of window (the default). - Place specified line at bottom of window. . Place specified line in center of window. ^ Move up one window. = #BREAK# Place specified line in center of window, and leave this line as the current line. ! [address] !command Execute Linux command in a shell. If address is specified, apply the lines contained in address as standard input to command, and replace the lines with the output. Examples :!ls List files in the current directory:11,20!sort -f Sort lines 11-20 of current file = [address] = Print the line number of the next line matching address. If no address is given, print the number of the last line. < > [address]<[count] [address]>[count] Shift lines specified by address either left (<) or right (>). Only blanks and tabs are removed in a left shift. count specifies the number of lines to shift, starting with address. address address Print the line specified in address. #BREAK# Return Return Print the next line in the file. & & [options] [count] Repeat the previous substitution (s) command. count specifies the number of lines on which to substitute, starting with address. Examples :s/Overdue/Paid/ Substitute once on current line:g/Status/& Redo substitution on all “Status” lines ~ [address] ~ [count] Replace the previous regular expression with the previous replacement pattern from a substitute (s) command. ^D ^D Scroll through the file. ^Z ^Z Suspend the editing session. Use fg to resume session. PreviousHomeNext #BREAK# 11.12. Syntax of ex Commands Book Index11.14. vi Configuration Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 11.14. vi Configuration This section describes the following: l The :set command l Options available with :set l Sample ~/.exrc file 11.14.1. The :set Command The :set command lets you specify options that change characteristics of your editing environment. Options may be put in the ~/.exrc file or set during a vi session. The colon should not be typed if the command is put in ~/.exrc. Command Action :set x Enable option x. :set nox Disable option x. :set x=val Give value to option x. :set Show changed options. :set all Show all options. :set x? Show value of option x. 11.14.2. Options Used by :set The following table describes the options to :set. The first column includes the optional abbreviation, if there is one, and uses an equals sign to show that the option takes a value. The #BREAK# second column gives the default, and the third column describes the behavior of the enabled option. Option Default Description autoindent (ai) noai In insert mode, indent each line to the same level as the line above or below. Use with shiftwidth option. autoprint (ap) ap Display changes after each editor command. (For global replacement, display last replacement.) autowrite (aw) noaw Automatically write (save) file if changed, before opening another file with :n or before giving Linux command with :!. beautify (bf) nobf Ignore all control characters during input (except tab, newline, or formfeed). directory= (dir) /tmp Name the directory in which ex stores buffer files. (Directory must be writable.) edcompatible noed-compatible Use ed-like features on substitute commands. errorbells (eb) errorbells Sound bell when an error occurs. exrc (ex) noexrc Allow the execution of ~/.exrc files that reside outside the user’s home directory. hardtabs= (ht) 8 Define boundaries for terminal hardware tabs. ignorecase (ic) noic Disregard case during a search. lisp nolisp Insert indents in appropriate LISP format. (), { }, [[, and ]] are modified to have meaning for LISP. #BREAK# list nolist Print tabs as ^I; mark ends of lines with $. (Use list to tell if end character is a tab or a space.) magic magic Wildcard characters . (dot), * (asterisk), and [ ] (brackets) have special meaning in patterns. mesg mesg Permit system messages to display on terminal while editing in vi. number (nu) nonu Display line numbers on left of screen during editing session. redraw (re) noredraw Terminal redraws screen whenever edits are made (in other words, insert mode pushes over existing characters, and deleted lines immediately close up). Default depends on line speed and terminal type. noredraw is useful at slow speeds on a dumb terminal: deleted lines show up as @, and inserted text appears to overwrite existing text until you press Esc. remap remap Allow nested map sequences. report= 5 Display a message on the prompt line whenever you make an edit that affects at least a certain number of lines. For example, 6dd reports the message “6 lines deleted.” scroll= <1/2 window> Amount of screen to scroll. sections= (sect) SHNHH HU Define section delimiters for [[ ]] movement. The pairs of characters in the value are the names of nroff/troff macros that begin sections. shell= (sh) /bin/sh Pathname of shell used for shell escape (:!) and shell command (:sh). Default value is derived from SHELL variable. shiftwidth= (sw) 8 Define number of spaces used by the indent commands (^T, ^D, >>, and <<). showmatch (sm) nosm In vi, when ) or } is entered, cursor moves briefly to matching ( or {. (If the match is not on the screen, rings the error message bell.) Very useful for programming. #BREAK# showmode noshowmode In insert mode, displays a message on the prompt line indicating the type of insert you are making, such as "Open Mode" or "Append Mode." slowopen (slow) Hold off display during insert. Default depends on line speed and terminal type. tabstop= (ts) 8 Define number of spaces that a tab indents during editing session. (Printer still uses system tab of 8.) taglength= (tl) 0 Define number of characters that are significant for tags. Default (0) means that all characters are significant. tags= tags /usr/lib/tags Define pathname of files containing tags (see the ctags command in Chapter 3, "Linux Commands"). By default, the system looks for files tags (in the current directory) and /usr/lib/tags. term= Set terminal type. terse noterse Display shorter error messages. timeout (to) timeout Keyboard maps timeout after 1second. ttytype= Set terminal type. Default is inherited from TERM environment variable. warn warn Display the message, "No write since last change." window= (w) Show a certain number of lines of the file on the screen. Default depends on line speed and terminal type. wrapmargin= (wm) 0 Define right margin. If greater than 0, automatically insert carriage returns to break lines. wrapscan (ws) ws Searches wrap around either end of file. #BREAK# writeany (wa) nowa Allow saving to any file. 11.14.3. Sample ~/.exrc File The following lines of code are an example of a customized .exrc file: set nowrapscan wrapmargin=7set sections=SeAhBhChDh nomesgmap q :w^M:n^Mmap v dwElpab ORA O'Reilly & Associates, Inc. PreviousHomeNext11.13. Alphabetical Summary of ex Commands Book Index12. The sed Editor Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 12. The sed Editor Contents: Conceptual OverviewCommand-Line SyntaxSyntax of sed CommandsGroup Summary of sed CommandsAlphabetical Summary of sed Commands This chapter presents the following topics: l Conceptual overview of sed l Command-line syntax l Syntax of sed commands l Group summary of sed commands l Alphabetical summary of sed commands For more information, see the O'Reilly book sed & awk, 2d ed., by Dale Dougherty and Arnold Robbins. 12.1. Conceptual Overview sed is a noninteractive, or stream-oriented, editor. It interprets a script and performs the actions in the script. sed is stream-oriented, because, as with many Unix programs, input flows through the program and is directed to standard output. For example, sort is stream- oriented; vi is not. sed's input typically comes from a file but can be directed from the keyboard. Output goes to the screen by default but can be captured in a file instead. #BREAK# Typical uses of sed include: l Editing one or more files automatically l Simplifying repetitive edits to multiple files l Writing conversion programs sed operates as follows: l Each line of input is copied into a pattern space. l All editing commands in a sed script are applied in order to each line of input. l Editing commands are applied to all lines (globally) unless line addressing restricts the lines affected. l If a command changes the input, subsequent commands are applied to the changed line, not to the original input line. l The original input file is unchanged, because the editing commands modify a copy of the original input line. The copy is sent to standard output (but can be redirected to a file). PreviousHomeNext11.14. vi Configuration Book Index12.2. Command-Line Syntax Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 12.2. Command-Line Syntax The syntax for invoking sed has two forms: sed [options] 'command' file(s) sed [options] -f scriptfile file(s) The first form allows you to specify an editing command on the command line, surrounded by single quotes. The second form allows you to specify a scriptfile, a file containing sed commands. If no files are specified, sed reads from standard input. The following options are recognized: -e cmd Next argument is an editing command; not needed unless specifying two or more editing commands. -f scriptfile Next argument is a file containing editing commands. -n Suppress the default output; sed displays only those lines specified with the p command or with the p flag of the s command. -V Display version number. --quiet #BREAK# Same as -n. --expression=cmd Same as -e. --file=file Same as -f. --help Display brief help message with command-line options. --silent Same as -n. --version Same as -V. PreviousHomeNext12. The sed Editor Book Index12.3. Syntax of sed Commands Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 12.3. Syntax of sed Commands sed commands have the general form: [address[,address]][!]command [arguments] sed commands consist of addresses and editing commands. commands consist of a single letter or symbol; they are described later, alphabetically and by group. arguments include the label supplied to b or t, the filename supplied to r or w, and the substitution flags for s. addresses are described in the next section. 12.3.1. Pattern Addressing A sed command can specify zero, one, or two addresses. An address can be a line number, the symbol $ (for last line), or a regular expression enclosed in slashes (/pattern/). Regular expressions are described in Chapter 9, "Pattern Matching". Additionally, n can be used to match any newline in the pattern space (resulting from the N command) but not the newline at the end of the pattern space. If the Command Specifies Then the Command Is Applied To No address Each input line. One address Any line matching the address. Some commands (a, i, r, q, and =) accept only one address. Two comma-separated addresses First matching line and all succeeding lines up to and including a line matching the second address. An address followed by ! All lines that do not match the address. 12.3.1.1. Examples s/xx/yy/g Substitute on all lines (all occurrences) /BSD/d Delete lines containing BSD/^BEGIN/,/^END/p Print between BEGIN and END, inclusive/SAVE/!d Delete any line that doesn't contain SAVE/BEGIN/,/END/!s/xx/yy/g Substitute on all lines, except between BEGIN and END Braces ({}) are used in sed to nest one address inside another or to apply multiple commands at the same address: [/address/[,/address/]]{ command1command2} The opening curly brace must end a line, and the closing curly brace must be on a line by itself. Be sure there are no blank spaces after the braces. #BREAK# PreviousHomeNext12.2. Command-Line Syntax Book Index12.4. Group Summary of sed Commands Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 12.4. Group Summary of sed Commands In the following tables, the sed commands are grouped by function and are described tersely. Full descriptions, including syntax and examples, can be found afterward in the alphabetical summary. 12.4.1. Basic Editing Command Action a Append text after a line. c Replace text (usually a text block). i Insert text before a line. d Delete lines. s Make substitutions. y Translate characters (like tr in Chapter 3, "Linux Commands"). 12.4.2. Line Information Command Action = Display line number of a line. l Display control characters in ASCII. p Display the line. 12.4.3. Input/Output Processing #BREAK# Command Action n Skip current line and go to line below. r Read another file's contents into the input. w Write input lines to another file. q Quit the sed script (no further output). 12.4.4. Yanking and Putting Command Action h Copy pattern space into hold space; wipe out what's there. H Copy pattern space into hold space; append to what's there. g Get the hold space back; wipe out the pattern space. G Get the hold space back; append to pattern space. x Exchange contents of hold space and pattern space. 12.4.5. Branching Commands Command Action b Branch to label or to end of script. t Same as b, but branch only after substitution. :label Label branched to by t or b. 12.4.6. Multiline Input Processing Command Action N Read another line of input (creates embedded newline). D Delete up to the embedded newline. P Print up to the embedded newline. #BREAK# PreviousHomeNext12.3. Syntax of sed Commands Book Index12.5. Alphabetical Summary of sed Commands Copyright 2001 O'Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 12.5. Alphabetical Summary of sed Commands # # Begin a comment in a sed script. Valid only as the first character of the first line. (Some versions of sed, including the GNU version on Linux, allow comments anywhere, but it is better not to rely on this.) If the first line of the script is #n, sed behaves as if -n had been specified. : :label Label a line in the script for the transfer of control by b or t. label may contain up to seven characters. = [/pattern/]= Write to standard output the line number of each line containing pattern. a [address]a text Append text following each line matched by address. If text goes over more than one line, newlines must be "hidden" by preceding them with a backslash. The text will be terminated by the first newline that is not hidden in this way. The text is not available in the pattern space, and subsequent commands cannot be applied to it. The results of this command are sent to standard output when the list of editing commands is finished, regardless of what happens to the current line in the pattern space. Example $a This goes after the last line in the file (marked by $). This text is escaped at the end of each line, except for the last one. #BREAK# b [address1[,address2]]b[label] Transfer control unconditionally to :label elsewhere in script. That is, the command following the label is the next command applied to the current line. If no label is specified, control falls through to the end of the script, so no more commands are applied to the current line. Example Ignore lines between .TS and .TE; resume script after .TE: /^.TS/,/^.TE/b c [address1[,address2]]c text Replace the lines selected by the address with text. When a range of lines is specified, all lines as a group are replaced by a single copy of text. The newline following each line of text must be escaped by a backslash, except the last line. The contents of the pattern space are, in effect, deleted, and no subsequent editing commands can be applied. Example Replace first 100 lines in a file: 1,100c d [address1[,address2]]d Delete the addressed line (or lines) from the pattern space. Thus, the line is not passed to standard output. A new line of input is read, and editing re sumes with the first command in the script. Example Delete all blank lines: /^$/d #BREAK# D [address1[,address2]]D Delete first part (up to embedded newline) of multiline pattern space created by N command, and resume editing with first command in script. If this command empties the pattern space, then a new line of input is read, as if the d had been executed. Example Strip multiple blank lines, leaving only one: /^$/{ N/^n$/D} g [address1[,address2]]g Paste the contents of the hold space (see h or H command) back into the pattern space, wiping out the previous contents of the pattern space. The example shows a simple way to copy lines. Example This script collects all lines containing the word Item: and copies them to a place marker later in the file. The place marker is overwritten. /Item:/H//g G [address1[,address2]]G Same as g, except that the hold space is pasted below the address instead of overwriting it. The example shows a simple way to cut and paste lines. Example This script collects all lines containing the word Item: and moves them after a place marker later in the file. The original Item: lines are deleted. /Item:/{ Hd} /Summary of items:/G #BREAK# h [address1[,address2]]h Copy the pattern space into the hold space, a special temporary buffer. The previous contents of the hold space are obliterated. You can use h to save a line before editing it. Example # Edit a line; print the change; replay the original/Linux/{ hs/.* Linux (.*) .*/1:/ px} Sample input: This describes the Linux ls command. This describes the Linux cp command. Sample output: ls: This describes the Linux ls command. cp: This describes the Linux cp command. H [address1[,address2]]H Append the contents of the pattern space (preceded by a newline) to the contents of the hold space. Even if the hold space is empty, H still appends a newline. H is like an incremental copy. See examples under g and G. i [address1]i text Insert text before each line matched by address. (See a for details on text.) Example /Item 1/i The five items are listed below: #BREAK# l [address1[,address2]]l List the contents of the pattern space, showing nonprinting characters as ASCII codes. Long lines are wrapped. n [address1[,address2]]n Read next line of input into pattern space. The current line is sent to standard output, and the next line becomes the current line. Control passes to the command following n instead of resuming at the top of the script. Example In the ms macros, a section header occurs on the line below an .NH macro. To print all lines of header text, invoke this script with sed -n: /^.NH/{ np} N [address1[,address2]]N Append next input line to contents of pattern space; the two lines are separated by an embedded newline. (This command is designed to allow pattern matches across two lines.) Using n to match the embedded newline, you can match patterns across multiple lines. See example at D. Examples Like previous example, but print .NH line as well as header title: /^.NH/{ Np} Join two lines (replace newline with space): /^.NH/{ Ns/n/ / p} #BREAK# p [address1[,address2]]p Print the addressed lines. Unless the -n command-line option is used, this command causes duplicate lines to be output. Also, it typically is used before commands that change flow control (d, N, b) and that might prevent the current line from being output. See examples at h, n, and N. P [address1[,address2]]P Print first part (up to embedded newline) of multiline pattern created by N command. Same as p if N has not been applied to a line. q [address]q Quit when address is encountered. The addressed line first is written to output (if default output is not suppressed), along with any text appended to it by previous a or r commands. Examples Delete everything after the addressed line: /Garbled text follows:/q Print only the first 50 lines of a file: 50q r [address]r file Read contents of file and append after the contents of the pattern space. Exactly one space must be put between the r and the filename. Example /The list of items follows:/r item_file s [address1[,address2]]s/pattern/replacement/[flags] Substitute replacement for pattern on each addressed line. If pattern addresses are used, the pattern // represents the last pattern address specified. The following flags can be specified: n Replace nth instance of /pattern/ on each addressed line. n is any number in the range 1 to 512; the default is 1. g #BREAK# Replace all instances of /pattern/ on each addressed line, not just the first instance. p Print the line if a successful substitution is done. If several successful substitutions are done, multiple copies of the line will be printed. w file Write the line to a file if a replacement was done. Examples Here are some short, commented scripts: # Change third and fourth quote to ( and ): /function/{ s/”/(/3s/”/)/4} # Remove all quotes on a given line: /Title/s/”//g# Remove first colon or all quotes; print resulting lines: s/://ps/”//gp# Change first “if” but leave “ifdef” alone: /ifdef/!s/if/ if/ t [address1[,address2]]t [label] Test if any substitutions have been made on addressed lines, and if so, branch to line marked by :label. (See b and :.) If label is not specified, control falls through to bottom of script. The t command is like a case statement in the C programming language or the shell programming languages. You test each case; when it’s true, you exit the construct. Example Suppose you want to fill empty fields of a database. You have this: ID: 1 Name: greg Rate: 45ID: 2 Name: daleID: 3 You want this: ID: 1 Name: greg Rate: 45 Phone: ?? ID: 2 Name: dale Rate: ?? Phone: ?? #BREAK# ID: 3 Name: ???? Rate: ?? Phone: ?? You need to test the number of fields already there. Here’s the script (fields are tab-separated): /ID/{ s/ID: .* Name: .* Rate: .*/& Phone: ??/pts/ID: .* Name: .*/& Rate: ?? Phone: ??/pts/ID: .*/& Name: ?? Rate: ?? Phone: ??/p} w [address1[,address2]]w file Append contents of pattern space to file. This action occurs when the command is encountered, rather than when the pattern space is output. Exactly one space must separate the w and the filename. This command will create the file if it does not exist; if the file exists, its contents will be overwritten each time the script is executed. Multiple write commands that direct output to the same file append to the end of the file. Example # Store tbl and eqn blocks in a file: /^.TS/,/^.TE/w troff_stuff/^.EQ/,/^.EN/w troff_stuff x [address1[,address2]]x Exchange contents of the pattern space with the contents of the hold space. See h for an example. y [address1[,address2]]y/abc/xyz/ Translate characters. Change every instance of a to x, b to y, c to z, etc. Example # Change item 1, 2, 3 to Item A, B, C … /^item [1-9]/y/123456789/ABCDEFGHI/ PreviousHomeNext12.4. Group Summary of sed Commands Book Index13. The gawk Scripting Language Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 13. The gawk Scripting Language Contents: Conceptual OverviewCommand-Line SyntaxPatterns and Proceduresgawk System VariablesOperatorsVariable and Array AssignmentsGroup Listing of gawk CommandsAlphabetical Summary of Commands gawk is the GNU version of awk, a powerful pattern-matching program for processing text files that may be composed of fixed or variable length records separated by some delineator (by default, a newline character). gawk may be used from the command line or in gawk scripts. Normally you should be able to invoke this utility using either awk or gawk on the shell command line. This chapter presents the following topics: l Conceptual overview l Command-line syntax l Patterns and procedures l System variables l Operators #BREAK# l Variable and array assignment l Group listing of commands l Alphabetical summary of commands For more information, see the O’Reilly book sed & awk, 2d ed., by Dale Dougherty and Arnold Robbins. 13.1. Conceptual Overview With gawk, you can: l Conveniently process a text file as though it were made up of records and fields in a textual database. l Use variables to change the database. l Execute shell commands from a script. l Perform arithmetic and string operations. l Use programming constructs such as loops and conditionals. l Define your own functions. l Process the result of shell commands. l Process command-line arguments more gracefully. l Produce formatted reports. PreviousHomeNext12.5. Alphabetical Summary of sed Commands Book Index13.2. Command-Line Syntax Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 13.2. Command-Line Syntax gawk’s syntax has two forms: gawk [options] ’script’ var=value file(s) gawk [options] -f scriptfile var=value file(s) You can specify a script directly on the command line, or you can store a script in a scriptfile and specify it with -f. Multiple -f options are allowed; awk concatenates the files. This feature is useful for including libraries. gawk operates on one or more input files. If none are specified (or if - is specified), gawk reads from the standard input. Variables can be assigned a value on the command line. The value assigned to a variable can be a literal, a shell variable ($name), or a command substitution (`cmd`), but the value is available only after a line of input is read (i.e., after the BEGIN statement). For example, to print the first three (colon-separated) fields of the password file, use -F to set the field separator to a colon: gawk -F: ‘{print $1; print $2; print $3}’ /etc/passwd Numerous examples are shown later in Section 13.3, “Patterns and Procedures”. 13.2.1. Options All options exist in both traditional POSIX (one-letter) format and GNU-style (long) format. Some recognized options are: — Treat all subsequent text as commands or filenames, not options. #BREAK# -f scriptfile, –file=scriptfile Read gawk commands from scriptfile instead of command line. -v var=value, –assign=var=value Assign a value to variable var. This allows assignment before the script begins execution. -Fc, –field-separator=c Set the field separator to character c. This is the same as setting the variable FS. c may be a regular expression. Each input line, or record, is divided into fields by whitespace (blanks or tabs) or by some other user-definable record separator. Fields are referred to by the variables $1, $2,…, $n. $0 refers to the entire record. -W option All -W options are specific to gawk, as opposed to awk. An alternate syntax is — option (i.e., –compat). option may be one of: compat Same as traditional. copyleft Print copyleft notice and exit. copyright Same as copyleft. help Print syntax and list of options, then exit. lint Warn about commands that might not port to other versions of awk or that gawk considers problematic. lint-old #BREAK# Like lint but compares to an older version of awk. posix Expect exact compatibility with POSIX; additionally, ignore x escape sequences, **, and **=. re-interval Allow use of {n,m} intervals in regular expressions. source=script Treat script as gawk commands. Like the ’script’ argument but lets you mix commands from files (using -f options) with commands on the gawk command line. traditional Behave exactly like traditional (non-GNU) awk. usage Same as help. version Print version information and exit. PreviousHomeNext13. The gawk Scripting Language Book Index13.3. Patterns and Procedures Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 13.3. Patterns and Procedures gawk scripts consist of patterns and procedures: pattern {procedure} Both are optional. If pattern is missing, {procedure} is applied to all records. If {procedure} is missing, the matched record is printed. By default, each line of input is a record, but you can specify a different record separator through the RS variable. 13.3.1. Patterns A pattern can be any of the following: /regular expression/ relational expressionpattern-matching expressionpattern,patternBEGINEND Some rules regarding patterns include: l Expressions can be composed of quoted strings, numbers, operators, functions, defined variables, or any of the predefined variables described later under “gawk System Variables.” l Regular expressions use the extended set of metacharacters and are described in Chapter 9, “Pattern Matching”. l In addition, ^ and $ can be used to refer to the beginning and end of a field, respectively, rather than the beginning and end of a record. l Relational expressions use the relational operators listed under “Operators” later in this chapter. Comparisons can be either string or numeric. For example, $2 > $1 selects lines for which the second field is greater than the first. l Pattern-matching expressions use the operators ~ (match) and !~ (don’t match). See #BREAK# “Operators” later in this chapter. l The BEGIN pattern lets you specify procedures that take place before the first input record is processed. (Generally, you set global variables here.) l The END pattern lets you specify procedures that take place after the last input record is read. l If there are multiple BEGIN or END patterns, their associated actions are taken in the order in which they appear in the script. l pattern,pattern specifies a range of lines. This syntax cannot include BEGIN or END as a pattern. Except for BEGIN and END, patterns can be combined with the Boolean operators || (OR), && (AND), and ! (NOT). In addition to other regular-expression operators, GNU awk supports POSIX character lists, which are useful for matching non-ASCII characters in languages other than English. These lists are recognized only within [ ] ranges. A typical use would be [[:lower:]], which in English is the same as [a-z]. See Chapter 9, “Pattern Matching” for a complete list of POSIX character lists. 13.3.2. Procedures Procedures consist of one or more commands, functions, or variable assignments, separated by newlines or semicolons and contained within curly braces. Commands fall into four groups: l Variable or array assignments l Printing commands l Built-in functions l Control-flow commands 13.3.3. Simple Pattern-Procedure Examples 1. Print first field of each line (no pattern specified): { print $1 } 2. Print all lines that contain “Linux”: /Linux/ 3. Print first field of lines that contain “Linux”: #BREAK# /Linux/{ print $1 } 4. Print records containing more than two fields: NF > 2 5. Interpret each group of lines up to a blank line as a single input record: BEGIN { FS = “n”; RS = “” } 6. Print fields 2 and 3 in switched order but only on lines whose first field matches the string “URGENT”: $1 ~ /URGENT/ { print $3, $2 } 7. Count and print the number of instances of “ERR” found: /ERR/ { ++x }; END { print x } 8. Add numbers in second column and print total: {total += $2 }; END { print “column total is”, total} 9. Print lines that contain fewer than 20 characters: length() < 20 10. Print each line that begins with "Name:" and that contains exactly seven fields: NF == 7 && /^Name:/ 11. Reverse the order of fields: { for (i = NF; i >= 1; i–) print $i } PreviousHomeNext13.2. Command-Line Syntax Book Index13.4. gawk System Variables Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 13.4. gawk System Variables Variable Description $n nth field in current record; fields are separated by FS $0 Entire input record ARGC Number of arguments on command line ARGIND Current file’s place in command line (starting with 0) ARGV An array containing the command-line arguments CONVFMT Conversion format for numbers (default is %.6g) ENVIRON An associative array of environment variables ERRNO Description of last system error FIELDWIDTHS List of field widths (whitespace-separated) FILENAME Current filename FNR Like NR, but relative to the current file FS Field separator (default is any whitespace; null string separates into individual characters) IGNORECASE If true, make case-insensitive matches NF Number of fields in current record NR Number of the current record OFMT Output format for numbers (default is %.6g) OFS Output field separator (default is a blank) ORS Output record separator (default is a newline) #BREAK# RLENGTH Length of the string matched by match function RS Record separator (default is a newline) RSTART First position in the string matched by match function SUBSEP Separator character for array subscripts (default is 34) PreviousHomeNext13.3. Patterns and Procedures Book Index13.5. Operators Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 13.5. Operators The following table lists the operators, in order of increasing precedence, that are available in gawk. Symbol Meaning = += -= *= /= %= ^= **= Assignment ?: C conditional expression || Logical OR && Logical AND ~ !~ Match regular expression and negation < <= > >= != == Relational operators (blank) Concatenation + - Addition, subtraction * / % Multiplication, division, and modulus + - ! Unary plus and minus and logical negation ^ ** Exponentiation ++ — Increment and decrement, either prefix or postfix $ Field reference in Array membership (see for command) PreviousHomeNext13.4. gawk System Variables Book Index13.6. Variable and Array Assignments #BREAK# Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 13.6. Variable and Array Assignments Variables can be assigned a value with an equals sign. For example: FS = “,” Expressions using the operators +, -, /, and % (modulo) can be assigned to variables. Arrays can be created with the split function (see the listing in Section 13.8, “Alphabetical Summary of Commands”), or they can simply be named in an assignment statement. Array elements can be subscripted with numbers (array[1]) or with names. For example, to count the number of occurrences of a pattern, you could use the following script: /pattern/ { array[”/pattern/”]++ } END { print array[”/pattern/”] } In gawk, variables need not be declared previous to their use, nor do arrays need to be dimensioned; they are activated upon first reference. All variables are stored as strings but may be used either as strings or numbers. gawk will use the program script context to determine whether to treat a variable as a string or a number, but the distinction also can be forced by the user. To force a variable to be treated as a string, catenate a null to the variable: var “” To force a variable to be treated as a number, add 0 to it: var + 0 PreviousHomeNext13.5. Operators Book Index13.7. Group Listing of gawk Commands #BREAK# Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 13.7. Group Listing of gawk Commands gawk commands may be classified as follows: Arithmetic Functions String Functions Control Flow Statements Input/Output Processing Time Functions Misc. atan2 gensub break close strftime delete cos gsub continue fflush systime function exp index do/while getline system int length exit next log match for nextfile rand split if print sin sub return printf sqrt substr sprintf srand tolower while toupper PreviousHomeNext13.6. Variable and Array Assignments Book Index13.8. Alphabetical Summary of Commands Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 13.8. Alphabetical Summary of Commands The following alphabetical list of statements and functions includes all that are available in gawk in Linux. atan2 atan2(y,x) Return the arctangent of y/x in radians. break break Exit from a while or for loop. close close(filename-expr) close(command-expr) Close a file read by a getline command or a pipe; takes as an argument the same expression that opened the pipe or file. continue continue Begin next iteration of while or for loop without reaching the bottom. cos cos(x) Return the cosine of x, an angle in radians. delete delete array[element] delete array Delete element of array. If no element is specified, all elements are deleted. #BREAK# do do body while(expr) Looping statement. Execute statements in body, then evaluate expr. If expr is true, execute body again. exit exit Do not execute remaining instruction, and read no new input. END procedures will be executed. exp exp(arg) Return the natural exponent of arg (the inverse of log). fflush fflush(filename) Flushes output to filename; default is the standard output. for for(i=lower ; i<=upper ; i++) command While the value of variable i is in the range between lower and upper, do command. A series of commands must be put within braces. <= or any relational operator can be used; ++ or -- can be used to increment or decrement the variable. for for(item in array) command For each item in an associative array, do command. Multiple commands must be put inside braces. Refer to each element of the array as array[item]. Elements of gawk arrays are stored in an order that enables access of any element in essentially equivalent time. This order may appear to be indiscriminate; if the output is desired in sorted order, you must pipe it through the sort command. function function name(parameter-list) { statements } Create name as a user-defined function consisting of gawk statements that apply to the specified list of parameters. #BREAK# gensub gensub(r,s,n,t) Substitute s for the nth match of regular expression r in the string t. Leave t unchanged, but return new string as the result. If n is "g" or "G" change all matches. If t is not supplied, it defaults to $0. getline getline [varhairsp;] [=, or >, as well as the pattern-matching operator ~. A series of commands must be put within braces. Example The following lines determine whether the first word in each line starts with A, uppercase or lowercase: if ($1 ~ /[Aa]*/) …Begins with A or a index index(substr,str) Return the position of a substring in a string. Returns 0 if substr is not contained in str. int int(arg) Return the integer part of arg. #BREAK# length length(arg) Return the length of arg. If arg is not supplied, $0 is assumed. log log(arg) Return the natural logarithm of arg (the inverse of exp). match match(s,r) Return position in s where regular expression r first matches or 0 if no occurrences are found. Sets the value of RSTART and RLENGTH. next next Read next input line and start new cycle through pattern/procedures statements. nextfile nextfile Skip to the next file on the gawk command line and start new cycle through pattern/procedures statements. print print [args] [destination] Print args on output. Literal strings must be quoted. Fields are printed in the order they are listed. If separated by commas in the argument list, they are separated in the output by the character specified by OFS. If separated by spaces, they are concatenated in the output. destination is a shell redirection or pipe expression (e.g., > file) that redirects the default output. printf printf [format [, expressions]] Formatted print statement. Expressions or variables can be formatted according to instructions in the format argument. The number of expressions must correspond to the number specified in the format sections. format follows the conventions of the C-language printf statement. Here are a few of the most common formats: %s A string. %d A decimal number. %n.mf A floating point number. n = total number of digits; m = number of digits after decimal point. #BREAK# %[-]nc n specifies minimum field length for format type c, while - left-justifies value in field; otherwise, value is right-justified. Field widths are adjustable. For example, %3.2f limits a floating-point number to a total width of three digits, with two digits after the decimal point. format also can contain embedded escape sequences, n (newline) and t (tab) being the most common. Spaces and literal text can be placed in the format argument by quoting the entire argument. If there are multiple expressions to be printed, multiple formats should be specified. Example Using the script: {printf (”The sum on line %s is %d.n”, NR, $1+$2)} the following input line: 5 5 produces this output, followed by a newline: The sum on line 1 is 10. rand rand( ) Generate a random number between 0 and 1. This function returns the same series of numbers each time the script is executed, unless the random number generator is seeded using the srand function. return return [expr] Used at end of user-defined functions to exit function, returning the value of expr. sin sin(x) Return the sine of x, an angle in radians. split split(string,array[,sep]) Split string into elements of array array[1],…,array[n]. The string is split at each occurrence of separator sep. If sep is not specified, FS is used. If sep is a null string, a split is performed on every character. The number of array elements created is returned. #BREAK# sprintf sprintf [format [, expression(s)]] Return the value of one or more expressions, using the specified format (see printf). Data is formatted but not printed. sqrt sqrt(arg) Return square root of arg. srand srand(expr) Use expr to set a new seed for random number generator. Default is time of day. strftime strftime([format [,timestamp]]) Format timestamp according to format. Return the formatted string. The timestamp is a time-of-day value in seconds since midnight, January 1, 1970, UTC. The format string is similar to that of sprintf. (See the example for systime.) If timestamp is omitted, it defaults to the current time. If format is omitted, it defaults to a value that produces output similar to that of date. sub sub(r,s,t) Substitute s for first match of the regular expression r in the string t. Return 1 if successful; 0 otherwise. If t is not supplied, the default is $0. substr substr(string,m[,n]) Return substring of string beginning at character position m and consisting of the next n characters. If n is omitted, include all characters to the end of string. system system(command) Execute the specified shell command and return its status. The status of the command that is executed typically indicates its success (1), completion (0), or unexpected error (-1). The output of the command is not available for processing within the gawk script. systime systime() Return number of seconds since midnight UTC, January 1, 1970. Example Log the start and end times of a data-processing program: BEGIN { now = systime() mesg = strftime(”Started at %m/%d/%Y %H:%M:%S”, now) print mesg #BREAK# } process data … END { now = systime() mesg = strftime(”Ended at %m/%d/%Y %H:%M:%S”, now) print mesg} tolower tolower(str) Translate all uppercase characters in str to lowercase and return the new string. toupper toupper(str) Translate all lowercase characters in str to uppercase and return the new string. while while (condition) command Do command while condition is true (see if for a description of allowable conditions). A series of commands must be put within braces. PreviousHomeNext13.7. Group Listing of gawk Commands Book Index14. CVS and RCS Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext Chapter 14. CVS and RCS Contents: Basic ConceptsThe CVS UtilityCVS Administrator ReferenceCVS User ReferenceThe RCS UtilityOverview of RCS CommandsBasic RCS OperationsGeneral RCS SpecificationsAlphabetical Summary of RCS Commands CVS, and the older RCS, offer version control (or revision control), the practice of maintaining information about a project’s evolution so that prior versions may be retrieved, changes tracked, and, most importantly, the efforts of a team of developers coordinated. 14.1. Basic Concepts RCS (Revision Control System) works within a single directory. To accommodate large projects using a hierarchy of several directories, CVS creates two new concepts called the repository and the sandbox. The repository (also called an archive) is the centralized storage area, managed by the version control system and the repository administrator, which stores the projects’ files. The repository contains information required to reconstruct historical versions of the files in a project. An administrator sets up and controls the repository using the procedures and commands later in Section 14.3, “CVS Administrator Reference”. A sandbox (also called a working directory) contains copies of versions of files from the repository. New development occurs in sandboxes, and any number of sandboxes may be created from a single repository. The sandboxes are independent of one another and may #BREAK# contain files from different stages of the development of the same project. Users set up and control sandboxes using the procedures and commands found in Section 14.4, “CVS User Reference”, later in this chapter. In a typical interaction with the version control system, a developer checks out the most current code from the repository, makes changes, tests the results, and then commits those changes back to the repository when they are deemed satisfactory. 14.1.1. Locking and Merging Some systems, including RCS, use a locking model to coordinate the efforts of multiple developers by serializing file modifications. Before making changes to a file, a developer must not only obtain a copy of it, but he must also request and obtain a lock on it from the system. This lock serves to prevent (really dissuade) multiple developers from working on the same file at the same time. When the changes are committed, the developer unlocks the file, permitting other developers to gain access to it. The locking model is pessimistic: it assumes that conflicts must be avoided. Serialization of file modifications through locks prevents conflicts. But it is cumbersome to have to lock files for editing when bug-hunting. Often, developers will circumvent the lock mechanism to keep working, which is an invitation to trouble. Unlike RCS and SCCS, CVS uses a merging model which allows everyone to have access to the files at all times and supports concurrent development. The merging model is optimistic: it assumes that conflicts are not common and that when they do occur, it usually isn’t difficult to resolve them. CVS is capable of operating under a locking model via the -L and -l options to the admin command. Also, CVS has special commands (edit and watch) for those who want additional development coordination support. CVS uses locks internally to prevent corruption when multiple people are accessing the repository simultaneously, but this is different from the user- visible locks of the locking model discussed here. 14.1.2. Conflicts and Merging In the event that two developers commit changes to the same version of a file, CVS automatically defers the commit of the second committer’s file. The second developer then issues the cvs update command, which merges the first developer’s changes into the local file. In many cases, the changes will be in different areas of the file, and the merge is successful. However, if both developers have made changes to the same area of the file, the second to commit will have to resolve the conflict. This involves examination of the problematic area(s) of the file and selection among the multiple versions or making changes that resolve the conflict. #BREAK# CVS only detects textual conflicts, but conflict resolution is concerned with keeping the project as a whole logically consistent. Therefore, conflict resolution sometimes involves changing files other than the one about which CVS complained. For example, if one developer adds a parameter to a function definition, it may be necessary for all the calls to that function to be modified to pass the additional parameter. This is a logical conflict, so its detection and resolution is the job of the developers (with support from tools like compilers and debuggers); CVS won’t notice the problem. In any merge situation, whether or not there was a conflict, the second developer to commit will often want to retest the resulting version of the project because it has changed since the original commit. Once it passes, the developer will need to recommit the file. 14.1.3. Tagging CVS tracks file versions by revision number, which can be used to retrieve a particular revision from the repository. In addition, it is possible to create symbolic tags so that a group of files (or an entire project) can be referred to by a single identifier even when the revision numbers of the files are not the same (which is most often the case). This capability is often used to keep track of released versions or other important project milestones. For example, the symbolic tag hello-1_0 might refer to revision number 1.3 of hello.c and revision number 1.1 of Makefile (symbolic tags are created with the tag and rtag commands). 14.1.4. Branching The simplest form of development is linear, in which there is a succession of revisions to a file, each derived from the prior revision. Many projects can get by with a completely linear development process, but larger projects (as measured by number of files, number of developers, and/or the size of the user community) often run into maintenance issues that require additional capabilities. Sometimes, it is desirable to do some speculative development while the main line of development continues uninterrupted. Other times, bugs in the currently released version must be fixed while work on the next version is underway. In both of these cases, the solution is to create a branch (fork) from an appropriate point in the development of the project. If at a future point some or all of the changes on the branch are needed back on the main line of development (or elsewhere), they can be merged in (joined). Branches are forked with the tag -b command; they are joined with the update -j command. PreviousHomeNext13.8. Alphabetical Summary of Commands Book Index14.2. The CVS Utility #BREAK# Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 14.2. The CVS Utility This section offers general background about CVS. 14.2.1. CVS Command Format CVS commands are of the form: cvs global_options command command_options For example, here is a simple sequence of commands showing both kinds of options in the context of creating a repository, importing existing files, and performing a few common operations on them: user@localhost$ cvs -d /usr/local/cvsrep inituser@localhost$ cd ~/work/hellouser@localhost$ cvs -d /usr/local/cvsrep import -m ‘Import’ hello vendor startuser@localhost$ cd .. user@localhost$ mv hello hello.bakuser@localhost$ cvs -d /usr/local/cvsrep checkout hellouser@localhost$ cd hellouser@localhost$ vi hellouser@localhost$ cvs commit -m ‘Fixed a typo’user@localhost$ cvs tag hello-1_0user@localhost$ cvs remove -f Makefileuser@localhost$ cvs commit -m ‘Removed old Makefile’user@localhost$ cvs upd -r hello-1_0user@localhost$ cvs upd -A Some global options are common to both user and administrator commands, and some are specific to each of these. The common global options are described in the next section, and the user and administrator options are described in the Section 14.4, “CVS User Reference” and Section 14.3, “CVS Administrator Reference” sections, respectively. 14.2.2. Common Global Options Table 14-1 lists the global options that apply to both user and administrator commands. Table 14-1. Common Global Options Option Description -b bindir Location of external RCS programs. This option is obsolete, having been deprecated at CVS versions above 1.9.18. -T tempdir Absolute path for temporary files. Overrides the setting of $TMPDIR. #BREAK# -v –version Display version and copyright information. 14.2.3. Gotchas This section clarifies a few aspects of CVS that can sometimes cause confusion. CVS’s file orientation While directories are supported, they are not versioned in the same way as traditional files. This is particularly important in the early evolutionary stages of a project, when the structure may be in flux. Also, if the project is undergoing major changes, the structure is likely to change. See later in Section 14.3.4, “Hacking the Repository”. CVS’s text-orientation There is no equivalent to diff for binary files, although CVS’s support for binary files is usually sufficient. Use admin -kb to tell CVS a file is binary. CVS’s line-orientation Moving a segment of code from one place in a file to another is seen as one delete (from the old location) and an unrelated add (to the new location). CVS is not syntax-aware As far as CVS is concerned, small formatting changes are equivalent to sweeping logic changes in the same line ranges. RCS anachronisms CVS was originally built on top of RCS, but now all the RCS-related functionality is internal to CVS itself. RCS still shows up in the name of the $RCSBIN environment variable and the description of the -b option, which are now obsolete. PreviousHomeNext14. CVS and RCS Book Index14.3. CVS Administrator Reference Copyright 2001 O’Reilly & QKFIN. All rights reserved. #BREAK# Book HomeSearch this bookLinux in a Nutshell PreviousLinux in a Nutshell, 3rd EditionNext 14.3. CVS Administrator Reference This section provides details on creating and configuring repositories and performing other CVS administrative tasks. A single computer can run multiple copies of the CVS server, and each server can serve multiple repositories. 14.3.1. Creating a Repository Select a directory that will contain the repository files (/usr/local/cvsrep is used in the following examples). Use the init command to initialize the repository. Either set the $CVSROOT environment variable first: user@localhost$ export CVSROOT=/usr/local/cvsrepuser@localhost$ cvs init or use the -d option to specify the repository location: user@localhost$ cvs -d /usr/local/cvsrep init For information on importing code, see Section 14.4, “CVS User Reference”, especially Section 14.4.7.11, “import” and Section 14.4.7.1, “add” sections. 14.3.1.1. Setting up the password server If you want users to access the repository from other computers, then configure the pserver by doing the following as root: l Make sure there is an entry in /etc/services similar to the following: cvspserver 2401/tcp l If you are not using tcpwrappers, then place a line like this in /etc/inetd.conf: cvspserver stream tcp nowait root /usr/bin/cvs cvs –allow-root=/usr/local/cvsroot pserver l Or, if you are using tcpwrappers, then use a line like this: cvspserver stream tcp nowait root /usr/sbin/tcpd /usr/bin/cvs –allow-root=/usr/local/cvsroot pserver l Once these changes are in place, restart inetd (or send it the appropriate signal to cause it to re-read #BREAK# inetd.conf). 14.3.2. Security Issues The following are security issues that need to be considered when working with CVS: l The contents of files will be transmitted in the open over the network with pserver and rsh. With pserver, passwords are transmitted in the open as well. l When using a local repository (i.e., when CVS is not being used in client/server mode), developers need write access to the repository, which means they can hack it. l The CVS server runs as root briefly before changing its user ID. l The ~/.cvspass file must be kept unreadable by all users except the owner to prevent passwords from being accessible. l A user who has authority to make changes to the files in the CVSROOT module can run arbitrary programs. l Some of the options to the admin command are very dangerous, so it is advisable to restrict its use. This can be accomplished by creating a user group named cvsadmin. If this user group exists, then only users in that group can run the admin command (except admin -kkflag, which is available to everyone). 14.3.3. Repository Structure The CVS repository is implemented as a normal directory with special contents. This section describes the contents of the repository directory. 14.3.3.1. The CVSROOT directory The CVSROOT directory contains the administrative files for the repository; other directories in the repository contain the modules. The administrative files permit (and ignore) blank lines and comment lines in addition to the lines with real configuration information on them. Comment lines start with a hash mark (`#’). Some of the administrative files contain filename patterns to match file and directory names. These patterns are regular expressions like those used in GNU Emacs. Table 14-2 contains the special constructions used most often. Table 14-2. Filename Pattern Special Constructions Construction Description ^ Match the beginning of the string. $ Match the end of the string. . Match any single character. #BREAK# * Modify the preceding construct to match zero or more repetitions. CVS will perform a few important expansions in the contents of the administrative files before interpreting the results. First, the typical shell syntax for referring to a home directory is ~/, which expands to the home directory of the user running CVS; and ~user expands to the home directory of the specified user. In addition, CVS provides a mechanism similar to the shell’s environment variable expansion capability. Constructs such as ${variable} will be replaced by the value of the named variable. Variable names start with letters and consist entirely of letters, numbers, and underscores. Curly brackets may be omitted if the character immediately following the variable reference is not a valid variable name character. While this construct looks like a shell environment variable reference, the full environment is not available. Table 14-3 contains the built-in variables. Table 14-3. Administrative File Variables Variable Description CVSEDITOR EDITOR VISUAL The editor CVS uses for log file editing. CVSROOT The repository locator in use. USER The name of the user (on the server, if using a remote repository) running CVS. =var The value of a user-defined variable named var. Values for these variables are provided by the global -s option. In order to edit these files, check out the CVSROOT module from the repository, edit the files, and commit them back to the repository. You must commit the changes for them to affect CVS’s behavior. Table 14-4 describes the administrative files and their functions. Table 14-4. CVSROOT Files File Description checkoutlist Extra files to be maintained in CVSROOT commitinfo Specifications for commit governors config Settings to affect the behavior of CVS cvsignore Filename patterns of files to ignore cvswrappers Specifications for checkout and commit filters editinfo Specifications for log editors (obsolete) #BREAK# history Log information for the history command loginfo Specify commit notifier program(s) modules Module definitions notify Notification processing specifications passwd A list of users and their CVS-specific passwords rcsinfo Template form for log messages readers A list of users having read-only access taginfo Tag processing specifications users Alternate user email addresses for use with notify verifymsg Specify log message evaluator program writers A list of users having read/write access Since the editinfo file is obsolete, use the $EDITOR environment variable (or the -e option) to specify the editor and the verifymsg file to specify an evaluator. Each line of the taginfo file contains a filename pattern and a command line to execute when files with matching names are tagged. 14.3.3.2. The checkoutlist file Whenever changes to files in the CVSROOT module are committed, CVS prints the message: cvs commit: Rebuilding administrative file database to inform you that the checked-out copy in the repository has been updated to reflect any changes just committed. As with any other module directory in the repository, the CVSROOT directory contains RCS (*,v) files that retain the history of the files. But to use the files, CVS needs a copy of the latest revision. So, when CVS prints this message, it is checking out the latest revisions of the administrative files. If you have added files to the CVSROOT module (such as scripts to be called via entries in the loginfo file), you will need to list them in the checkoutlist file. This makes CVS treat them the same way as it treats the standard set of CVSROOT files. Each line in this file consists of a filename and an optional error message that is displayed in case there is trouble checking out the file. 14.3.3.3. The commitinfo file Whenever a commit is being processed, CVS consults this file to determine whether or not any precommit checking of the file is required. Each line of the file contains a directory name pattern, followed by the path of a program to invoke when files are commited in directories with matching names. Aside from the usual filename-pattern syntax, there are two special patterns: #BREAK# ALL If this pattern is present in the file, then all files are passed to the specified checking program. CVS then looks for a pattern that matches the name of each particular file and runs the additional checks found, if any. DEFAULT If this pattern is present in the file, all files for which there was no pattern match are sent to the specified checking program. The automatic match of every file to the ALL entry, if any, does not count as a match when determining whether or not to send the file to the DEFAULT checking program. CVS constructs the command line for the checking program by appending the full path to the directory within the repository and the list of files being committed (this means you can specify the first few command-line arguments to the program, if necessary). If the checking program exits with a nonzero status, the commit is aborted. The programs that run via this mechanism run on the server computer when a remote repository is used. Here is an example of a commitinfo file: ALL $CVSROOT/CVSROOT/commit-ALL.plDEFAULT $CVSROOT/CVSROOT/commit-DEFAULT.plCVSROOT$ $CVSROOT/CVSROOT/commit-CVSROOT.pl This example assumes you will create the script files in the CVSROOT module and add them to the checkoutlist file. 14.3.3.4. The config file Repository configuration is specified in the config administrative file. LockDir=dir Directs CVS to put its lock files in the alternate directory given instead of in the repository itself, allowing users without write access to the repository (but with write access to dir) to read from the repository. Version 1.10 doesn’t support alternate directories for lock files and reports an error if this option is set. Older versions of CVS (1.9 and older) don’t support this option either and will not report an error. Do not mix versions that support alternate directories for lock files with versions that don’t, since lock files in both places defeat the purpose of having them. RCSBIN=dir Obsolete (used in versions 1.9.12 to 1.9.18). This option used to tell CVS where to find RCS programs. Since all RCS-related functions are now handled internally, this option does nothing. SystemAuth=value CVS tries to authenticate users via the CVSROOT/passwd file first, and if that fails and this option is #BREAK# set to yes, CVS tries to authenticate via the system’s user database. This option is used with the password server. The default is yes. TopLevelAdmin=value If this option is set to yes, an additional CVS directory is created at the top-level directory when checkout is run. This allows the client software to detect the repository locator in that directory (see Section 14.4.1, “Repository Locators”). The default is no. This option is useful if you check out multiple modules to the same sandbox directory. If it is enabled, you won’t have to provide a repository locator after the first checkout; CVS infers it from the information in the top-level CVS directory created during the first checkout. 14.3.3.5. The cvsignore file The cvsignore administrative file contains a list of filename patterns to ignore, just like the .cvsignore files that can appear in sandboxes and user home directories. Unlike the filename patterns in other administrative files, these patterns are in sh syntax; they are not GNU Emacs-style regular expressions. There can be multiple patterns on a line, separated by whitespace (consequently, the patterns themselves cannot contain whitespace). Table 14-5 shows the most commonly used sh-style pattern constructs. Table 14-5. Filename Patterns for cvsignore Construct Description ? Any one character. * Any sequence of zero or more characters. Again, diverging from the standards used by the rest of the administrative files, the cvsignore file does not support comments. 14.3.3.6. The cvswrappers file While the cvsignore file allows CVS to ignore certain files, the cvswrappers file allows you to give CVS default options for commands that work with files. Lines in this file consist of a sh-style filename pattern followed by a -k (keyword substitution mode) option and/or an -m (update method) option. The legal values for -k are described in Table 14-19. The legal values for -m are COPY and MERGE. If -m COPY is specified, CVS will not attempt to merge the files. Instead, it presents the user with conflicting versions of the file, and he can choose one or the other or resolve the conflict manually. For example, to treat all files ending in .exe as binary, add this line to the file: *.exe -k b 14.3.3.7. The history file #BREAK# If this file exists, CVS inserts records of activity against the repository. This information produces displays of the cvs history command. The history file is not intended for direct reading or writing by programs other than CVS. A repository set up with cvs init automatically has a history file. 14.3.3.8. The loginfo file The loginfo administrative file works much like the commitinfo file and can use the special patterns ALL and DEFAULT. This file allows you to do something with commit log messages and related information. The programs called during loginfo processing receive the log message on standard input. Table 14-6 shows the three codes that can pass additional information to the called programs via command-line arguments. Table 14-6. Special loginfo Variables Variable Description s Filename V Pre-commit revision number v Post-commit revision number If a percent sign (%) followed by the desired variable is placed after the command path, CVS inserts the corresponding information as a whitespace-separated list with one entry for each file, preceded by the repository path (as with commitinfo). There can be only one percent sign on the command line, so if you want information from more than one variable, place the variable names inside curly brackets: %{…}. In this case, each file-specific entry has one field for each variable, separated by commas. For example, the code %{sVv} expands into a list like this: /usr/local/cvsrep/hello Makefile,1.1,1.2 hello.c,1.8,1.9 It can be helpful to send email notifications each time someone commits a file to the repository. Developers can monitor this stream of notices to determine when they should pull the latest development code into their private sandboxes. For example, consider a developer doing some preparatory work in his sandbox while he awaits stabilization and addition of another developer’s new library. As soon as the new library is added and committed, email notification goes out, and the waiting developer sees the code is ready to use. So, he runs cvs upd -d in the appropriate directory to pull in the new library code and then sets about integrating it with his work. It is simple to set up this kind of notification. Just add a line like this to the CVSROOT/loginfo file: DEFAULT mail -s %s developers@company.com Often, the email address is a mailing list, which has all the interested parties (developers or otherwise) on the distribution list. If you want to send messages to multiple email addresses, you can write a script to do that and have that script called via this file. Alternatively, you can use the log.pl program that comes as part of the CVS source distribution (located at /usr/local/src/cvs-1.10.8/contrib/log.pl, assuming CVS was unpacked into #BREAK# /usr/local/src). Instructions for its use are provided as comments in the file. 14.3.3.9. The modules file The top-level directories in a repository are called modules. In addition to these physical modules, CVS provides a mechanism to create logical modules through the modules administrative file. Here are the three kinds of logical modules: Alias Alias modules are defined by lines of the form: module_name -a alias_module … You can use the alias module name in CVS commands in the same way you use the modules named after the -a option. Regular Regular modules are defined by lines of the form: module_name [options] directory file … Checking out module_name results in the specified files from directory being checked out into a directory named module_name. The intervening directories (if any) are not reflected in the sandbox. Ampersand Ampersand modules are defined by lines of the form: module_name [options] &other_module … Checking out such a module results in a directory named module_name, which in turn contains copies of the other_module modules. Table 14-7 shows the options that can define modules. Table 14-7. Module Options Option Description -d name Overrides the default working directory name for the module -e prog Runs the program prog when files are exported from the module; the module name is passed to prog as the sole argument -i prog Runs the program prog when files are committed to the module; the repository directory of the committed files is passed in to prog as the sole argument #BREAK# -i prog Runs the program prog when files are checked out from the module; the module name is passed in to prog as the sole argument -s status Assigns a status descriptor to the module -t prog Runs the program prog when files are tagged in the module using rtag; the module name and the symbolic tag are passed to prog -u prog Runs the program prog when files are updated in the module’s top-level directory; the full path to the module within the repository is passed to prog as the sole argument Alias modules provide alternative names for other modules or shortcuts for referring to collections or subdirectories of other modules. Alias module definitions function like macro definitions in that they cause commands to run as if the expanded list of modules and directories was on the command line. Alias modules do not cause the modules of their definition to be grouped together under the alias name (use ampersand modules for that). For example, the definition: h -a hello makes the name h a synonym for the hello module. This definition: project -a library client server allows you to check out all three modules of the project as a unit. If an entry in the definition of an alias module is preceded by an exclamation point (!), then the named directory is excluded from the module. Regular modules allow you to create modules that are subsets of other modules. For example, the definition: header library library.h creates a module that just contains the header file from the library module. Ampersand modules are true logical modules. There are no top-level directories for them in the repository, but you can check them out to sandboxes, and directories with their names will then appear. The modules listed in the definition are below that directory. For example: project &library &client &server is almost the same as the alias module example given earlier, except that the submodules are checked out inside a subdirectory named project. In this file, long definitions may be split across multiple lines by terminating all but the last line with backslashes (). 14.3.3.10. The notify file This file is used in conjunction with the watch command. When notifications are appropriate, this file is consulted to determine how to do the notification. Each line of the notify file contains a filename pattern and a command line. CVS’s notification mechanism uses the command line specified to perform notifications for files having names that match the corresponding #BREAK# pattern. There is a single special-purpose variable, %s, that can appear in the command specification. When the command is executed, the name of the user to notify replaces the variable name. If the users administrative file exists, the user names are looked up there, and the resulting values are used for %s instead. This allows emails to be sent to accounts other than those on the local machine. Details are sent to the notification program via standard input. Typical usage of this feature is the single entry: ALL mail %s -s “CVS notification” In fact, this entry is present in the default notify file created when you run cvs init to create a repository (although it is initially commented out). 14.3.3.11. The passwd file If you access the repository via a pserver repository locator (see Section 14.4.1, “Repository Locators”), then CVS can have its own private authentication information, separate from the system’s user database. This information is stored in the CVSROOT/passwd administrative file. This feature provides anonymous CVS access over the Internet. By creating an entry for a public user (usually anoncvs or anonymous), the pserver can be used by many people sharing the public account. If you don’t want to create a system user with the same name as the public user, or if you have such a user but it has a different purpose, you can employ a user alias to map it to something else: anonymous:TY7QWpLw8bvus:cvsnoname Then, make sure you create the cvsnoname user on the system. You can use /bin/false as the login shell and the repository’s root directory as the home directory for the user. To restrict the public user to read-only access, list it in the CVSROOT/readers administrative file. Additionally, CVS’s private user database is useful even if you don’t want to set up anonymous CVS access. You can restrict access to a subset of the system’s users, provide remote access to users who don’t have general system access, or prevent a user’s normal system password from being transmitted in the clear over the network (see Section 14.3.2, “Security Issues”). There is no cvs passwd command for setting CVS-specific passwords (located in the repository file CVSROOT/passwd). CVS-specific user and password management are manual tasks. 14.3.3.12. The rcsinfo file CVS consults this file when doing a commit or import to determine the log message editor template. Each entry in the file consists of a filename pattern and the name of the file to use as the template for module directories with matching names. The ALL and DEFAULT special patterns apply to this file. 14.3.3.13. The readers file #BREAK# If this file exists, users listed in it have read-only access. 14.3.3.14. The taginfo file CVS consults this file whenever the tag or rtag commands are used. Entries in this file are filename patterns and program specifications. The ALL special pattern applies to this file. The taginfo file is called with the tag, the operation being performed, the module directory name (relative to the repository root), and the filename and revision number for each affected file. The valid operations are: add (for tag), del (for tag -d), and mov (for tag -F). If the taginfo program returns a nonzero status, the tag or rtag command that caused its execution is aborted. 14.3.3.15. The users file If this file exists, it is consulted during processing of the notify administrative file’s contents. Entries in this file consist of two colon-separated fields on a single line. The first field is the name of a user, and the second field is a value (normally the user’s email address on another machine). For example: john:john@somecompany.comjane:jane@anothercompany.com 14.3.3.16. The verifymsg file CVS consults this file to determine if log messages should be validated. If the program returns a nonzero status, the commit is aborted. The verifymsg file is called with the full path to a file containing the log message to be verified. The ALL special pattern is not supported for this file, although DEFAULT is. If more than one pattern matches, the first match is used. 14.3.3.17. The writers file If this file exists, users listed in it have read/write access (unless they are also listed in the readers file, in which case they have read-only access). 14.3.4. Hacking the Repository Since the repository is a normal directory, albeit one with special contents, it is possible to cd into the directory and examine its contents and/or make changes to the files and directories there. For each file that has been added there will be a file with the same name followed by ,v in a corresponding directory in the repository. These are RCS (the format, not the program) files that contain multiple versions of the file. NOTE Since the activities discussed in this section involve making changes directly to the repository instead of working through CVS commands, you should exercise extreme caution and have current backups when following these instructions. #BREAK# 14.3.4.1. Restructuring a project Restructuring the project by moving files and directories around (and possibly renaming them) in the repository will allow the files to retain their history. The standard way to rename a file when using CVS is to rename the file in the sandbox and do a cvs remove on the old name and a cvs add on the new name. This results in the file being disconnected from its history under the new name, so sometimes it is better to do the renaming directly in the repository, although doing this while people have active sandboxes is dangerous, since the sandboxes will contain information about a file that is no longer in the repository. 14.3.4.2. Bulk importing When importing an entire project, all of the project’s files will be added to the repository. But, if some of these files shouldn’t have been added, you’ll want to remove them. Doing a cvs remove will accomplish this, but copies of those files will remain in the repository’s .Attic directory forever. To avoid this, you can delete the files from the repository directly before checking out sandboxes from it. 14.3.5. Importing If you have an existing code base, you’ll want to import it into CVS in a way that preserves the most historical information. This section provides instructions for importing projects into CVS from code snapshots or other version control systems. All of these, except the code snapshot import procedure, are based upon conversion to RCS files, followed by placing the RCS files in the proper location in the CVS repository. 14.3.5.1. Importing code snapshots If you have maintained project history archives manually by taking periodic snapshots of the code, you can import the first snapshot, tag it with the date or version number, and then successively overlay the updated files from later archives. Each set can then be committed and tagged in order to bootstrap a repository that maintains the prior history. For example, first unpack the distributions (this assumes they unpack to directories containing the version numbers): user@localhost$ tar xvzf foo-1.0.tar.gzuser@localhost$ tar xvzf foo-1.1.tar.gzuser@localhost$ tar xvzf foo-2.0.tar.gz Next, make a copy of the first version, import it into the CVS repository, check it out to make a sandbox (since importing doesn’t convert the source directory into a sandbox), and use cvs tag to give it a symbolic name reflecting the project version: user@localhost$ mkdir foouser@localhost$ cp -R -p foo-1.0/* foouser@localhost$ cd foouser@localhost$ cvs import -m ‘Imported version 1.0′ foo vendor startuser@localhost$ cd .. user@localhost$ mv foo foo.bakuser@localhost$ cvs checkout foouser@localhost$ cd foouser@localhost$ cvs tag foo-1_0user@localhost$ cd .. #BREAK# Now, apply the differences between version 1.0 and 1.1 to the sandbox, commit the changes, and create a tag: user@localhost$ diff -Naur foo-1.0 foo-1.1 | (cd foo; patch -Np1) user@localhost$ cd foouser@localhost$ cvs commit -m ‘Imported version 1.1′ user@localhost$ cvs tag foo-1_1user@localhost$ cd .. Now, apply the differences between version 1.1 and 2.0 to the sandbox, commit the changes, and create a tag: user@localhost$ diff -Naur foo-1.1 foo-2.0 | (cd foo; patch -Np1) user@localhost$ cd foouser@localhost$ cvs commit -m ‘Imported version 2.0′ user@localhost$ cvs tag foo-2_0 Now, you can use the log command to view the history of the files, browse past versions of the files, and continue development under version control. 14.3.5.2. Importing from RCS If you are migrating from RCS to CVS, following these instructions will result in a usable CVS repository. This procedure involves direct modification of the CVS repository, so it should be undertaken with caution. Before beginning, make sure none of the files to be imported into CVS are locked by RCS. Then, create a new CVS repository and module (or a new module within an existing repository). Next, create directories in the CVS repository to mirror the project’s directory structure. Finally, copy all the version files (,v) from the project (which may be in RCS subdirectories) into the appropriate directories in the repository (without RCS subdirectories). For example, first move aside the directory under RCS control, create an empty directory to build the new CVS structure, import the directory, and then check it out to make a sandbox: user@localhost$ mv foo foo-rcsuser@localhost$ mkdir foouser@localhost$ cd foouser@localhost$ cvs import -m ‘New empty project’ foo vendor startuser@localhost$ cd .. user@localhost$ mv foo foo.bakuser@localhost$ cvs checkout foo Next, make directories and add them to the repository to match the structure in the RCS project: user@localhost$ cd foouser@localhost$ mkdir diruser@localhost$ cvs add diruser@localhost$ cd .. Now, copy the ,v files from the RCS project into the repository for the CVS project: user@localhost$ cp -p foo-rcs/*,v $CVSROOT/foo #BREAK# user@localhost$ cp -p foo-rcs/dir/*,v $CVSROOT/foo/dir Finally, issue the cvs update command in the sandbox directory to bring in the latest versions of all the files: user@localhost$ cd foouser@localhost$ cvs upd 14.3.5.3. Importing from SCCS To import from SCCS, use the sccs2rcs script located in the contrib directory of the CVS distribution to convert the files to RCS format, and then follow the preceding RCS procedure. You must have both CVS and SCCS installed for this to work. The script’s comments contain additional instructions. 14.3.5.4. Importing from PVCS To import from PVCS, use the pvcs_to_rcs script located in the contrib directory of the CVS distribution to convert the files to RCS format, and then follow the previous RCS procedure. You must have both CVS and PVCS installed for this to work. The script’s comments contain additional instructions. 14.3.6. Using an Interim Shared Sandbox Sometimes projects will develop unintended environmental dependencies over time, especially when there is no pressure for the code to be relocatable. A project developed outside version control may even be initially developed in place (at its intended installation location). While these practices are not recommended, they do occur in real-world situations; CVS can be helpful in improving the situation, by encouraging relocatability from the beginning of a project. The default mode of operation for CVS is multiple independent sandboxes, all coordinated with a central shared repository. Code that runs in this environment is necessarily (at least partially) relocatable. So, using CVS from the beginning of a project helps ensure flexibility. However, if a project is already well underway, an interim approach can be used. For example, you could convert the development area to a single shared sandbox by importing the code into CVS and checking it back out again: user@localhost$ cd /usr/local/baruser@localhost$ cvs import bar vendor startuser@localhost$ cd .. user@localhost$ mv bar bar.bakuser@localhost$ cvs checkout bar Chances are good that this approach is too aggressive and will check in more files than absolutely necessary. You can either go back and hack the repository to remove the files that shouldn’t be there or just issue the cvs remove command to delete them as you discover them. In addition, there will probably be some binary files in the sandbox that were imported as text files. Wherever you see a binary file that needs to remain in the repository, you should issue the command cvs admin -kb file, then make a fresh copy from the project backup. Finally, issue the command cvs commit file to commit the fixed file back to the repository. Having version control in place before making flexibility enhancements is a good idea, since it makes it easier #BREAK# to find (and possibly reverse) changes that cause trouble. The repository locator (see Section 14.4.1, “Repository Locators”) is specified via the -d option or the $CVSROOT environment variable. It is stored in the various sandbox CVS/root files. If you are using the password server (pserver), the user ID of the person checking out the sandbox will be remembered. If more than one person is working with a particular sandbox, they will have to share an account for CVS access. One way to do this is to have a neutral user account, with a password known by everyone with CVS access. Everyone can then issue the cvs login command with the same user ID and password and have access to the repository. Once you are no longer using a shared sandbox, this workaround won’t be necessary. However, during the time you are using a shared sandbox, it is important that the developers type their real user IDs into their log messages, since all the changes will appear to be made by the common user. 14.3.7. Global Server Option The server has one global option: –allow-root=rootdir. This option is used to tell the CVS server to accept and process requests for the specified repository. 14.3.8. Administrator Commands Table 14-8 lists the commands that CVS administrators can use to manage their repositories. Table 14-8. Administrator Commands Command Description admin adm rcs Perform administrative functions init Create a new repository server Run in server mode 14.3.8.1. admin admin [ -b[rev] ] [ -cstring ] [ -kkflag ] [ -l[rev] ] [ -L ] [ -mrev:msg ] [ -nname[:[rev]] ] [ -Nname[:[rev]] ] [ -orange ] [ -q ] #BREAK# [ -sstate[:rev] [ -t[file] ] [ -t-string ] [ -u[rev] ] [ -U ] [ files … ] The admin is used to perform administrative functions. If a cvsadmin user group exists, then only those users in that group will be able to run admin with options other than -k. Additional options that may be used with the admin command are listed in Table 14-9. Table 14-9. admin Options Option Description -b[rev] Set the default branch. -cstring Obsolete. Set the comment leader. -kkflag Set the default keyword substitution mode. -l[rev] Lock the specified revision. -L Enable strict locking. -mrev:msg Change the revision’s log message. -nname[:[rev]] Give the branch or revision specified the symbolic name name. -Nname[:[rev]] The same as -n, except that if name is already in use, it is moved. -orange Delete revisions permanently. -q Don’t print diagnostics. -sstate[:rev] Change the state of a revision. -t[file] Set the descriptive text in the RCS file. -t-string Set the descriptive text in the RCS file to string. -u[rev] Unlock the specified revision. -U Disable strict locking. If the revision specified for -l is a branch, the latest revision on that branch will be used. If no revision is given, the latest revision on the default branch is used. If the name given for -n is already in use, an error is generated. You can use -N to move a tag (change the revision associated with the tag); however, you should usually use cvs tag or cvs rtag instead. The -o option is very dangerous and results in a permanent loss of information from the repository. Use it with extreme caution and only after careful consideration. See Table 14-10 for the various ways to specify ranges. There must not be any branches or locks on the revisions to be removed. Beware of interactions between this #BREAK# command and symbolic names. If no file is specified to the -t option, CVS reads from standard input until it reaches the end of the file or a period on a line by itself. The determination of the target revision for the -u option is the same as for -l. Table 14-10. Range Formats Format Description rev1::rev2 Eliminate versions between rev1 and rev2, retaining only enough information to go directly from rev1 to rev2. The two specified versions are retained. ::rev The same as rev1::rev2, except the first revision is the branchpoint revision. rev:: The same as rev1::rev2, except the second revision is the end of the branch, and it is deleted instead of retained. rev Delete the specified revision. rev1: rev2 The same as rev1::rev2, except the two named revisions are deleted as well. :rev