aliased shell commands from perl script

Discussion in 'Perl Misc' started by Dmitry, Jul 9, 2004.

  1. Dmitry

    Dmitry Guest

    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" );
    Dmitry, Jul 9, 2004
    #1
    1. Advertising

  2. (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\\
    Brian McCauley, Jul 11, 2004
    #2
    1. Advertising

  3. Dmitry

    Dmitry Guest

    Brian McCauley <> wrote in message news:<>...
    > (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!
    Dmitry, Jul 12, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .Net Sports
    Replies:
    1
    Views:
    373
    Verde
    Sep 17, 2005
  2. Leonard Slatkin
    Replies:
    0
    Views:
    440
    Leonard Slatkin
    Nov 1, 2003
  3. Andy C

    anti-aliased 2D vector graphics

    Andy C, Aug 1, 2003, in forum: Python
    Replies:
    10
    Views:
    615
    Borcis
    Aug 4, 2003
  4. Replies:
    3
    Views:
    323
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    236
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page