redirect system command STDER

redirect system command STDER

am 19.07.2011 21:14:10 von Tessio Fechine

--0050450176cca585b404a870ebf6
Content-Type: text/plain; charset=ISO-8859-1

Hello,
I have a subroutine that uses useradd to create accounts

--
@cmd = ('useradd', '-m', $account);
my $result = system @cmd;
--

but when useradd fails, I need to stop it from sending the error message to
STDER.
Is it possible with system?

Thanks!

--0050450176cca585b404a870ebf6--

Re: redirect system command STDER

am 19.07.2011 23:35:33 von Kevin Spencer

On Tue, Jul 19, 2011 at 12:14 PM, Tessio Fechine wrote:
> Hello,
> I have a subroutine that uses useradd to create accounts
>
> --
> @cmd = ('useradd', '-m', $account);
> my $result = system @cmd;
> --
>
> but when useradd fails, I need to stop it from sending the error message to
> STDER.
> Is it possible with system?
>
> Thanks!

Just as you would when using the shell, redirect STDERR somewhere
else: "2>/dev/null".

Kevin.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: redirect system command STDER

am 20.07.2011 01:22:14 von Jim Gibson

On 7/19/11 Tue Jul 19, 2011 12:14 PM, "Tessio Fechine"
scribbled:

> Hello,
> I have a subroutine that uses useradd to create accounts
>
> --
> @cmd = ('useradd', '-m', $account);
> my $result = system @cmd;
> --
>
> but when useradd fails, I need to stop it from sending the error message to
> STDER.
> Is it possible with system?

You can use a shell process to discard STDERR messages (untested):

my $cmd = "useradd -m $account 2> /dev/null";
my $result = system($cmd);

You could do the same by writing a shell script to redirect STDERR and call
that from your Perl program.

You can also use the IPC::Open3 module to capture STDERR. See 'perldoc
IPC::Open3' for examples.




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: redirect system command STDER

am 20.07.2011 09:45:44 von Christian Walde

On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine wr=
ote:

> Hello,
> I have a subroutine that uses useradd to create accounts
>
> --
> @cmd =3D ('useradd', '-m', $account);
> my $result =3D system @cmd;
> --
>
> but when useradd fails, I need to stop it from sending the error messag=
e to
> STDER.
> Is it possible with system?
>
> Thanks!
>

For an easy and cross-platform solution you can use Capture::Tiny to capt=
ure both STDERR and STDOUT separately and then have your perl code decide=
what to do with each.

--=20
With regards,
Christian Walde

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: redirect system command STDER

am 20.07.2011 11:42:45 von Shlomi Fish

Hi Jim,

On Tue, 19 Jul 2011 16:22:14 -0700
Jim Gibson wrote:

> On 7/19/11 Tue Jul 19, 2011 12:14 PM, "Tessio Fechine" om>
> scribbled:
>=20
> > Hello,
> > I have a subroutine that uses useradd to create accounts
> >=20
> > --
> > @cmd =3D ('useradd', '-m', $account);
> > my $result =3D system @cmd;
> > --
> >=20
> > but when useradd fails, I need to stop it from sending the error messag=
e to
> > STDER.
> > Is it possible with system?
>=20
> You can use a shell process to discard STDERR messages (untested):
>=20
> my $cmd =3D "useradd -m $account 2> /dev/null";
> my $result =3D system($cmd);
>=20

The problem with interpolating strings into shell commands like that is that
someone may put malicious code in it:

my $account =3D 'foo; rm -fr / ';

so be careful - see: http://shlomif-tech.livejournal.com/35301.html
(Code/Markup Injection and Its Prevention ).

Regards,

Shlomi Fish

> You could do the same by writing a shell script to redirect STDERR and ca=
ll
> that from your Perl program.
>=20
> You can also use the IPC::Open3 module to capture STDERR. See 'perldoc
> IPC::Open3' for examples.
>=20
>=20
>=20
>=20



--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/

An apple a day keeps the doctor away.
Two apples a day will keep two doctors away.=20
â€=94 one of Shlomi Fishâ€=99s relatives

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: redirect system command STDER

am 21.07.2011 03:08:43 von derykus

On Jul 20, 12:45=A0am, walde.christ...@googlemail.com ("Christian
Walde") wrote:
> On Tue, 19 Jul 2011 21:14:10 +0200, Tessio Fechine wr=
ote:
> > Hello,
> > I have a subroutine that uses useradd to create accounts
>
> > --
> > @cmd =3D ('useradd', '-m', $account);
> > my $result =3D system @cmd;
> > --
>
> > but when useradd fails, I need to stop it from sending the error messag=
e to
> > STDER.
> > Is it possible with system?
>
> > Thanks!
>
> For an easy and cross-platform solution you can use Capture::Tiny to capt=
ure both STDERR and STDOUT separately and then have your perl code decide w=
hat to do with each.
>

Neat solution. (I wonder why the name 'Tiny' rather than...
say, IPC::Capture for instance..)

Another possibility would IPC::Run :

use IPC::Run qw( run timeout );

run \@cmd, \$in, \$out, \$err, timeout( 10 )
or die "cmd err: $?";

--
Charles DeRykus




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/