Sponsored Content
Top Forums Shell Programming and Scripting passing "stderr " to a subroutine.. Post 91065 by sekar sundaram on Tuesday 29th of November 2005 07:14:24 PM
Old 11-29-2005
solution

after some search i got the solution.
for information of others i am replying to my post itself..

i think with system command we cant get the stderr separately.
----------------
Why can't I get the output of a command with system()?
You're confusing the purpose of system() and backticks (``). system() runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value). Backticks (``) run a command and return what it sent to STDOUT.
----------------
the solution is:
instead of using the system command we have to use backticks(``).

=============
How can I capture STDERR from an external command?
There are three basic ways of running external commands:

system $cmd; # using system()
$output = `$cmd`; # using backticks (``)
open (PIPE, "cmd |"); # using open()With system(), both STDOUT and STDERR will go the same place as the script's STDOUT and STDERR, unless the system() command redirects them. Backticks and open() read only the STDOUT of your command

=============
system, backticks, exe--- are the three ways to run commands...
-------
i use this line.."i think" since it might be correct or wrong. i am still learning..
-------
moer details :
http://perldoc.perl.org/perlfaq8.html
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Shell Programming and Scripting

Adding custom mesg. when redirecting "exec 2>stderr.err" ?

Doubt regarding using "exec" command to redirect the STDERR to a file. e.g I did it this way. mystage.sh #!/bin/sh exec 2>stage.err .... .... cat stage.err mv: cannot move `/root/stage' to a subdirectory of itself, `/root/stage_old/stage' ls: *.zDB: No such file or... (0 Replies)
Discussion started by: snurani
0 Replies

3. Shell Programming and Scripting

Why stderr file descriptor redirection makes ksh's "select" construct hang.

I am trying to use one global declaration --> "exec 2>$ERR" to capture all stderr outputs that may occur anywhere in my script. Then close it at the end of the script using --> "exec 2<&-" I am using KSH on Solaris 8. KSH Version M-11/16/88i If I comment two "exec .." statements in the... (11 Replies)
Discussion started by: kchinnam
11 Replies

4. Shell Programming and Scripting

tar "--totals" writes to stderr not stdout?

I want to use the "--totals" option in GNU tar for some reporting, however I have discovered that it writes the output to stderr not stdout and I would like to know why. This is running from BASH. mkdir /tmp/test touch /tmp/test/foo.file cd /tmp/ tar --totals -clpzf test.tar.gz test 2>... (2 Replies)
Discussion started by: jelloir
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Redirect stdout/stderr, except e.g. "STRING"

Hi, I'm running a program (Python) whose output I would like to redirect to a log. But the program calls a library (that I cannot change), which outputs all sorts of useless information. I would like to redirect all output from my Python program into this log, except output that matches the... (7 Replies)
Discussion started by: rswindle
7 Replies

7. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

8. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

9. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies
System::Command(3pm)					User Contributed Perl Documentation				      System::Command(3pm)

NAME
System::Command - Object for running system commands SYNOPSIS
use System::Command; # invoke an external command, and return an object $cmd = System::Command->new( @cmd ); # options can be passed as a hashref $cmd = System::Command->new( @cmd, \%option ); # $cmd is basically a hash, with keys / accessors $cmd->stdin(); # filehandle to the process' stdin (write) $cmd->stdout(); # filehandle to the process' stdout (read) $cmd->stderr(); # filehandle to the process' stdout (read) $cmd->pid(); # pid of the child process # find out if the child process died if ( $cmd->is_terminated() ) { # the handles are not closed yet # but $cmd->exit() et al. are available } # done! $cmd->close(); # exit information $cmd->exit(); # exit status $cmd->signal(); # signal $cmd->core(); # core dumped? (boolean) # cut to the chase my ( $pid, $in, $out, $err ) = System::Command->spawn(@cmd); DESCRIPTION
"System::Command" is a class that launches external system commands and return an object representing them, allowing to interact with them through their "STDIN", "STDOUT" and "STDERR" handles. METHODS
"System::Command" supports the following methods: new( @cmd ) Runs an external command using the list in @cmd. If @cmd contains a hash reference, it is taken as an option hash. The recognized keys are: "cwd" The current working directory in which the command will be run. "env" A hashref containing key / values to add to the command environment. If a value is "undef", the variable corresponding to the key will be removed from the environment. "input" A string that is send to the command's standard input, which is then closed. Using the empty string as "input" will close the command's standard input without writing to it. Using "undef" as "input" will not do anything. This behaviour provides a way to modify previous options populated by some other part of the program. On some systems, some commands may close standard input on startup, which will cause a SIGPIPE when trying to write to it. This will raise an exception. If several option hashes are passed to "new()", they will be merged together with individual values being overridden by those (with the same key) from hashes that appear later in the list. The "System::Command" object returned by "new()" has a number of attributes defined (see below). close() Close all pipes to the child process, collects exit status, etc. and defines a number of attributes (see below). is_terminated() Returns a true value if the underlying process was terminated. If the process was indeed terminated, collects exit status, etc. and defines the same attributes as "close()", but does not close all pipes to the child process, spawn( @cmd ) This shortcut method calls "new()" (and so accepts options in the same manner) and directly returns the "pid", "stdin", "stdout" and "stderr" attributes, in that order. Accessors The attributes of a "System::Command" object are also accessible through a number of accessors. The object returned by "new()" will have the following attributes defined: cmdline() Return the command-line actually executed, as a list of strings. options() The merged list of options used to run the command. pid() The PID of the underlying command. stdin() A filehandle opened in write mode to the child process' standard input. stdout() A filehandle opened in read mode to the child process' standard output. stderr() A filehandle opened in read mode to the child process' standard error output. Regarding the handles to the child process, note that in the following code: my $fh = System::Command->new( @cmd )->stdout; $fh is opened and points to the output handle of the child process, while the anonymous "System::Command" object has been destroyed. Once $fh is destroyed, the subprocess will be reaped, thus avoiding zombies. After the call to "close()" or after "is_terminated()" returns true, the following attributes will be defined: exit() The exit status of the underlying command. core() A boolean value indicating if the command dumped core. signal() The signal, if any, that killed the command. CAVEAT EMPTOR
Note that "System::Command" uses "waitpid()" to catch the status information of the child processes it starts. This means that if your code (or any module you "use") does something like the following: local $SIG{CHLD} = 'IGNORE'; # reap child processes "System::Command" will not be able to capture the "exit", "core" and "signal" attributes. It will instead set all of them to the impossible value "-1", and display the warning "Child process already reaped, check for a SIGCHLD handler". To silence this warning (and accept the impossible status information), load "System::Command" with: use System::Command -quiet; It is also possible to more finely control the warning by setting the $System::Command::QUIET variable (the warning is not emitted if the variable is set to a true value). If the subprocess started by "System::Command" has a short life expectancy, and no other child process is expected to die during that time, you could even disable the handler locally (use at your own risks): { local $SIG{CHLD}; my $cmd = System::Command->new(@cmd); ... } AUTHOR
Philippe Bruhat (BooK), "<book at cpan.org>" ACKNOWLEDGEMENTS
Thanks to Alexis Sukrieh who, when he saw the description of "Git::Repository::Command" during my talk at OSDC.fr 2010, asked why it was not an independent module. This module was started by taking out of "Git::Repository::Command" 1.08 the parts that weren't related to Git. BUGS
Please report any bugs or feature requests to "bug-system-command at rt.cpan.org", or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=System-Command <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=System-Command>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SUPPORT
You can find documentation for this module with the perldoc command. perldoc System::Command You can also look for information at: o RT: CPAN's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=System-Command <http://rt.cpan.org/NoAuth/Bugs.html?Dist=System-Command> o AnnoCPAN: Annotated CPAN documentation http://annocpan.org/dist/System-Command <http://annocpan.org/dist/System-Command> o CPAN Ratings http://cpanratings.perl.org/d/System-Command <http://cpanratings.perl.org/d/System-Command> o Search CPAN http://search.cpan.org/dist/System-Command/ <http://search.cpan.org/dist/System-Command/> COPYRIGHT
Copyright 2010-2011 Philippe Bruhat (BooK). LICENSE
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information. perl v5.14.2 2012-04-19 System::Command(3pm)
All times are GMT -4. The time now is 01:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy