nanophd has asked for the wisdom of the Perl Monks concerning the following question:

Wise PerlMonks, I consider myself a novice at perl and I am in need of some assistance. I have an issue with regards to executing an alias from Perl. I have a script that that keeps track of my inputs in a file. When'-c' is inputed it should ask for a command, write the command to the file, execute command and write the output to the file as well. Then it should return back to the loop.

 `$cmd` was working fine for build in commands, but would not execute alias commands. I read that I would need to use the command

 `bash -c -i "$cmd"` This fixed the kinda fixed the problem, except it would exit the program and not reenter the loop.

Is there any way to run an alias command and continue with the rest of the code?

Here is the section of my code that is at fault:
if($input eq "-c"){ print "Enter Command: "; my $cmd = <STDIN>; print $fh $cmd; print $fh "\n\t#### START COMMAND OUTPUT ####\n"; print `bash -c -i "$cmd"`; foreach(`bash -c -i "$cmd"`){ print $fh "#".$_; } print $fh "\t#### END COMMAND OUTPUT ####\n"; next; }
Thank you in advance.

Replies are listed 'Best First'.
Re: How to execute alias commands
by choroba (Cardinal) on Jan 13, 2014 at 23:31 UTC
    Have you examined the exit code of the programme when it "exits"? It in fact just stops after recieving the SIGTTIN signal. It means the shell is not ended when the command is finished (it is an interactive shell), but it cannot get any input. You can easily test it by adding ; exit to the entered alias, which will close the shell and prevent the signal.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to execute alias commands (source your profile)
by Anonymous Monk on Jan 13, 2014 at 23:13 UTC
      Thank you for the reply. I liked the idea of using shell scripts as aliases. I've added /mybin to my path and I am able to execute the script successfully with argument values from the command line. However, when I execute it with perl, the argv values do not get passed.
      if($input eq "-c"){ print "Enter Command: "; my $cmd = <STDIN>; chomp($cmd); print $fh $cmd; print $fh "\n\t#### START COMMAND OUTPUT ####\n"; print `$cmd`; foreach(`$cmd`){ print $fh "#".$_; } print $fh "\t#### END COMMAND OUTPUT ####\n"; next; }
      Shell Script:
      #!/bin/bash perl /path/to/script/script.pl $1 $2 $3 $4
      Any suggestions?
        Never mind. I fell victim of my own stupidity. I was editing the wrong version of the script. Everything seems to be working perfectly.

        Thank you again!

Re: How to execute alias commands
by ww (Archbishop) on Jan 13, 2014 at 22:44 UTC

    Is the 'alias command' you're trying to execute not available/executable under its real name?

    Come, let us reason together: Spirit of the Monastery
      The alias executes a perl script.