perl doesn't recogize alias?

J

Jianping Wu

Hi, I am confused by the following problem:

On bash shell, I did:
alias gcc='grep'

Then I wrote a perl script with only one line:
`gcc -v`;

On bash shell, if I type "gcc -v", I got:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

This is correct because gcc is aliased to grep.
But when I run the perl script, I got:
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --disable-libunwind-exceptions --with-system-zlib
--enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

How come Perl doesn't recognize the aliasing?

Thanks for your help!

Jianping
 
J

John Bokma

Jianping said:
Hi, I am confused by the following problem:

On bash shell, I did:
alias gcc='grep'

Then I wrote a perl script with only one line:
`gcc -v`;

On bash shell, if I type "gcc -v", I got:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

This is correct because gcc is aliased to grep.
But when I run the perl script, I got:
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --disable-libunwind-exceptions --with-system-zlib
--enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

How come Perl doesn't recognize the aliasing?

Quick guess: ` ` runs in it's own shell, e.g. if you open up another xterm
/ shell, and type gcc -v you get the same message.
 
C

Chris Mattern

Jianping said:
Hi, I am confused by the following problem:

On bash shell, I did:
alias gcc='grep'

Then I wrote a perl script with only one line:
`gcc -v`;

Perl has a rule about how it handles backticks (and similar calling
of external programs). If it has shell metacharacters in it, it
passes the string to /bin/sh. If it doesn't, it passes the string
to exec(). Your example gets passed to exec(), so no shell aliases
for you!
On bash shell, if I type "gcc -v", I got:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

This is correct because gcc is aliased to grep.
But when I run the perl script, I got:
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --disable-libunwind-exceptions --with-system-zlib
--enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

How come Perl doesn't recognize the aliasing?

Because it doesn't pass it to the shell.
Thanks for your help!

Jianping

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
A

Axel

Perl has a rule about how it handles backticks (and similar calling
of external programs). If it has shell metacharacters in it, it
passes the string to /bin/sh. If it doesn't, it passes the string
to exec(). Your example gets passed to exec(), so no shell aliases
for you!

Even using shell metacharacters would not help since the alias would not
be inherited.

I suppose you could place the alias in a startup file, remembering
that the behaviour of bash in reading startup files differs according
to whether it is invoked by the name 'sh' or 'bash'.

Axel
 
C

Chris Mattern

Axel said:
Even using shell metacharacters would not help since the alias would not
be inherited.

I suppose you could place the alias in a startup file, remembering
that the behaviour of bash in reading startup files differs according
to whether it is invoked by the name 'sh' or 'bash'.

And that's on Linux. On, say, Solaris, where /bin/sh is an honest-to-
Ritchie real Borune shell, you can't even get that, because you've
got no aliases, period.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
J

Joe Smith

Jianping said:
Hi, I am confused by the following problem:

On bash shell, I did:
alias gcc='grep'

That command affects the current bash shell only.
It affects how command lines in the current shell
are interpreted. You should not expect anything
else to see the alias. Perl won't see it, and
neither will any programs invoked by perl.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top