5 $0 =~ s/.*\/([^\/]+)$/$1/;
8 'r' => {symbol
=> '<', word
=> 'read'},
9 'c' => {symbol
=> '>', word
=> 'create/clobber'},
10 'rw' => {symbol
=> '+<', word
=> 'read/write'},
11 'a' => {symbol
=> '>>', word
=> 'append'},
18 my $fdspec = shift @ARGV;
19 if($fdspec eq '--') { last; }
20 if($fdspec =~ /^(?'fd'\d+|std(in|out|err)):(?'dest'-|(?'mode'r|c|rw|a):(file:(?'path'.+)|fd:(?'fileno'\d+|std(in|out|err))))$/)
22 my ($fd, $dest, $mode, $path, $fileno) = ($+{'fd'}, $+{'dest'}, $+{'mode'}, $+{'path'}, $+{'fileno'});
23 $fd = 0 if $fd eq 'stdin';
24 $fd = 1 if $fd eq 'stdout';
25 $fd = 2 if $fd eq 'stderr';
26 $fileno = 0 if $fileno eq 'stdin';
27 $fileno = 1 if $fileno eq 'stdout';
28 $fileno = 2 if $fileno eq 'stderr';
36 mode
=> $perlopenmode{$mode}->{'symbol'} . (defined $fileno ?
'&=' : ''),
37 modename
=> $perlopenmode{$mode}->{'word'},
38 file
=> defined $fileno ?
$fileno : $path,
43 warn "invalid redirect specification: $fdspec\n";
44 pod2usage
(-exitval
=>2, -verbose
=>99);
50 for my $redir (@redirs)
52 open $fh, $redir->{'mode'}, $redir->{'file'} or die "$0: can not open ($redir->{'modename'}) $redir->{'file'}: $!\n";
53 dup2
(fileno $fh, $redir->{'fd'}) or die "$0: can not duplicate fd of $redir->{'file'}: $!\n";
56 exec {$command[0]} @command;
66 redirexec - Execute a command with some file descriptiors redirected.
70 redirexec [I<FILENO>:I<MODE>:file:I<PATH>] -- I<COMMAND> I<ARGS>
72 redirexec [I<FILENO>:I<MODE>:fd:I<FD>] -- I<COMMAND> I<ARGS>
74 redirexec [I<FILENO>:-] -- I<COMMAND> I<ARGS>
78 Setup redirections before executing I<COMMAND>.
79 You can setup the same type of file and file descriptor redirections as in shell.
81 I<FILENO> and I<FD> are file descriptor integers or literal "stdin", "stdout", or "stderr".
107 +-----------------+--------------------------+
108 | shell syntax | redirexec(1) equivalents |
109 +=================+==========================+
110 | > output.txt | stdout:c:file:output.txt |
111 | | 1:c:file:output.txt |
112 +-----------------+--------------------------+
113 | 2>&1 | stderr:c:fd:stdout |
115 +-----------------+--------------------------+
116 | </dev/null | 0:r:file:/dev/null |
118 +-----------------+--------------------------+
119 | >/dev/null 2>&1 | 1:- 2:- |
120 +-----------------+--------------------------+
125 redirfd by execlineb(1)