Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > aliased shell commands from perl script

Reply
Thread Tools

aliased shell commands from perl script

 
 
Dmitry
Guest
Posts: n/a
 
      07-09-2004
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" );
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      07-11-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Dmitry) writes:

> 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\\
 
Reply With Quote
 
 
 
 
Dmitry
Guest
Posts: n/a
 
      07-12-2004
Brian McCauley <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (Dmitry) writes:
>
> > 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
execute a shell script in a shell script moongeegee Perl Misc 2 12-04-2007 12:18 AM
Applet security restrictions and aliased domains douggunnoe@gmail.com Java 3 10-11-2007 12:34 PM
Aliased column in stored procedure not seen by datagrid .Net Sports ASP .Net 1 09-17-2005 05:00 AM
NetBeans + Mac - Text in Source Editor is very light (aliased) Leonard Slatkin Java 0 11-01-2003 06:11 PM
anti-aliased 2D vector graphics Andy C Python 10 08-04-2003 05:15 PM



Advertisments