Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Heredoc with system call

by Tanktalus (Canon)
on Dec 18, 2008 at 04:54 UTC ( [id://731158]=note: print w/replies, xml ) Need Help??


in reply to Heredoc with system call

While I'm glad you've answered your own question, I have to admit that I'm confused by it. I would expect that the "set"s and the "status" are commands that lsnrctl should receive, and not be separate commands. In that case, I would have expected that they needed to be sent to lsnrctl's STDIN. The easiest way to do that would be:

system(<<EOF) and die "couldn't run listener control: $?"; echo "set password 'oracle' set current_listener 'LISTENER' status" | lsnrctl EOF
though that's definitely not the most perlish (and probably won't quite work as-is on Windows). Something more like:
open my $fh, "|-", "lsnrctl" or die "can't run listener control: $?"; print $fh <<EOF; set password 'oracle' set current_listener 'LISTENER' status EOF close $fh;
is probably closer, and far more perlish.

Replies are listed 'Best First'.
Re^2: Heredoc with system call
by curtisb (Monk) on Dec 18, 2008 at 05:18 UTC
    Your open $fh approach worked. Will this same solution work on UNIX as well as windows?

    Bobby

      It will work at least as well, possibly better on unix.
Re^2: Heredoc with system call
by Anonymous Monk on Apr 22, 2019 at 09:15 UTC
    Sorry for opening an old thread but I arrived to this one as one of my requirements is to also pass arguments to the command as $0 and $1. Can you please suggest how to append this code that it also works with arguments ?
    echo "$var1 and $var2 can be read here"; open my $fh, "|-", "sudo su - APP" or die "can't run sudo: $ +?"; print $fh <<' _EOF_'; ls echo "$var1 and $var2 can't be found here ? :( " _EOF_ close $fh;

    2019-04-23 Athanasius added code tags

      a quick hack is to remove the single quotes from the heredoc marker. In this way the heredoc contents will be processed by perl and any variables within it will be interpolated (and if you do not have use strict; use warnings; all perl-looking variables - declared in your program or not - will be replaced). However, better than a heredoc is to construct a string with your specifications under your full control, using for example, sprintf(), escaping sigils and single quotes. For example, my $var1 = ...; my $str = "echo '$var1' | awk '{print \$1}'";

      See also, Re^2: Ubuntu File Names with spaces and special characters

        Thanks for the suggestion but I am not very familiar with Perl so not really able to comprehend your answer and resolve my case. Basically I have to run a "cp $source $dest" command after logging-in as a different user(ABU) using "sudo su - ABU". The $source and $dest is something that I formulate in my perl script above but I am unable to pass it to the shell command I fork. Code(working snippet): ----
        print "I want to copy $source to $dest after logging-in as ABU"; open my $fh, "|-", "sudo su - ABU" or die "can't run sudo: $?"; print $fh <<_EOF_; echo 'Logged-in as ABU' ls _EOF_ close $fh;
        The only problem is to run "cp $source $dest" on the next line soon after the "ls" command. But I am not sure how to pass these arguments ?
      On bash like systems xargs is supposed to transform STDIN to positionals args

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://731158]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found