in reply to null output on program

G'day Nobby,

I see that the STDOUT/STDERR problem has been explained. Here's some additional comments.

Here's some output from my command line:

$ ls -al a_real_dir total 4 drwxr-xr-x 1 ken None 0 Nov 19 18:20 . drwxr-xr-x 1 ken None 0 Nov 19 19:10 .. -rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_A -rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_B $ ls -al not_a_real_dir ls: cannot access 'not_a_real_dir': No such file or directory

Here's a demo script that uses all of the points I raised:

#!/usr/bin/env perl use strict; use warnings; use autodie; use HTML::Escape 'escape_html'; my %commands = (ls => '/usr/bin/ls -al'); my @targets = qw{a_real_dir not_a_real_dir}; my $program = 'ls'; print "<html>\n<head>...</head>\n<body>\n"; for my $target (@targets) { my $cmd = "$commands{$program} $target 2>&1"; print '<h1>', escape_html($cmd), "</h1>\n<pre>\n"; { open my $cmd_pipe, '-|', $cmd; print escape_html($_) while <$cmd_pipe>; } print "</pre>\n"; } print "</body>\n</html>\n";

Output:

<html> <head>...</head> <body> <h1>/usr/bin/ls -al a_real_dir 2&gt;&amp;1</h1> <pre> total 4 drwxr-xr-x 1 ken None 0 Nov 19 18:20 . drwxr-xr-x 1 ken None 0 Nov 19 19:10 .. -rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_A -rw-r--r-- 1 ken None 0 Nov 19 18:20 demo_file_B </pre> <h1>/usr/bin/ls -al not_a_real_dir 2&gt;&amp;1</h1> <pre> /usr/bin/ls: cannot access &#39;not_a_real_dir&#39;: No such file or d +irectory </pre> </body> </html>
"I have omitted a lot of the header and html code, ..."

That's fine. My code is only intended as a demo example: adapt to your needs.

— Ken

Replies are listed 'Best First'.
Re^2: null output on program
by Nobby (Novice) on Nov 20, 2022 at 05:45 UTC
    Thanks, I've removed chomp and newline, as mentioned for brevity some code was not included (the sanitization parts that only allow set chars), this script has worked safely for over 20 years, but it was ipv4 limited, I am moving it to work with ipv6 as well. can't really do the print while runmain, as the runmain output is checked for a (simplified eg) if (runcode eq x} { print foo if /Y/; print bar if /Z/} kind of thing that it looks for in line by line output (SPF records and different options, produces this found or not found, I will be moving that into another external script one day so I can get it out of this one, it has about a dozen options as $program, and the spf is only one thats checked inline) Thanks for other pointers, I've made notes so when time comes I can clean it up more