Reputation: 127
2 examples with issues: What is wrong with the following statement syntax (perl newbie):
$mailCmd = sprintf("echo $message | /usr/ucb/mail -s 'X Detected an Invalid Thing' %s", $people_list);
When I do system($mailCmd)
or `$mailCmd`
, it results in:
sh: syntax error at line 2: `|' unexpected
Another one:
$message = "Invalid STUFF setup for ID $r. Please correct this ASAP.\n" .
"Number of thingies = $uno \n" .
"Another thingy = $id \n" ;
This produces:
sh: Number: not found
sh: Another: not found
Thanks in advance
Upvotes: 0
Views: 115
Reputation: 386331
The direct cause of the first problem is that you are executing the following command because the contents of $message
ends in a newline.
echo ...
| /usr/usb/mail ...
Both problems are the result of improper construction of the shell command. Fixed:
use String::ShellQuote qw( shell_quote );
my $echo_cmd = shell_quote('echo', $message);
my $mail_cmd = shell_quote('/usr/ucb/mail',
'-s' => 'X Detected an Invalid Thing',
$people_list,
);
system("$echo_cmd | $mail_cmd");
Or avoid echo
and the shell entirely:
use IPC::Run3 qw( run3 );
my @cmd = ('/usr/ucb/mail',
'-s' => 'X Detected an Invalid Thing',
$people_list,
);
run3 \@cmd, \$message;
Upvotes: 3