shell commands return values

shell commands return values

Post by dushki » Tue, 06 Jul 2004 15:47:38


Hello,
I am programming perl scripts on unix. I want to create and
manipulate directories. For this matter, I use shell commands. I used
backtics and then turned to system().
My question is: how can I know if the shell command succeeded? I
know according to system() man page that it returns 127 or -1 if the
command failed. However, there were some places in the script where it
created a directory as needed but system() still returned -1...

What is the correct way of getting and exploring the shell
commands returned values?

Thanks All!
 
 
 

shell commands return values

Post by Igor Pozga » Tue, 06 Jul 2004 17:13:43


Hi. If you are using perl, try this:

system ("ls this_does_not_exist");
$retval = `echo $?`; # $? holds error code
print "Command failed" if ($retval != 0);

 
 
 

shell commands return values

Post by mazzaw » Wed, 07 Jul 2004 01:39:28

you don't have to use system.
do something like this to create a directory. always avoid system()
when you can.

unless ( -e $dir ) {
eval { mkpath($dir) };


if ( $@ ) {
die "Could not make $dir: $@";
}
}

but make sure you trust the scalar $dir, because this will execute it.
 
 
 

shell commands return values

Post by Barry Marg » Wed, 07 Jul 2004 05:52:10

In article < XXXX@XXXXX.COM >,



Why are you using shell commands instead of using the built-in
mechanisms of Perl?

--
Barry Margolin, XXXX@XXXXX.COM
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
 
 
 

shell commands return values

Post by John W. Kr » Fri, 09 Jul 2004 06:44:46


Hello,


You might want to post Perl questions on comp.lang.perl.misc instead or
maybe one of the mailing lists at http://www.yqcomputer.com/


perldoc perlfunc
[snip]
Input and output functions
`binmode', `close', `closedir', `dbmclose', `dbmopen',
`die', `eof', `fileno', `flock', `format', `getc',
`print', `printf', `read', `readdir', `rewinddir',
`seek', `seekdir', `select', `syscall', `sysread',
`sysseek', `syswrite', `tell', `telldir', `truncate',
`warn', `write'
[snip]
Functions for filehandles, files, or directories
`-X', `chdir', `chmod', `chown', `chroot', `fcntl',
`glob', `ioctl', `link', `lstat', `mkdir', `open',
`opendir', `readlink', `rename', `rmdir', `stat',
`symlink', `umask', `unlink', `utime'


perldoc -f system
[snip]
Because `system' and backticks block `SIGINT' and
`SIGQUIT', killing the program they're running
doesn't actually interrupt your program.

@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"

You can check all the failure possibilities by
inspecting `$?' like this:

$exit_value = $? >> 8;
$signal_num = $? & 127;
$dumped_core = $? & 128;

When the arguments get executed via the system
shell, results and return codes will be subject to
its quirks and capabilities. See the section on
"`STRING`" in the perlop manpage and the exec
entry elsewhere in this document for details.


perldoc perlvar
[snip]
$CHILD_ERROR

$? The status returned by the last pipe close, back tick (```') command, successful call to wait() or
waitpid(), or from the system() operator. This is
just the 16-bit status word returned by the wait()
system call (or else is made up to look like it).
Thus, the exit value of the subprocess is really
(`$?>>> 8'), and `$? & 127' gives which signal, if
any, the process died from, and `$? & 128' reports
whether there was a core dump. (Mnemonic: similar
to sh and ksh.)
Additionally, if the `h_errno' variable is sup ported in C, its value is returned via $? if any
`gethost*()' function fails.
If you have installed a signal handler for
`SIGCHLD', the value of `$?' will usually be wrong
outside that handler.


John
--
use Perl;
program
fulfillment