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

Dear Monks, I am at my wits ends trying to resolve this problem. I have an alias in Red Hat linux (e.g. tt which translates into cd directoryname; pwd). Now when I execute this from within perl using either perl -e "system 'tt';" or perl -e "exec 'tt';" I don't get anything at all. However, from the shell, I get the name of the working directory. Any ideas what I am missing here? Thanks. Alia(s)bused

Replies are listed 'Best First'.
Re: system calls and shell aliases
by Fletch (Bishop) on Aug 17, 2005 at 03:51 UTC

    Of course what no one's mentioned yet is that this particular alias (changing the working directory) isn't really going to do much good since you're changing the child shell's CWD, not your perl process'. See the FAQ (perldoc -q environment).

    --
    We're looking for people in ATL

Re: system calls and shell aliases
by davidrw (Prior) on Aug 17, 2005 at 03:05 UTC
    When the system or exec or backticks spawn a shell it doesn't load your settings, and doesn't inherit the current environment.

    As a workaround, you could just run "cd $dirname && pwd" as the command instead of "tt". Alternatively (and more robustly), move your "tt" from being an alias to being an executable shell script in say ~/bin/tt .. then your system or backtick call can just run "/home/myname/bin/tt".
Re: system calls and shell aliases
by merlyn (Sage) on Aug 17, 2005 at 03:06 UTC
Re: system calls and shell aliases
by superfrink (Curate) on Aug 17, 2005 at 05:21 UTC
    A shell alias is something that your shell deals with. The aliases only exist as shortcuts in your shell white the shell is executing.

    These aliases do not exist as program files in the filesystem. When you run system("tt") from perl the directories in your PATH environment variable are searched for a program file named tt. In this case no program named tt is being found.

    The man page for BASH indicates alias expansion takes place after one full line of text is read in. This could mean that aliases are only expanded when BASH is reading from a terminal and not when it has been passed a command to run.

    Normally BASH runs in a terminal and executes a loop where it reads a command and runs the command then repeats. It might be that aliases are only expaned inside of that loop but I'm not sure that is the case.

    I tried running perl -e 'system("bash --login -c l");' where I have a BASH alias mapping "l" to "ls -l" and I got back: "bash: line 1: l: command not found".

    As an aside the man page for BASH has this text in the BUGS section:
      Aliases are confusing in some uses.
Re: system calls and shell aliases
by Anonymous Monk on Aug 17, 2005 at 12:24 UTC
    Whaow, Thank you guys for the quick response. The tt was only an dirty instance I used for exemplification. In reality, I am supposed to write a script to run as a cron for an integrated library system. This system runs a number of interactive aliases. I was hoping to use the shortcut of just running the aliases. The ideas of creating executable shell scripts sounds like an excellent idea. Thank you so much guys. Randal, it's an honor! Alia(s)mused..
      why not sourcing your .bashrc (or anywhere you define your alias) before calling the alias?
      perl -e 'system("source ~/.profile; source ~/.bashrc; bash --login -c +l");'
      For instance, matlab is an alias for me defined in ~/.profile. Try to check if matlab exist as a command from perl:
      $perl -e 'system("command -v matlab")' $perl -e 'system("source ~/.profile;command -v matlab")' alias matlab='/Applications/MATLAB_R2015a.app/bin/matlab'
      Best, Hatef