Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Automating execution of shell scripts

by BillKSmith (Monsignor)
on Aug 30, 2021 at 14:44 UTC ( #11136213=note: print w/replies, xml ) Need Help??

in reply to Automating execution of shell scripts

As I understand your problem, you have two shell scripts which you must use "as is". You want a perl program to automate this procedure. For each argument, run script1 which computes a directory name and creates that directory, then run script2 in that directory. Your question is how can your perl program get the name of directory it needs to run script2.

You tell us that script1 " prints in the terminal the path to the created dir." You should be able to capture that output if you run script1 with backticks. my $script_output = `script1`'. You can change the working directory with the function chdir. You may need the module Cwd to get the directory you want to return to.


Replies are listed 'Best First'.
Re^2: Automating execution of shell scripts
by wisely (Initiate) on Sep 02, 2021 at 11:30 UTC
    Hi Bill,

    Yes, that sums up the problem well :).
    I was not sure how can I get the info that the first script prints out and I wasn't sure if it was a feature.
    So I didn't consider it and went with checking the dates of the folders, which is not as good of an option.
    This sounds like the solution I am searching for I will try to implement it.
    Thank you very much for the help.

Re^2: Automating execution of shell scripts
by wisely (Initiate) on Sep 03, 2021 at 11:52 UTC
    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?
      $text = @_;

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

      my ($text) = @_;


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

      is better written as

      if (/user_name/) {

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Your code as supplied does not compile:

      $ perl -cw Missing right curly or square bracket at line 11, at end o +f line syntax error at line 11, at EOF 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.


      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)

        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2022-01-18 11:44 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (53 votes). Check out past polls.