NetBSD/usr.bin/apply/apply.1

162 lines
4.9 KiB
Groff

.\" $NetBSD: apply.1,v 1.15 2016/03/14 09:53:37 wiz Exp $
.\"
.\" Copyright (c) 1983, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)apply.1 8.2 (Berkeley) 4/4/94
.\"
.Dd March 12, 2016
.Dt APPLY 1
.Os
.Sh NAME
.Nm apply
.Nd apply a command to a list of arguments
.Sh SYNOPSIS
.Nm
.Op Fl a Ns Ar c
.Op Fl Ns Ar #
.Ar command arguments ...
.Sh DESCRIPTION
.Nm
divides its
.Ar arguments
into fixed-size groups and runs
.Ar command
in turn on each group.
.Pp
On each execution of
.Ar command ,
each character sequence of the form
.Dq Li \&%d
in
.Ar command ,
where
.Ar d
is a digit from 1 to 9, is replaced with the
.Ar d Ns \'th
argument from the current argument group.
The argument group size is set to the largest such
.Ar d
found.
Any given argument number can be used arbitrarily many times.
(Including zero.)
.Pp
If no explicit substitution sequences are found in
.Ar command ,
the current argument group is substituted after
.Ar command
delimited by spaces, and the argument group size defaults to 1 and can
be set with the
.Fl #
option.
.Pp
If the argument group size is set to 0, one argument from
.Ar arguments
is taken for each execution of
.Ar command
anyway, but is discarded and not substituted; thus,
.Ar command
is run verbatim once for every argument.
.Pp
The options are as follows:
.Bl -tag -width "-ac"
.It Fl Ns Ar #
Set the argument group size.
Ignored if explicit substitutions are used.
.It Fl a Ns Ar c
Change the magic substitution character from the default
.Dq Li %
to
.Ar c .
.El
.Sh ENVIRONMENT
The following environment variable affects the execution of
.Nm :
.Bl -tag -width SHELL
.It Ev SHELL
Pathname of the shell to use to execute
.Ar command .
If this variable is not defined, the Bourne shell is used.
.El
.Sh FILES
.Bl -tag -width /bin/sh -compact
.It Pa /bin/sh
Default shell.
.El
.Sh EXAMPLES
.Bl -tag -width apply -compact
.It Li "apply echo *"
Prints the name of every file in the current directory.
.It Li "apply \-2 diff a1 b1 a2 b2 a3 b3"
Compares the `a' files to the `b' files.
.It Li "apply \-0 who 1 2 3 4 5"
Runs
.Xr who 1
5 times.
.It Li "apply \'ln %1 /home/joe/joe.%1\'" *
Hard-links all files in the current directory into the directory
.Pa /home/joe ,
with their names prefixed with "joe.".
.It Li "apply \'cvs diff %1 > %1.diff'" *.c
Diff all C sources in the current directory against the last
checked-in version and store each result in its own output file.
.El
.Sh HISTORY
The
.Nm
command appeared in
.Bx 4.2 .
.Sh AUTHORS
.An Rob Pike
.Sh RESTRICTIONS
The complete command to be executed on each iteration is assembled as
a string without additional quoting and then passed to a copy of the
shell for parsing and execution.
Thus, commands or arguments that contain spaces or shell
metacharacters may behave in unexpected ways.
.Pp
To protect a shell metacharacter fully it must be quoted twice, once
against the current shell and once against the subshell used for
execution.
Similarly, for a shell metacharacter to be interpreted by the subshell
it must be quoted to protect it from the current shell.
A simple rule of thumb is to enclose the entire
.Ar command
in single quotes
.Pq ''
so that the current shell does not interpret any of it.
.Sh BUGS
There is no easy way to produce the literal string
.Dq %1
in
.Ar command .
.Pp
.Nm
unconditionally inserts "exec" at the beginning of each copy of
.Ar command
so compound commands may not behave as intended.