加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl根据shebang线派遣到其他口译员?

发布时间:2020-12-15 21:55:05 所属栏目:大数据 来源:网络整理
导读:参见英文答案 Why don’t I get any syntax errors when I execute my Python script with Perl?1个 当我不小心使用perl运行bash脚本时,我的思绪被吹了……进一步尝试,似乎perl读取脚本的shebang并发送给正确的解释器: $cat /tmp/ohhai.sh#!/bin/bashecho o
参见英文答案 > Why don’t I get any syntax errors when I execute my Python script with Perl?1个
当我不小心使用perl运行bash脚本时,我的思绪被吹了……进一步尝试,似乎perl读取脚本的shebang并发送给正确的解释器:
$cat /tmp/ohhai.sh
#!/bin/bash
echo ohhai bash

$perl /tmp/ohhai.sh
ohhai bash
$cat /tmp/ohhai.py
#!/usr/bin/python2
print 'ohhai python'

$perl /tmp/ohhai.py
ohhai python
$cat /tmp/ohhai.groovy
#!/usr/bin/groovy
println 'ohhai groovy'

$perl /tmp/ohhai.groovy
ohhai groovy

嗯……哇?

为了确保我没有疯狂,我尝试与其他口译员一起做这件事,并确认这只是一个perl-ism:

$python /tmp/ohhai.sh
  File "/tmp/ohhai.sh",line 2
    echo ohhai bash
             ^
SyntaxError: invalid syntax
$ruby /tmp/ohhai.sh
ruby: no Ruby script found in input (LoadError)
$bash /tmp/ohhai.py
/tmp/ohhai.py: line 2: print: command not found

这是在某处记录的吗?这是一件新事物吗? ……为什么?

确实是“瑞士军队电锯”.

解决方法

这是 perldoc perlrun中记载的旧事物:

If the #! line does not contain the word “perl” nor the word “indir” the program named after the #! is executed instead of the Perl interpreter. This is slightly bizarre,but it helps people on machines that don’t do #!,because they can tell a program that their SHELL is /usr/bin/perl,and Perl will then dispatch the program to the correct interpreter for them.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读