http://qs1969.pair.com?node_id=444222

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

I am prompting the user for some information, once they enter that info my script uses that that information to locate a file via ls in a directory via a rsh command. If the file does not exist the system responds with a "No match." and my perl script continues on as if everything is ok. I know I need to set up an if statement, but am uncertain what I need to check for....I am new to perl and have hunted on the web, but to no avail....can you all help? Thanks, T

2005-04-02 Edited by Arunbear: Changed title from 'perl question', as per Monastery guidelines

Replies are listed 'Best First'.
Re: invoking rsh and testing for success/failure
by ikegami (Patriarch) on Apr 01, 2005 at 15:53 UTC
    if (`rsh ...` =~ /No match/) { die("File not found.\n"); }

    `` returns the STDOUT of the command as a string instead of displaying it. If you need both STDOUT and STDERR, redirect STDERR to STDOUT (by addiging "2>&1" to the command).

      Thanks a bunch!! I finally figured it out and it works! really appreciate your help though. Have a great weekend! T
Re: invoking rsh and testing for success/failure
by RazorbladeBidet (Friar) on Apr 01, 2005 at 16:55 UTC
    Alternatively, you can check the return code of the execution. (see perlvar)
    my $cmd = "rsh $file"; # or whatever my $output = qx/$cmd/; # qx// is the same as `` my $rc = $?; # $? is a special Perl variable if ( $rc ) { print STDERR "No Match\n"; # you just want to print and continue, ri +ght? }
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."