$ cat if-mail.pl #!/usr/bin/perl exit 0 unless (my @lines = ); open(my $mail, "|-", join " ", "/usr/bin/mail", @ARGV) or die $!; print $mail @lines; $ LC_ALL=C ./if-mail.pl root@localhost -s "do not run echo; touch ~/zzz && ls ~/zzz - it does not make sense" TEST ^D ls: cannot access -: No such file or directory ls: cannot access it: No such file or directory ls: cannot access does: No such file or directory ls: cannot access not: No such file or directory ls: cannot access make: No such file or directory ls: cannot access sense: No such file or directory /home/aitap/zzz $ ./if-mail.pl root@localhost -s "try running echo *" TEST ^D #### $ mail From: Krylov Ivan To: echo@tarkus, not@tarkus, root@localhost, run@tarkus Subject: do Message-ID: Date: Пнд, 02 Дек 2013 00:03:21 +0400 ---------------------------------------------------------------- TEST ---------------------------------------------------------------- From: Mail Delivery System To: aitap@tarkus Subject: Mail delivery failed: returning message to sender Message-ID: Date: Пнд, 02 Дек 2013 00:03:22 +0400 ---------------------------------------------------------------- This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: echo@tarkus Unrouteable address not@tarkus Unrouteable address run@tarkus Unrouteable address ------ This is a copy of the message, including all the headers. ------ <...> From: Krylov Ivan To: echo@tarkus, if-mail.pl@tarkus, root@localhost, running@tarkus Subject: try Message-ID: Date: Пнд, 02 Дек 2013 00:03:55 +0400 ---------------------------------------------------------------- TEST ---------------------------------------------------------------- From: Mail Delivery System To: aitap@tarkus Subject: Mail delivery failed: returning message to sender Message-ID: Date: Пнд, 02 Дек 2013 00:03:55 +0400 ---------------------------------------------------------------- This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: echo@tarkus Unrouteable address if-mail.pl@tarkus Unrouteable address running@tarkus Unrouteable address ------ This is a copy of the message, including all the headers. ------ <...>