Hello Monks Sorry for my babyperl , I had happy perl learning, but I find a little Wtf! issue. I try to get db2versions, db2instances and list of data bases without modules. I have The "root powEr!" but When I trying "su - $user -c 'command to get DB list' " I get some like this.

Product instance full path /opt/wheredb2/Version Current instance Userid
but the command doesn't execute

This is my code ...

use warnings; use diagnostics; use strict; use 5.010; use Cwd; use DB2::Admin::Constants; ############################## + + + + ###Get Version and instances## + + + + ############################## + + + + sub check_for_instance{ open(EXEC, '-|', 'db2ls | awk \'!/Install/ && !/----/ \'') +or die "Can't exec: $!\n"; # Now read the output just like a file + + + + while(my $line = <EXEC>) { chomp $line; if($line=~/(\/db2\/V\d.\d+)/){ print "Product instance full path\n $`"."$1\n"; foreach ($line) { my $Path="$`"."$1"."/instance/"; chdir($Path) or die "Cant chdir to $Path $!"; my $dir = getcwd; open my $cmd, '-|', './db2ilist'; while ($line = <$cmd>) { say "Current instance \n$line"; my $user="$line"; my $action="db2 list db directory"; ######################### ### This doesn't works # ######################### system ("su -l - $user -c \'$action > DBlist.txt && cat + DBlist.txt\'"); ########################### # # # this works ! XD # ########################### #system ("su -l - MyRealuser -c \'$action > DBlist.txt && cat DBlist. +txt\'"); ####### } close $cmd; } } } close(EXEC); } &check_for_instance;

btw ,I tried to use sudo - $user -c 'command', sudo su - $user "command", sudo - $user -c "command". but when i use directly the userid su - "Myrealuser" 'comman' works as i waiting.

I hope someone can give me some light


In reply to run shell command as another user by ox1d0

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.