Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Automating execution of shell scripts

by wisely (Initiate)
on Sep 03, 2021 at 07:52 UTC ( #11136410=note: print w/replies, xml ) Need Help??


in reply to Re: Automating execution of shell scripts
in thread Automating execution of shell scripts

It appears I am missing something about the way this works as I am not able to implement it.
I added the backticks and it appears to correctly capture the stdout, I tried the following:
$script2 = `script1 arg1 arg2 arg3 arg4`; $working_dir = &getWorkDir($script2); sub getWorkDir { $text = @_; while($text) { if ($_ =~ ".*user_name.*"){ my $dir = $_; } return $dir; }
This appears to never enter the if statement even.
I also tried using @script2 to store the output and then used @text in the sub procedure and then loop through it using foreach.
That way actually managed to enter the if statement once the line containing the directory was reached but still $dir was left empty.
I don't understand why the $dir does not get assigned a value. When I check in the debugger once I enter the if statement the $_ actually contains the line but I still can't assign it to $dir and return it.
What am I missing here?

Replies are listed 'Best First'.
Re^3: Automating execution of shell scripts
by choroba (Archbishop) on Sep 03, 2021 at 09:01 UTC
    $text = @_;

    This evaluates @_ in scalar context, returning the number of parameters instead of the actual value. Instead, use

    my ($text) = @_;

    Also,

    if ($_ =~ ".*user_name.*"){

    is better written as

    if (/user_name/) {

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: Automating execution of shell scripts
by hippo (Bishop) on Sep 03, 2021 at 09:07 UTC

    Your code as supplied does not compile:

    $ perl -cw 11136410.pl Missing right curly or square bracket at 11136410.pl line 11, at end o +f line syntax error at 11136410.pl line 11, at EOF 11136410.pl had compilation errors.

    Other problems: you are not using strict, your scope of $dir is incorrect, there is no assignment to $_, you have scalar context when assigning the arguments in your subroutine and your while loop will never end.

    Spend a little time trying to fix these issues and post back here if you get stuck.


    🦛

Re^3: Automating execution of shell scripts
by BillKSmith (Monsignor) on Sep 03, 2021 at 21:42 UTC
    There are so many errors in your subroutine that we are not sure what you are trying to do. I recommend that you study the documentation perlsub. I suspect that you are confusing us (and yourself) with a poor choice of variable names ($script1 for the name of a script file and $script2 for its output???). Given the code you have shown, $_ should not contain anything. You probably do not need a subroutine if all it does is extract the directory name from the script output. Use a regex with capture parenthesis in your main program.

    We can provide much better help if you post a short program that we can run and duplicate your problem. Use use strict; use warnings and fix all errors and warnings that they cause. (Read the tutorial Short, Self-Contained, Correct Example)

    Bill
      Sorry I forgot to mention this was only a snippet of the whole program, and it wasn't exactly accurate to what I had in actual program.
      I just wanted to illustrate what the direction in which I was headed for solving this.
      Sorry also that I couldn't just paste the whole code.
      Anyway in my actual program it turned out that the issue was the scope of the $dir variable in my subroutine as far as I can tell.
      The program works correctly now.
      I guess it would be better (also faster?) if it is done in the main program and not in a subroutine, but it was easier for me to follow what I am doing by writing subroutines. I am also not sure how to do what you mention.
        I guess it would be better (also faster?) if it is done in the main program and not in a subroutine ...

        A highly questionable guess IMHO. Reasonable factoring of code into subroutines certainly has the benefits of increased clarity and maintainability, encapsulation, etc., as you mention. The only speed advantage of inlining might be in a case in which you are calling a relatively simple function a gazillion times in a program. In the typical case, Perl call-return overhead vanishes into the background of a script.

        (And yes, please see Short, Self-Contained, Correct Example.)


        Give a man a fish:  <%-{-{-{-<

        We almost never want your complete program. The code that you extract should be made into a complete program which includes enough data that we can run it and duplicate your problem. This can be harder than it sounds. Often this is exactly what you need to solve the problem yourself. I suspect that this is what happened to you.
        Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2022-05-21 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?