Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

perl open shell and linux commands

190 views
Skip to first unread message

ej...@pacbell.net

unread,
May 27, 2021, 5:24:17 PM5/27/21
to
Ive 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 load project/test' );

Thanks!!!!

Kenny McCormack

unread,
May 27, 2021, 8:45:18 PM5/27/21
to
In article <fafb1b32-faaa-49ad...@googlegroups.com>,
ej...@pacbell.net <ej...@pacbell.net> wrote:
>Ive been asked to create a perl script in bash that will open an xterm or

Isn't the concept of a "perl script in bash" pretty much about like
saying a "C program in Fortran" ?

--
The people who were, are, and always will be, wrong about everything, are still
calling *us* "libtards"...

(John Fugelsang)

Allodoxaphobia

unread,
May 28, 2021, 8:35:06 AM5/28/21
to
On Fri, 28 May 2021 00:45:13 -0000 (UTC), Kenny McCormack wrote:
> In article <fafb1b32-faaa-49ad...@googlegroups.com>,
> ej...@pacbell.net <ej...@pacbell.net> wrote:
>
>>Ive been asked to create a perl script in bash that will open an xterm
>
> Isn't the concept of a "perl script in bash" pretty much about like
> saying a "C program in Fortran" ?

Indeed!

ej...@pacbell.net

unread,
May 28, 2021, 11:05:01 AM5/28/21
to
I agree but thats what Im stuck with! Any ideas about the script?

Janis Papanagnou

unread,
May 28, 2021, 11:24:13 AM5/28/21
to
On 28.05.2021 17:04, ej...@pacbell.net wrote:
> I agree but thats what Im stuck with! Any ideas about the script?

Any ideas about the intention (or context)?

Any ideas about what the one who asked you meant?

(Maybe the folks in a perl newsgroup are more familiar with
such a question or can make some sense of that question?)

But yes, I have ideas about such a script. One would be to
just do it without any additional, spurious, or intermediate
language.

Janis

Javier

unread,
May 28, 2021, 11:27:30 AM5/28/21
to
It's not very clear what you want to do exactly, but all languages
have a feature to execute commands at interpreter startup befaure it
goes into interactive. In bash it's done with --rcfile (the -c option
in your post will not go into interactive)

#!/usr/bin/perl
system ('gnome-terminal','--', 'bash --rcfile <(echo "cd /home/$USER/projects;
module load project/test;" )');

Note that I use bash instead of sh to use the bash feature of process
substitution <(...) to avoiding creating an extra file. I'm almost
sure it will work, because if your system has gnome-terminal
installed, there is a >99% probability that bash is also there.

PS: what you want to do is a bad praxis because it hides the
environment setup from the users and they will not be able to replicate
the environment setup should they want to do the same thing in their
own scripts.

Christian Weisgerber

unread,
May 28, 2021, 11:30:09 AM5/28/21
to
On 2021-05-28, Kenny McCormack <gaz...@shell.xmission.com> wrote:

>>Ive been asked to create a perl script in bash that will open an xterm or
>
> Isn't the concept of a "perl script in bash" pretty much about like
> saying a "C program in Fortran" ?

A Perl script embedded in a bash wrapper?

#!/usr/local/bin/bash
perl -e 'print "hello world\n"'

But yes, presumably the original poster is too confused to ask a
coherent question.

--
Christian "naddy" Weisgerber na...@mips.inka.de

Allodoxaphobia

unread,
May 28, 2021, 11:42:42 AM5/28/21
to
On Fri, 28 May 2021 14:41:03 -0000 (UTC), Christian Weisgerber wrote:
> On 2021-05-28, Kenny McCormack <gaz...@shell.xmission.com> wrote:
>
>>>Ive been asked to create a perl script in bash that will open an xterm or
>>
>> Isn't the concept of a "perl script in bash" pretty much about like
>> saying a "C program in Fortran" ?
>
> A Perl script embedded in a bash wrapper?
>
> #!/usr/local/bin/bash
> perl -e 'print "hello world\n"'
>
> But yes, presumably the original poster is too confused to ask a
> coherent question.

... or, is a student trying to rush a final semester's assignment.

William Unruh

unread,
May 28, 2021, 12:56:02 PM5/28/21
to
On 2021-05-28, ej...@pacbell.net <ej...@pacbell.net> wrote:
> I agree but thats what Im stuck with! Any ideas about the script?

Uh, I think you need to be clear about what you want before you ask for
help. Or is this a homework assignment?

(My interpretation would be a perl script to be run from bash.)

ej...@pacbell.net

unread,
May 28, 2021, 1:57:09 PM5/28/21
to
I originally looked for a perl newgroup but the the last entry was 2017 so thought I'd try here. This is not homework or a final exam. There is a parent GUI that needs some code that will open an xterm or gnome-terminal for user input which must have all of the env set that will be loaded from the users .bashrc . Once the xterm opens, it needs to cd to specific path, thats the "cd" portion. I can get the xterm to open but not cd....thats the requested help.

Janis Papanagnou

unread,
May 28, 2021, 2:12:30 PM5/28/21
to
[ Please stop top-posting! ]

On 28.05.2021 19:57, ej...@pacbell.net wrote:
> I originally looked for a perl newgroup but the the last entry was
> 2017 so thought I'd try here. This is not homework or a final exam.
> There is a parent GUI that needs some code that will open an xterm or
> gnome-terminal for user input which must have all of the env set
> that will be loaded from the users .bashrc . Once the xterm opens,
> it needs to cd to specific path, thats the "cd" portion. I can get
> the xterm to open but not cd....thats the requested help.

Are you sure the syntax you posted

system ('gnome-terminal', 'sh', -c 'cd /home/$USER/projects; module load
project/test' );

is correct? - You should inspect the perl docs! - At least all the
quoted arguments to system() with the interspersed unquoted -c that
isn't even separated by a comma looks strange (i.e. to me, someone
who is unfamiliar with perl).

Also the call of 'module' after the shell command 'cd' looks strange;
it doesn't look like a shell command.

Janis

Javier

unread,
May 28, 2021, 2:40:18 PM5/28/21
to
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>
> Also the call of 'module' after the shell command 'cd' looks strange;
> it doesn't look like a shell command.
>

module is actually a shell function to change the environment.

It's used to change the environment variables in the shell: PATH,
LD_LIBRARY_PATH,etc by using a shell function. It's very popular in
computing clusters to select the versions of compilers or libraries
from the shell. Scientific software is very sensitive to the versions
of its dependencies.

The module files are implemented in tcl or lua.

http://www.softpanorama.org/HPC/Environment_modules/index.shtml

If it is installed into your system you can look at module(1), modulefiles(4).

Javier

unread,
May 28, 2021, 2:46:32 PM5/28/21
to
Javier <inv...@invalid.invalid> wrote:
>
> #!/usr/bin/perl
> system ('gnome-terminal','--', 'bash --rcfile <(echo "cd /home/$USER/projects;
> module load project/test;" )');
>

Since the OP asks in the other subthread about xterm, this is the xterm version.
Just tested and works fine for me.

#!/usr/bin/perl
system ('xterm','-e', 'bash --rcfile <(echo "cd /home/$USER;

Ben Bacarisse

unread,
May 28, 2021, 2:54:10 PM5/28/21
to
Janis Papanagnou <janis_pa...@hotmail.com> writes:

> [ Please stop top-posting! ]
>
> On 28.05.2021 19:57, ej...@pacbell.net wrote:
>> I originally looked for a perl newgroup but the the last entry was
>> 2017 so thought I'd try here. This is not homework or a final exam.
>> There is a parent GUI that needs some code that will open an xterm or
>> gnome-terminal for user input which must have all of the env set
>> that will be loaded from the users .bashrc . Once the xterm opens,
>> it needs to cd to specific path, thats the "cd" portion. I can get
>> the xterm to open but not cd....thats the requested help.
>
> Are you sure the syntax you posted
>
> system ('gnome-terminal', 'sh', -c 'cd /home/$USER/projects; module load
> project/test' );
>
> is correct?

The syntax is OK (Perl syntax is odd) but...

> - You should inspect the perl docs! - At least all the
> quoted arguments to system() with the interspersed unquoted -c that
> isn't even separated by a comma looks strange

... -c is a unary operator that tests its operand to see if it is a
character special file. Since there is probably no character special
file called 'cd /home/$USER/projects; module load project/test' the
third parameter to system is false.

> Also the call of 'module' after the shell command 'cd' looks strange;
> it doesn't look like a shell command.

Yes. I think this is a genuine case for asking what the OP is trying to
do. Opening an interactive terminal to run a specific command does not
seem like the right way to do anything.

--
Ben.

ej...@pacbell.net

unread,
May 28, 2021, 2:57:20 PM5/28/21
to
the xterm example does open an xterm and cd's but does not load the .bashrc.


ej...@pacbell.net

unread,
May 28, 2021, 3:04:05 PM5/28/21
to
Heres the sequence of events
open a terminal
cd to a specific path
load various modules <- for this happen, the paths needs to be set by reading the bashrc ( just like opening a terminal )
this terminal is available for user input

Javier

unread,
May 28, 2021, 3:24:15 PM5/28/21
to
ej...@pacbell.net <ej...@pacbell.net> wrote:
> the xterm example does open an xterm and cd's but does not load the .bashrc.

Yes, when using the --rcfile option you need to include the bashrc
sourcing explicitely in your own rc file.

#!/usr/bin/perl
system ('xterm','-e', 'bash --rcfile <(echo ". cd /home/$USER;
. /home/$USER/.bashrc;

Janis Papanagnou

unread,
May 28, 2021, 3:26:20 PM5/28/21
to
On 28.05.2021 21:04, ej...@pacbell.net wrote:
>
> Heres the sequence of events
> open a terminal
> cd to a specific path
> load various modules <- for this happen, the paths needs to be set by reading the bashrc ( just like opening a terminal )
> this terminal is available for user input

What's wrong about putting all the stuff (cd, source rc-file, load
module, AND a final bash call) in a shell executable file and start
that file from the 'terminal -e' call. If the environment variables
are exported they should be available in the interactive shell then.
(Not that I'd think that it is a good idea to handle that through
an implicitly launched terminal application.)

Janis

ej...@pacbell.net

unread,
May 28, 2021, 3:38:44 PM5/28/21
to
it would certainly be easier but the parent GUI is in perl and thats the request.

Janis Papanagnou

unread,
May 28, 2021, 3:46:46 PM5/28/21
to
On 28.05.2021 21:38, ej...@pacbell.net wrote:
> On Friday, May 28, 2021 at 12:26:20 PM UTC-7, Janis Papanagnou wrote:
>> On 28.05.2021 21:04, ej...@pacbell.net wrote:
>>>
>>> Heres the sequence of events
>>> open a terminal
>>> cd to a specific path
>>> load various modules <- for this happen, the paths needs to be set by reading the bashrc ( just like opening a terminal )
>>> this terminal is available for user input
>> What's wrong about putting all the stuff (cd, source rc-file, load
>> module, AND a final bash call) in a shell executable file and start
>> that file from the 'terminal -e' call. If the environment variables
>> are exported they should be available in the interactive shell then.
>> (Not that I'd think that it is a good idea to handle that through
>> an implicitly launched terminal application.)
>
> it would certainly be easier but the parent GUI is in perl and thats the request.

And how about just call the shell script from the perl module?
Instead of
bash -> terminal -> shell-script: cd/source/load/bash
just
perl-script -> terminal -> shell-script: cd/source/load/bash
or more precisely (if started from an interactive bash shell)
bash -> perl-script -> terminal -> shell-script: cd/source/load/bash

Janis

Keith Thompson

unread,
May 28, 2021, 5:17:26 PM5/28/21
to
"ej...@pacbell.net" <ej...@pacbell.net> writes:
> On Friday, May 28, 2021 at 12:26:20 PM UTC-7, Janis Papanagnou wrote:
>> On 28.05.2021 21:04, ej...@pacbell.net wrote:
>> >
>> > Heres the sequence of events
>> > open a terminal
>> > cd to a specific path
>> > load various modules <- for this happen, the paths needs to be set by reading the bashrc ( just like opening a terminal )
>> > this terminal is available for user input
>> What's wrong about putting all the stuff (cd, source rc-file, load
>> module, AND a final bash call) in a shell executable file and start
>> that file from the 'terminal -e' call. If the environment variables
>> are exported they should be available in the interactive shell then.
>> (Not that I'd think that it is a good idea to handle that through
>> an implicitly launched terminal application.)
>
> it would certainly be easier but the parent GUI is in perl and thats the request.

Are you saying that you need to write some Perl code that will be
incorporated into an existing Perl script or module? Your original post
showed a standalone Perl script.

My reaction to that was that using Perl adds no value. As a user, if I
invoke a command, I seldom care whether it's a shell script, a Perl
script. or a binary executable. What you're doing can be done more
cleanly in pure shell.

Of course you could write a bash script and have your script invoked
from Perl using system(). Is there some reason that wouldn't meet the
requirement?

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Randal L. Schwartz

unread,
May 29, 2021, 1:23:11 AM5/29/21
to
>>>>> "Janis" == Janis Papanagnou <janis_pa...@hotmail.com> writes:

Janis> (Maybe the folks in a perl newsgroup are more familiar with
Janis> such a question or can make some sense of that question?)

Nope. We're lost as well. And it looks like a cd inside backticks,
which really isn't gonna do much. (That makes it relevant again here.)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Dart/Flutter consulting, Technical writing, Comedy, etc. etc.
Still trying to think of something clever for the fourth line of this .sig

Randal L. Schwartz

unread,
May 29, 2021, 1:23:12 AM5/29/21
to
>>>>> "ej" == ej @pacbell net <ej...@pacbell.net> writes:

ej> I originally looked for a perl newgroup but the the last entry was 2017 so
ej> thought I'd try here.

There are still quite a few Perl support areas, including an entire
collection of mailing lists. "perl.org" lists most of them, as I
recall.

You can always post to Stack Overflow with a Perl tag too.
0 new messages