output asked headers in the order they were asked; avoid header name spoofing by...
[hband-tools.git] / user-tools / substenv
blobfad9b4aed634597544659de2953f28fef3c373b4
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 substenv - Substitute environment variables in parameters and run the resulting command
9 =head1 SYNOPSIS
11 substenv I<COMMAND> [I<ARGS>]
13 =head1 DESCRIPTION
15 Replace all occurrances of C<$NAME> in I<COMMAND> and I<ARGS> to the I<NAME> environment
16 variable's value, then run I<COMMAND>.
18 =head1 EXAMPLE
20 This function call, in C, runs substenv(1),
21 note, there is no dollar-interpolation in C.
23 execve("substenv", ["substenv", "ls", "$HOME/.config"])
25 Then substenv issues this system call:
27 execve("ls", ["ls", "/home/jdoe/.config"])
29 =head1 NOTES
31 Does not support full shell-like variable interpolation.
32 Use a real shell for it.
34 =head1 RATIONALE
36 Sometimes you don't want a shell to be in the picture when composing commands,
37 yet need to weave some environment variable into it.
39 =head1 SEE ALSO
41 envsubst(1) from gettext-base package
43 =cut
46 @run_cmd = ();
48 for my $arg (@ARGV)
50 $arg =~ s/\$([A-Z0-9_]+)/$ENV{$1}/g;
51 push @run_cmd, $arg;
54 exec {$run_cmd[0]} @run_cmd;
55 ($errno, $errstr) = (int $!, $!);
56 warn "$0: ${run_cmd[0]}: $errstr\n";
57 exit 125+$errno;