hostux has asked for the wisdom of the Perl Monks concerning the following question:

hi guys, can you tell me how to break out of a script and return an error if the command 'adduser bob' returns the error 'adduser: user bob already exists'? Thanks

Replies are listed 'Best First'.
Re: Checking For Errors
by tadman (Prior) on Jul 31, 2002 at 20:17 UTC
    You might want to check the return value of your system call. Normal execution should not return any errors. A failure may be documented.
    if (my $error = system("adduser","foo")) { die "Trouble brewing\n"; }
Re: breaking from a script
by derby (Abbot) on Jul 31, 2002 at 20:21 UTC
    check out system. Normally most unix commands will return a 0 upon success and something else upon failure. You can investigate the return value of system (or $?) shifted 8 places in order to see the exit status of the command ($? >> 8).

    -derby

Re: breaking from a script
by krujos (Curate) on Jul 31, 2002 at 20:17 UTC
    To exit from the script you can use  exit $errorcode;
    I would use a reg ex to match that string i guess. Or check the error level of the adduser command (which is probably a better option for what your trying to do). Do this using perls built in system call.If you try to add a user that already exists it probably wont exit with 0.
    if (!system("adduser bob")) { exit 0; } else { exit 122 }
      hi again, excuse me for my complete stupidity, but how can i integrate that into my script? where would i print the error message in this code?
        if (!system("adduser bob")) { print "system returned 0 everything is ok"; exit 0; } else { print "there was an error, exit and return 127 to the os"; exit 122; }