aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-02 21:12:38 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-02 21:12:38 +0200
commit75f6926390f33f62b95b355d48bc5454243ddcea (patch)
tree43188f35ea35b96ba11efc3566da012480c7c945
parent6f014212fb084876ec0e0f63b07a304437038fed (diff)
downloadxutil-75f6926390f33f62b95b355d48bc5454243ddcea.tar.gz
Add 'each' utility
-rwxr-xr-xeach18
-rw-r--r--each.148
2 files changed, 66 insertions, 0 deletions
diff --git a/each b/each
new file mode 100755
index 0000000..bed5bb8
--- /dev/null
+++ b/each
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+
+$i = 0;
+for (@ARGV) {
+ if (/^;$/) {
+ $i++;
+ next;
+ }
+ s/"/"'"'"/g;
+ s/\$/\\\$/g;
+ s/\\\$\+/\$/g;
+ $commands[$i] .= "\"$_\" "
+}
+
+while (<STDIN>) {
+ chomp($ENV{x} = $_);
+ system($_) for @commands;
+}
diff --git a/each.1 b/each.1
new file mode 100644
index 0000000..9790be4
--- /dev/null
+++ b/each.1
@@ -0,0 +1,48 @@
+.Dd $Mdocdate$
+.Dt each 1
+.Os
+.
+.Sh NAME
+.Nm each
+.Nd do with each line
+.
+.Sh SYNOPSIS
+.Nm each
+.Ar command
+.Op Ar \\\\; command ...
+.
+.Sh DESCIPTION
+.Pp
+.Nm
+runs each given
+.Ar command
+for every line read on standard input.
+With a command, the contents of the line is assigned to the variable
+.Ev x ,
+which can be accessed with the normal shell variable interpolation syntax,
+except you need to put a plus sign after the dollar sign:
+.Bd -literal -offset indent
+$+x
+$+{x}
+$+{x#/}
+$+{x%.mp3}
+$+{x%%.*}
+.Ed
+.Pp
+This syntax generally doesn't need to be quoted
+when entered in the Bourne shell
+(unless you need to put commas inside the curly braces
+or have files who names begin with
+.Ql $+ ) .
+.Sh AUTHORS
+.Pp
+.Nm
+is written by John Ankarström
+.Aq Mt john (at) ankarstrom.se .
+.Sh BUGS
+.Pp
+It is not possible to insert a literal
+.Ql $+ .
+Quoting it with a backslash prevents the interpolation,
+but results in the literal string
+.Ql $ .