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" );
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" );