简体   繁体   English

使perl脚本可执行...我可以取消前面的“ perl”语句吗?

[英]Making perl scripts executable… can I do away with the preceding 'perl' statement?

This is a pretty simple one... I just want to make a perl script executable without the preceding perl command, and instead let the environment deduce the interpreter from the shebang line. 这是一个非常简单的...我只想使一个perl脚本可执行而无需前面的perl命令,而是让环境从shebang行中推导出解释器。 Here is my sample script called test : 这是我的示例脚本test

#!/usr/bin/perl
print "Hey there\n";

I then use chmod 775 test to make the script executable. 然后,我使用chmod 775 test使脚本可执行。 If I use the command perl test , I get the output Hey there . 如果我使用命令perl testperl test得到输出Hey there

However, if I just type test , I get no output. 但是,如果我只是输入test ,则不会输出。 What's the deal? 这是怎么回事? Why isn't my shebang line making the environment realize this is perl? 为什么我的shebang线不能使环境意识到这是perl? Can someone please help me? 有人可以帮帮我吗?

Don't name your script test . 不要命名您的脚本test This is a built-in command in most shells, so they don't go looking for an external program. 在大多数shell中,这是一个内置命令,因此它们不会去寻找外部程序。

Also, to run a program in your current directory, you should type ./programname . 另外,要在当前目录中运行程序,应键入./programname It's generally a bad idea to have . 通常,这是一个坏主意. in your $PATH , which would be necessary to execute it without the directory prefix. $PATH ,这对于不带目录前缀执行它是必要的。

To run something from the current directory you need to prefix "./" to tell it "this directory" ie ./testprogram . 要从当前目录运行某些内容,您需要在“ ./” ./testprogram./testprogram ”这个./testprogram

If you type just test it will look in standard install directories like /bin. 如果您输入只是test ,它将查找标准安装目录,例如/ bin。 This is why when you run cp or rm it knows where the executable is. 这就是为什么当您运行cprm它知道可执行文件在哪里。

As mentioned by others, naming scripts test is not allowed with most shells. 正如其他人提到的那样,大多数外壳程序都不允许对命名脚本进行test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM