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

I want to learn how to execute the following line in Perl:

`rcp infostag:"$DIR_CCIFAS/sys_snapshot_infostag" \
"$DIR_CCIFAS/sys_snapshot_infostag"` ;

Have tried many times and no luck

Ken

Replies are listed 'Best First'.
Re: Learn Unix multiline syntax
by derby (Abbot) on Aug 16, 2010 at 16:01 UTC

    Errr .. what have you tried? Seems like

    system( "rcp", "$ENV{DIR_CCIFAS}/sys_snapshot_infostag", "$ENV{DIR_CCIFAS}/sys_snapshot_infostag" );
    would do the trick? Multiline really doesn't come into play.

    -derby
      This command is working fine:
      `rcp "$DTMP/$Filename"_"stub" infostag:/"$TDIR/$Filename"_"stub"` ;

      `rcp "infostag:$DIR_CCIFAS/sys_snapshot_infostag",
      "$DIR_CCIFAS/sys_snapshot_infostag"` ;
      Results: Usage: rcp -p -F -k realm f1 f2; or: rcp -rp f1 ... fn d2
      sh: /ccifas/logs/snapshot_logs/sys_snapshot_infostag: 0403-006 Execute permission denied.

      system(rcp "infostag:$DIR_CCIFAS/sys_snapshot_infostag",
      "$DIR_CCIFAS/sys_snapshot_infostag") ;

      Results: Name "main::rcp" used only once: possible typo at ./kwb line 14.
      Can't exec "*main::rcp": A file or directory in the path name does not exist. at ./kwb line 13.

        Note that derby had
            system( "rcp", "quotedstring", "quotedstring" );
        whereas you have
            system(rcp "quotedstring", "quotedstring");
        which Perl thinks is a call to the Perl function  rcp with a couple of strings as arguments.

Re: Learn Unix multiline syntax
by ikegami (Patriarch) on Aug 16, 2010 at 18:09 UTC

    Just like the shell interprets "\"+newline as a single newline, so does Perl in double-quoted and backtick literals.

    $ perl -le'$_="\ "; print length;' 1

    To pass "\"+newline to the shell, you'll need to escape the backslash.

    $ perl -e'print `echo foo bar` ' sh: line 1: bar: command not found foo $ perl -e'print `echo foo \ bar` ' sh: line 1: bar: command not found foo $ perl -e'print `echo foo \\ bar` ' foo bar
Re: Learn Unix multiline syntax
by locked_user sundialsvc4 (Abbot) on Aug 16, 2010 at 22:15 UTC

    Remember that the “multi-line syntax” of the shell is simply a cue to the shell that it should not execute the current input immediately, but should instead prompt for another line of input.   The shell removes the trailing backslash from each line before processing the concatenated remainder.

    In Perl, you will simply create a string variable containing the entire command that you wish to execute.   This string should not contain “continuation characters.”   If the string needs to contain any special characters, remember that they might need to be properly “escaped.”

    Finally, remember how Perl treats string literals that are enclosed with double quotes differently from those that are enclosed with single quotes.   Double-quoted strings are interpolated, which means that Perl looks for things that appear to be variable-references in such strings, and attempts to replace them with the value of the corresponding variable.

    A good, practical approach is to first simply print the string to STDERR, to see if it is correct.   Stop coding at that point, and make corrections as necessary.   When all is well, proceed to finish the program.

      To clarify:   the shell has its own way of handling “$whatever”:  it substitutes an environment-variable.   But if you are assembling a double-quoted string, and perhaps in other situations also, “Perl gets there first,” and substitutes a Perl variable (or nothing).   There are various ways to avoid this behavior, including the use of \$ (which means, “the literal character, ‘$’”).   Hence, I stand by the admonition to “first, just print it out.”   It is a (very easy!) waste of time to try to figure out why a string is not being interpreted correctly, only to discover (too late!) that the string does not actually contain what you thought it would.

      (Let’s ask the Peanut Gallery here:    Will everyone who has done exactly this, please raise your hands?”   :-D   I rest my case.   (And lower my hand.)