8 redirexec - Execute a command with some file descriptiors redirected.
12 redirexec [I<FILENO>:I<MODE>:file:I<PATH>] [--] I<COMMAND> I<ARGS>
14 redirexec [I<FILENO>:I<MODE>:fd:I<FD>] [--] I<COMMAND> I<ARGS>
16 redirexec [I<FILENO>:-] [--] I<COMMAND> I<ARGS>
20 Setup redirections before executing I<COMMAND>.
21 You can setup the same type of file and file descriptor redirections as in shell.
23 I<FILENO> and I<FD> are file descriptor integers or literal "stdin", "stdout", or "stderr".
49 +-----------------+--------------------------+
50 | shell syntax | redirexec(1) equivalents |
51 +=================+==========================+
52 | > output.txt | stdout:c:file:output.txt |
53 | | 1:c:file:output.txt |
54 +-----------------+--------------------------+
55 | 2>&1 | stderr:c:fd:stdout |
57 +-----------------+--------------------------+
58 | </dev/null | 0:r:file:/dev/null |
60 +-----------------+--------------------------+
61 | >/dev/null 2>&1 | 1:- 2:- |
62 +-----------------+--------------------------+
67 redirfd by execlineb(1)
74 $0 =~ s/.*\/([^\/]+)$/$1/;
77 'r' => {symbol
=> '<', word
=> 'read'},
78 'c' => {symbol
=> '>', word
=> 'create/clobber'},
79 'rw' => {symbol
=> '+<', word
=> 'read/write'},
80 'a' => {symbol
=> '>>', word
=> 'append'},
87 my $fdspec = shift @ARGV;
88 if($fdspec eq '--') { last; }
89 if($fdspec =~ /^(?'fd'\d+|std(in|out|err)):(?'dest'-|(?'mode'r|c|rw|a):(file:(?'path'.+)|fd:(?'fileno'\d+|std(in|out|err))))$/)
91 my ($fd, $dest, $mode, $path, $fileno) = ($+{'fd'}, $+{'dest'}, $+{'mode'}, $+{'path'}, $+{'fileno'});
92 $fd = 0 if $fd eq 'stdin';
93 $fd = 1 if $fd eq 'stdout';
94 $fd = 2 if $fd eq 'stderr';
95 $fileno = 0 if $fileno eq 'stdin';
96 $fileno = 1 if $fileno eq 'stdout';
97 $fileno = 2 if $fileno eq 'stderr';
105 mode
=> $perlopenmode{$mode}->{'symbol'} . (defined $fileno ?
'&=' : ''),
106 modename
=> $perlopenmode{$mode}->{'word'},
107 file
=> defined $fileno ?
$fileno : $path,
112 # does not seem to be a redirection specification, take it as the command
113 unshift @ARGV, $fdspec;
120 for my $redir (@redirs)
122 open $fh, $redir->{'mode'}, $redir->{'file'} or die "$0: can not open ($redir->{'modename'}) $redir->{'file'}: $!\n";
123 dup2
(fileno $fh, $redir->{'fd'}) or die "$0: can not duplicate fd of $redir->{'file'}: $!\n";
126 exec {$command[0]} @command;
127 ($errno, $errstr) = (int $!, $!);
128 warn "$0: ${command[0]}: $errstr\n";