Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: wrapping bash and astronomy

by bliako (Monsignor)
on Feb 28, 2023 at 19:10 UTC ( [id://11150654]=note: print w/replies, xml ) Need Help??


in reply to wrapping bash and astronomy

#my $s1 = capturex 'set -eu'; # "set -eu" failed to start: "No such file or directory"

The usage is systemx("some_command",@args);. So: my $s1 = capturex 'set', '-eu'; The difference being that command and subsequent arguments must all be passed individually and not in one single string. The way you are calling it, it thinks that the whole string is one single command (and it complains it can't find it).

HOWEVER! I assume that capturex, systemx and friends spawn a system command directly or via an ephemeral shell which ends as soon as the shell command ends. This means that a sequence of capturex calls will use completely different shells, which will not share any state. So, setting shell options via set -eu on one capturex call will not be preserved on the next capturex call. If you want to preserve state in a shell and execute many shell commands in it there is a simple way: first create a temporary shell script (see tempfile of File::Temp) within your perl-script to contain all the commands you want to run. And then execute this shell script via a single capturex call.

Edit: Data::Dumper allows hiding the $VAR1 = section of its output if you set $Data::Dumper::Terse = 0; before first call. Similarly, perl2dump of Data::Roundtrip also allows terse output via perl2dump($vartodump, {'terse'=>1}); So you will have less text to filter out.

Aldebaran++ I have to commend your efforts not only in using Perl but also in using Linux. It's a steep learning curve but it hides a ... springboard at the end of it. Plus the methodical approach you take in achieving your aims.

bw, bliako

Replies are listed 'Best First'.
Re^2: wrapping bash and astronomy
by Aldebaran (Curate) on Mar 02, 2023 at 10:12 UTC
    The usage is systemx("some_command",@args);. So: my $s1 = capturex 'set', '-eu'; The difference being that command and subsequent arguments must all be passed individually and not in one single string. The way you are calling it, it thinks that the whole string is one single command (and it complains it can't find it).

    Ok, so it's clear now that have to tell it to find bash:

    my $capture = capturex "bash","2.create.bash", "@ARGV"; $logger->info("capx: $capture");

    I got a strong result with wrapping 2.create.bash, which I think is a nifty little utility for my nomenclature scheme.

    fritz@laptop:~/Documents/gitlab1$ ./4.wrap.pl 2.millcreek.pl Time is Thu Mar 2 01:43:55 2023 Julian day is 2460005.86383102 ./4.wrap.pl capx: The shebang is specifying bash Using bash 5.0.17(1)-release 2 3 3.millcreek.pl -rwxrwxr-x 1 fritz fritz 11K Mar 2 01:43 3.millcreek.pl fritz@laptop:~/Documents/gitlab1$
    HOWEVER! I assume that capturex, systemx and friends spawn a system command directly or via an ephemeral shell which ends as soon as the shell command ends. This means that a sequence of capturex calls will use completely different shells, which will not share any state. So, setting shell options via set -eu on one capturex call will not be preserved on the next capturex call. If you want to preserve state in a shell and execute many shell commands in it there is a simple way: first create a temporary shell script (see tempfile of File::Temp) within your perl-script to contain all the commands you want to run. And then execute this shell script via a single capturex call.

    I'm trying to follow, and I have a script to that end:

    #!/usr/bin/perl use v5.030; # strictness implied use warnings; use Path::Tiny; my ($path) = @ARGV; if (not defined $path) { die "Need path in\n"; } my $file_in = path("$path"); my @lines = $file_in->lines_utf8; my @matching; for my $line (@lines){ if ( $line =~ /^#*$/){ say "line matched $line"; next; } else { push( @matching, $line ); } } say @matching; say "cardinality: ", scalar @matching; my $target_dir = path('/tmp'); my $tempfile = $target_dir->tempfile('foobarXXXXXX'); $tempfile->spew("@matching"); # not atomic __END__

    Output. I can't figure out why the shebang and comments endure:

    fritz@laptop:~/Documents/gitlab1$ ./1.wrap.pl 3.git.sh line matched #!/bin/bash pwd >2.txt #echo "starting fresh with rm -rf .git" #rm -rf .git | tee 1.txt #git init #ls >README.md git add *.pl git add *.sh git add *.txt #git remote add origin git@gitlab.com:perlmonks/$1.git git commit -m 'next revision' | tee 2.txt git push -uf origin master | tee 2.txt #git push -uf master main | tee 2.txt cardinality: 13 fritz@laptop:~/Documents/gitlab1$

    Question: How is my regex not sieving out the comments? Do you see what I'm trying to pull off here?

    Aldebaran++ I have to commend your efforts not only in using Perl but also in using Linux. It's a steep learning curve but it hides a ... springboard at the end of it. Plus the methodical approach you take in achieving your aims.

    Woo-hoo, a springboard! If the board is at the height of Skylla, and extends to the center of Charybdis, how long does one fall until impact?

    Cheers,

      G'day Aldebaran,

      " can't figure out why the shebang and comments endure: ... How is my regex not sieving out the comments?"

      Your regex, /^#*$/, is anchored at the start (^) and end ($). Consider:

      $ perl -E ' my $x = "#shebang\n\nstatement1\n#comment\nstatement2"; my @lines = split /\n/, $x; say "All lines:"; say for @lines; say "-" x 40; say "Your regex:"; say for grep ! /^#*$/, @lines; say "-" x 40; say "Better regex:"; say for grep ! /^#/, @lines; say "-" x 40; ' All lines: #shebang statement1 #comment statement2 ---------------------------------------- Your regex: #shebang statement1 #comment statement2 ---------------------------------------- Better regex: statement1 statement2 ----------------------------------------

      Note how your regex is removing blank lines. Did you want that?

      Addendum: If the answer to that last question is yes, you can use /^(?:#|$)/.

      — Ken

        Thx, kcott,I can hardly believe this thing works, but it almost does:

        fritz@laptop:~/Documents/gitlab1$ ./1.wrap.pl 3.git.sh Time is Tue Mar 7 22:43:27 2023 Julian day is 2460011.73850694 ./1.wrap.pl line matched #!/bin/bash line matched #echo "starting fresh with rm -rf .git" line matched #rm -rf .git | tee 1.txt line matched #git init line matched #ls >README.md line matched #git remote add origin git@gitlab.com:perlmonks/$1.git line matched #git push -uf master main | tee 2.txt line matched pwd >2.txt git add *.pl git add *.sh git add *.txt git commit -m 'next revision' | tee 2.txt git push -uf origin master | tee 2.txt cardinality: 6 Enumerating objects: 13, done. Counting objects: 100% (13/13), done. Delta compression using up to 4 threads Compressing objects: 100% (12/12), done. Writing objects: 100% (12/12), 5.47 KiB | 224.00 KiB/s, done. Total 12 (delta 3), reused 0 (delta 0) remote: remote: To create a merge request for master, visit: remote: https://gitlab.com/perlmonks/betelgeuse/-/merge_requests/new +?merge_request%5Bsource_branch%5D=master remote: To gitlab.com:perlmonks/betelgeuse.git 092deaf..5000f52 master -> master capx: [master 5000f52] next revision 11 files changed, 963 insertions(+) create mode 100644 1.aldeb.txt create mode 100755 1.wrap.pl create mode 100755 2.2023.pl create mode 100755 2.create.bash create mode 100755 2.wrap.pl create mode 100755 3.2023.pl create mode 100755 3.millcreek.pl create mode 100644 3.output.txt create mode 100644 3.wrap.pl create mode 100755 4.2023.pl create mode 100755 4.wrap.pl Branch 'master' set up to track remote branch 'master' from 'origin'. fritz@laptop:~/Documents/gitlab1$

        Source:

        #!/usr/bin/perl use v5.030; # strictness implied use warnings; use Path::Tiny; use Time::Piece; use Log::Log4perl; use IPC::System::Simple qw/systemx capturex/; use utf8; my ($argv1) = @ARGV; if (not defined $argv1) { die "Need argv1\n"; } my $t = localtime; my $jd = $t->julian_day; my $log_conf4 = '/home/fritz/Documents/perlmonks/conf_files/4.conf'; Log::Log4perl::init($log_conf4); #info my $logger = Log::Log4perl->get_logger(); $logger->info("Time is $t"); $logger->info("Julian day is $jd"); $logger->info("$0"); my ($path) = @ARGV; if (not defined $path) { die "Need path in\n"; } my $file_in = path("$path"); my @lines = $file_in->lines_utf8; my @commands; # for my $line (@lines){ if ( $line =~ /^(?:#|$)/){ say "line matched $line"; next; } else { push( @commands, $line ); } } say @commands; say "cardinality: ", scalar @commands; #my $capture = capturex "bash", @commands; #$logger->info("capx: $capture"); my $target_dir = path('/tmp'); my $tempfile = $target_dir->tempfile('foobarXXXXXX'); $tempfile->spew("@commands"); # not atomic my $capture = capturex "bash", $tempfile; $logger->info("capx: $capture"); __END__

        What doesn't work? I created a merge request, but it won't merge.

        Looking at the log, I'm amazed at what came through capturex:

        2023/03/07 22:43:27 INFO Time is Tue Mar 7 22:43:27 2023 2023/03/07 22:43:27 INFO Julian day is 2460011.73850694 2023/03/07 22:43:27 INFO ./1.wrap.pl 2023/03/07 22:43:30 INFO capx: [master 5000f52] next revision 11 files changed, 963 insertions(+) create mode 100644 1.aldeb.txt create mode 100755 1.wrap.pl create mode 100755 2.2023.pl create mode 100755 2.create.bash create mode 100755 2.wrap.pl create mode 100755 3.2023.pl create mode 100755 3.millcreek.pl create mode 100644 3.output.txt create mode 100644 3.wrap.pl create mode 100755 4.2023.pl create mode 100755 4.wrap.pl Branch 'master' set up to track remote branch 'master' from 'origin'.

        I'm still working through Git::Wrapper to abrogate this unusual approach, thx bliako++ for the tempfile idea.

        Cheers from a land of snow,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found