Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running shell is wrong when called from script #3

Closed
barefootcoder opened this issue Apr 17, 2014 · 4 comments
Closed

Running shell is wrong when called from script #3

barefootcoder opened this issue Apr 17, 2014 · 4 comments
Labels

Comments

@barefootcoder
Copy link

If you call a Perl program using Shell::Guess->running_shell from inside a (say) bash script, it returns the shell of the command line used to launch the script as opposed to bash. This is because the method used by running_shell pulls the whole command line out of the file in the /proc system, but doesn't restrict what it looks at to arg 0.

A simple command to demonstrate this:

> tcsh -c 'bash -c "'"perl -MShell::Guess -le 'print Shell::Guess->running_shell->name'"\"
tc

That should really return bash. I'm not sure how to turn this into a useful test, however, since the test suite needs to run on all OSes.

The patch is simple:

diffing ./common/perl/Shell/Guess.pm against ./perl5/perlbrew/perls/perl-5.14.4/lib/site_perl/5.14.4/Shell/Guess.pm
57c57
<     $command_line =~ s/\0$//;

---
>     $command_line =~ s/\0.*$//;

or I could turn that into a pull request if that's easier.

@plicease plicease added the bug label Apr 18, 2014
@plicease
Copy link
Member

I think you are right. Let me give it some thought probably next week I can spend an hour or two to address it.

Any test for this can be skipped on OSes that don't have a /proc.

plicease added a commit that referenced this issue Apr 18, 2014
@plicease
Copy link
Member

So I have pushed out a dev version with this change as 0.02_01 on CPAN

If it still looks good in a few days I will promote this to a regular release.

Interestingly, with your particular example, I am still not getting bash:

iscah% tcsh -c 'bash -c "'"perl -MShell::Guess -le 'print Shell::Guess->running_shell->name'"\"
c

when I stepped through in the debugger I was seeing this as the parent command line:

  DB<1> x $command_line
0  "-csh\c@-c\c@bash -c \"perl -d -MShell::Guess -le 'print Shell::Guess->running_shell->name'\"\c@"

So I think bash must be doing an exec to execute Perl in this case, which I think is mostly impossible to defend against.

@plicease
Copy link
Member

On CPAN as 0.03

@barefootcoder
Copy link
Author

So I think bash must be doing an exec to execute Perl in this case, which I think is mostly impossible to defend against.

You're probably right there; I was trying to compact a test case into a one-liner there, but perhaps I didn't test thoroughly enough. A more realistic test would be to create, say, a bash script like so:

#! /bin/bash

perl -MShell::Guess -le 'print Shell::Guess->running_shell->name'

and then run that script from, say, tcsh.

IAE, your new version definitely fixes the issue, so thanx for the quick turnaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants