In wrapper script
exec
is used mainly in wrapper scripts.
If you want to modify the environment for a program before executing the main program, you'd often write an script and at the end of it start the main program. But there is no need for the script to stay in memory at that time. So, exec
is used in these cases so that, the main program can replace the mother script.
Here is a practical example of it. It's mate-terminal.wrapper
script comes with mate-terminal. It starts the mate-terminal
with some extra arguments by checking user's environments.
#!/usr/bin/perl -w
my $login=0;
while ($opt = shift(@ARGV))
{
if ($opt eq '-display')
{
$ENV{'DISPLAY'} = shift(@ARGV);
}
elsif ($opt eq '-name')
{
$arg = shift(@ARGV);
push(@args, "--window-with-profile=$arg");
}
elsif ($opt eq '-n')
{
# Accept but ignore
print STDERR "$0: to set an icon, please use -name <profile> and set a profile icon\n"
}
elsif ($opt eq '-T' || $opt eq '-title')
{
push(@args, '-t', shift(@ARGV));
}
elsif ($opt eq '-ls')
{
$login = 1;
}
elsif ($opt eq '+ls')
{
$login = 0;
}
elsif ($opt eq '-geometry')
{
$arg = shift(@ARGV);
push(@args, "--geometry=$arg");
}
elsif ($opt eq '-fn')
{
$arg = shift(@ARGV);
push(@args, "--font=$arg");
}
elsif ($opt eq '-fg')
{
$arg = shift(@ARGV);
push(@args, "--foreground=$arg");
}
elsif ($opt eq '-bg')
{
$arg = shift(@ARGV);
push(@args, "--background=$arg");
}
elsif ($opt eq '-tn')
{
$arg = shift(@ARGV);
push(@args, "--termname=$arg");
}
elsif ($opt eq '-e')
{
$arg = shift(@ARGV);
if (@ARGV)
{
push(@args, '-x', $arg, @ARGV);
last;
}
else
{
push(@args, '-e', $arg);
}
last;
}
elsif ($opt eq '-h' || $opt eq '--help')
{
push(@args, '--help');
}
}
if ($login == 1)
{
@args = ('--login', @args);
}
exec('mate-terminal',@args);
The point to notice here is, there is an exec
call, which replaces this script in memory.
Here is a similar question answered in Unix & Linux StackExchange Site - https://unix.stackexchange.com/q/270929/19288
To redirect file-descriptors
Another common use of exec
is in redirecting file-descriptors. stdin
, stdout
, stderr
can be redirected to files using exec.
Redirecting stdout
- exec 1>file
will cause the standard output to be a file named file
for the end of the current shell session. Anything to output to the display will be in the file.
Redirecting stdin
- It can also be used to redirect the stdin
to a file. For example, If you want to execute a script file script.sh
, you can just redirect the stdin
to the file using exec 0<script.sh
.
Best Answer
You're almost there. You need a
\;
on the end to let find know where the end of the command is.For commands that can take multiple arguments at a time (eg if you wanted to just
stat
each filename) you can use\+
instead. This will build a compound argument which can execute a faster because it doesn't fork out for every single file:That won't work here for your example though.
Just a test harness to highlight that quotes aren't required:
If it wasn't handling quoting for us, we'd see this instead of COMBO: