Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

perl scripting help

by ejm20 (Initiate)
on May 28, 2021 at 15:06 UTC ( #11133222=perlquestion: print w/replies, xml ) Need Help??

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

ve been asked to create a perl script in bash that will open an xterm or terminal, cd to a specific directory, the load modules. I have very little perl experience and have been searching the web and books with no luck. I can get it to open the shell with the env/paths but cant execute any commands like a simple "cd". Heres what I got:

#!/usr/bin/perl system ('gnome-terminal', 'sh', -c 'cd /home/$USER/projects; module lo +ad project/test' );

Thanks!!!!

Code and p tags added by GrandFather

Replies are listed 'Best First'.
Re: perl scripting help
by kcott (Bishop) on May 29, 2021 at 02:02 UTC

    G'day ejm20,

    Welcome to the Monastery.

    I'm not entirely sure what your ultimate goal is here. You wrote "create a perl script in bash" which I find confusing. It seems to me that you could put your bash commands in a bash script and run that from an xterm (or equivalent).

    Having said that, and based on the code you've managed so far, perhaps you're after something like this:

    #!/usr/bin/env perl use strict; use warnings; use autodie ':all'; my @commands = ( 'set -vx', "cd $ENV{HOME}", 'date', 'pwd', '/usr/bin/bash -i', ); system qw{/usr/bin/xterm -e}, join ';', @commands;

    When I run that, it pops up an xterm window containing:

    + cd /home/ken + date Sat, 29 May 2021 10:44:54 + pwd /home/ken + /usr/bin/bash -i ken@titan ~ $

    I can now type more commands into that xterm window. It shuts down normally with exit or Ctrl-D.

    One thing to note is that the Perl script (which I called pm_11133222_xterm.pl) will block until the xterm window closes. If that's an issue, you can run it in the background.

    This starts the xterm and returns my normal prompt:

    ken@titan ~/tmp $ ./pm_11133222_xterm.pl & [1] 2182 ken@titan ~/tmp $

    After closing the xterm and hitting enter:

    ken@titan ~/tmp $ [1]+ Done ./pm_11133222_xterm.pl ken@titan ~/tmp $

    You may, of course, already know how to do that.

    I didn't think I had gnome-terminal but apparently I do. I'm running Cygwin 3.2.0 (on Win10) so I no doubt got it via some package. I don't have a manpage for it; I found gnome-terminal(1); it says "gnome-terminal is designed to emulate the xterm program ..." but simply replacing xterm with gnome-terminal resulted in various warning and error messages. I got it to run by replacing the last line of script above with:

    system qw{/usr/bin/gnome-terminal -- /usr/bin/bash -c}, join ';', @com +mands;

    This now popped up a window with the same initial content as the xterm window described above; subsequent interaction also worked the same. There were, however, two differences: the Perl script did not block; and, I got this warning:

    ** (gnome-terminal:2292): WARNING **: Error retrieving accessibility b +us address: org.freedesktop.DBus.Error.NoReply: Message recipient dis +connected from message bus without replying

    An internet search for that message got quite a few hits. Here's a couple of workarounds to remove the warning:

    $ NO_AT_BRIDGE=1 ./pm_11133222_gnome.pl $ ./pm_11133222_gnome.pl 2> /dev/null

    As I don't use gnome-terminal — fairly obviously as I only just found that I had it — I wasn't particularly interested in delving into this further. If you encounter the same problem and identify a genuine fix (as opposed to a workaround) please share as it may be useful to others in the future.

    — Ken

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: perl scripting help
by hippo (Bishop) on May 28, 2021 at 15:24 UTC

    I don't use gnome-terminal but this works for me with xterm. It should get you started, anyway.

    #!/usr/bin/env perl use strict; use warnings; system ('xterm', '-e', 'ls && sleep 10');

    🦛

      Hmm, if OP wants to have an interactive terminal that just executes some commands before dropping to the command line, something like this should work:

      system('xterm', '-e', 'bash -rcfile /home/username/script.sh');

      perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'
      The xterm does open but closes after the sleep command. How can it stay open? Thanks!!!

        The same way you would without using Perl at all. eg:

        system ('xterm', '-e', 'ls && read line');

        will happily leave the xterm open indefinitely until the user closes it (or submits a line).


        🦛

Re: perl scripting help
by navalned (Beadle) on Jun 02, 2021 at 01:56 UTC

    Taking it a little further and daemonizing, etc...

    #!/usr/bin/env perl use common::sense; use POSIX qw//; defined (my $pid = fork()) or die "Can't fork: $!"; exit if $pid; chdir "$ENV{HOME}" or die "Can't chdir $ENV{HOME}"; POSIX::setsid or die "setsid: $!"; close STDOUT; close STDIN; close STDERR; open STDIN, '<', "/dev/null" or die "open: $!"; open STDOUT, '>', "/dev/null" or die "open: $!"; open STDERR, '>', "/dev/null" or die "open: $!"; defined ($pid = fork()) or die "Can't fork: $!"; if ($pid) { waitpid $pid, 0; } else { eval { my @cmd = ( '/usr/X11R6/bin/xterm', '-e', '/bin/sh -c "find . | xargs egrep "perl""', #simulate module loading ); exec @cmd; }; warn $@ if $@; exit 1; }
Re: perl scripting help
by perlfan (Vicar) on Jun 02, 2021 at 23:51 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11133222]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-05-24 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?