add kvpairs2td
[hband-tools.git] / user-tools / substenv
blob983b8e24a076d3bfac3eff95173a57076d996153
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 system('substenv', 'ls', '$HOME/.config')
22 runs:
24 execve('ls', ['ls', '/home/jdoe/.config'])
26 =head1 NOTES
28 Does not support full shell-like variable interpolation.
29 Use a real shell for it.
31 =head1 RATIONALE
33 Sometimes you don't want a shell to be in the picture when composing commands,
34 yet need to weave some environment variable into it.
36 =head1 SEE ALSO
38 envsubst(1) from gettext-base package
40 =cut
43 @run_cmd = ();
45 for my $arg (@ARGV)
47 $arg =~ s/\$([A-Z0-9_]+)/$ENV{$1}/g;
48 push @run_cmd, $arg;
51 exec {$run_cmd[0]} @run_cmd;
52 exit 127;