Re: shell commands return values

From: John W. Krahn (krahnj_at_acm.org)
Date: 07/07/04


Date: Wed, 07 Jul 2004 21:44:46 GMT

dushkin@012.net.il wrote:
>
> Hello,

Hello,

> I am programming perl scripts on unix.

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

> I want to create and
> manipulate directories.

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'

> 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?

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