aliased shell commands from perl script

D

Dmitry

Hi All,

I has faced a problem of calling shell aliases from a perl script recently.
Web search gave me almost nothing, so I feel obliged to share my results
with community :) The only way, which made perl script to execute my tcsh
aliases is below. I would very appreciate if anybody, who knows a better
method, posted it here.

Thanks in advance,
Dmitry

Example:
#! /bin/perl -w

# F.e. --- loopik.pl 'set aaa=`pwd`; cd dir1; nfail; cd $aaa; set aaa = ""' 2
# 'nfail' and 'cd' are aliases from ~/.aliases

use strict;
use Cwd;
use Env;

my( $n_args ) = 0;
my( $own_name ) = "";
my( $user_cmd ) = "";
my( $n_sec ) = 0;
my( $ali ) = "";
my( $kb ) = "";

$n_args = @ARGV;
$own_name = $0;

$own_name =~ s#.*/##; # Clear own name from full path info

if ( $n_args < 1 )
{
print( "\nUsage: $own_name <command> [interval in sec]\n" );
print( " Default interval is 3 sec.\n" );
print( " Type any key for stop. \n" );
exit();
}

$user_cmd = $ARGV[0];

if ( $n_args == 1 )
{
$n_sec = 3; # Default interval
}
else
{
$n_sec = $ARGV[1];
}

my( $tmp_fname ) = "KILLME.kill";

open( OUT, ">$tmp_fname" ) || die ( "Can't open file for writing: $!" );
print OUT "source ~/.aliases";
print OUT "\n";
print OUT $user_cmd;
print OUT "\n";
close( OUT );

use Term::ReadKey; # not a standard - should install CPAN ReadKey
my($char);

ReadMode('cbreak');

while ( !defined( $char = ReadKey(-1) ) )
{
system( "tcsh $tmp_fname" ); # execute commands from a file
sleep( $n_sec );
}

ReadMode('normal'); # restore normal tty settings

system( "rm $tmp_fname" );
 
B

Brian McCauley

I has faced a problem of calling shell aliases from a perl script recently.
Web search gave me almost nothing, so I feel obliged to share my results
with community :) The only way, which made perl script to execute my tcsh
aliases is below. I would very appreciate if anybody, who knows a better
method, posted it here.

Perhaps your mistake was to look for Perl solutuions. The solution is
more dependant on the languge being called than the language doing the
calling.

In general if you wish one program written in some language to cause a
script to be interpreted by a script interpreter for some other
language you need to pass the script in the other language interpreter
in one of three ways:

1 A pipe (if the interpreter supports reading from STDIN).
2 A temporary file (as you did).
3 On the command line (if the interpreter supports it).

Of these (3) is the simplest where it is possible. The exact what yo
do this depends on the interpreter you want to call. With the Perl
interpreter the switch used to pass the the script as a command line
argument is -e . In many other interpreters (including most (all?)
Unix shells) it is -c.

use strict;
use Cwd;
use Env;

my( $n_args ) = 0;
my( $own_name ) = "";
my( $user_cmd ) = "";
my( $n_sec ) = 0;
my( $ali ) = "";
my( $kb ) = "";

You are suffering premature declaration.

[ snip ]
my( $tmp_fname ) = "KILLME.kill";

There are better ways to get a temporary file BTW.
open( OUT, ">$tmp_fname" ) || die ( "Can't open file for writing: $!" );
print OUT "source ~/.aliases";
print OUT "\n";
print OUT $user_cmd;
print OUT "\n";
close( OUT );

[ snip ]
system( "tcsh $tmp_fname" ); # execute commands from a file

More simply

system ('tcsh','-c',"source ~/.aliases; $user_cmd");

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
D

Dmitry

Brian McCauley said:
I has faced a problem of calling shell aliases from a perl script recently.
Web search gave me almost nothing, so I feel obliged to share my results
with community :) The only way, which made perl script to execute my tcsh
aliases is below. I would very appreciate if anybody, who knows a better
method, posted it here.

Perhaps your mistake was to look for Perl solutuions. The solution is
more dependant on the languge being called than the language doing the
calling.

In general if you wish one program written in some language to cause a
script to be interpreted by a script interpreter for some other
language you need to pass the script in the other language interpreter
in one of three ways:

1 A pipe (if the interpreter supports reading from STDIN).
2 A temporary file (as you did).
3 On the command line (if the interpreter supports it).

Of these (3) is the simplest where it is possible. The exact what yo
do this depends on the interpreter you want to call. With the Perl
interpreter the switch used to pass the the script as a command line
argument is -e . In many other interpreters (including most (all?)
Unix shells) it is -c.

use strict;
use Cwd;
use Env;

my( $n_args ) = 0;
my( $own_name ) = "";
my( $user_cmd ) = "";
my( $n_sec ) = 0;
my( $ali ) = "";
my( $kb ) = "";

You are suffering premature declaration.

[ snip ]
my( $tmp_fname ) = "KILLME.kill";

There are better ways to get a temporary file BTW.
open( OUT, ">$tmp_fname" ) || die ( "Can't open file for writing: $!" );
print OUT "source ~/.aliases";
print OUT "\n";
print OUT $user_cmd;
print OUT "\n";
close( OUT );

[ snip ]
system( "tcsh $tmp_fname" ); # execute commands from a file

More simply

system ('tcsh','-c',"source ~/.aliases; $user_cmd");
Unfortunately, #3 doesn't work in my environment, so I had to stuck with #2...
Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top