So I have been struggling with a task to create a script which executes commands on remote hosts in parallel. While I have that working using the parallell::forkmanager module, I am getting whitespace from the command output that I have been unable to remove. One would think this would be simple but it eludes me. I have created the following sub to execute the commands and return the output as well as exit status.

sub exitstatus { ## # Exit Status Trap # Arguments required: &exitstatus(cmd); # # Examples: # my ($output, $status) = &exitstatus('/bin/cp', '/somepath/somefile +', '/some/new/location'); # my ($output, $status) = &exitstatus($somecommand); # # Returns text output from the command as well as exit status. ## my $command = join ' ', @_ or die "No command specified\n"; if ($debug) { print "command: $command\n"; } ($_ = qx{$command 2>&1}, $? >> 8); }
And passing a simple command such as 'uptime', 'date' or 'hostname' gives me a leading space or a trailing carriage return that I am unable to remove with chomp or s/// SAMPLE OUTPUT:
Running: 'hostname' on admin001: admin001 Running: 'uptime' on admin001: 09:42:50 up 333 days, 22:24, 4 users, load average: 0.05, 0.10, 0.3 +6 Running: 'date -I' on admin001: 2014-05-29 Running: 'hostname' on mon001: mon001 Running: 'uptime' on mon001: 09:42:51 up 333 days, 22:23, 1 user, load average: 1.18, 0.74, 0.57 Running: 'date -I' on on001: 2014-05-29
My question is this. Is there a way to trim the whitespace of the command output (both leading and trailing) without removing or conflicting with the exit status of the command. Every time I attempt to craft such code, I am met with the error message:
Useless use of right bitshift (>>) in void context at - line 437.

In reply to Erroneous Whitespace by lokista

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.