Linux 如何正确运行 Perl“one liner”命令行脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3220215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:14:27  来源:igfitidea点击:

How can I properly run Perl "one liner" command line scripts?

linuxperlcommand-line

提问by Rick

I have looked through a number of tutorials, but I still can't figure out what I am doing wrong... I am trying the code below (in a .pl Perl file, as an executable):

我已经浏览了许多教程,但我仍然无法弄清楚我做错了什么......我正在尝试下面的代码(在 .pl Perl 文件中,作为可执行文件):

#!/usr/bin/perl

perl -e 'print "Hello";' 

I run this script and get:

我运行这个脚本并得到:

Execution of /home/user1/Desktop/file_backups.pl aborted due to compilation errors.

由于编译错误,/home/user1/Desktop/file_backups.pl 的执行中止。

(I'm new to using Perl to call the Linux command line.)

(我是使用 Perl 调用 Linux 命令行的新手。)

采纳答案by Ether

perlis not a valid command inside a Perl script. If you had named that file as a .sh script, and used #!/bin/bashon the shebang line, it would have worked, but it doesn't really make a lot of sense to write a bash file just to invoke Perl (why not invoke Perl directly?)

perl不是 Perl 脚本中的有效命令。如果您将该文件命名为 .sh 脚本,并#!/bin/bash在 shebang 行上使用,它会起作用,但是编写一个 bash 文件只是为了调用 Perl 并没有多大意义(为什么不直接调用 Perl ?)

Since you mentioned you want to interact with the command line, I'll mention here that you can get at the command line options within Perl via the @ARGVarray. (See perldoc perlvar.)

既然您提到要与命令行交互,我将在这里提到您可以通过@ARGV数组获取 Perl 中的命令行选项。(请参阅perldoc perlvar。)

回答by miku

Try:

尝试:

#!/usr/bin/perl
# This is a comment ~~~
# This script will be run as a Perl script
# since 'perl' isn't a keyword or function in Perl
# something like this must fail:
#
# perl -e 'print "Hello";' 
#
# The following should work.

print "Hello"; print " World\n";

Or, if you want your shell script to execute Perl code:

或者,如果您希望 shell 脚本执行 Perl 代码:

#!/bin/sh
# That's a Bash script ~~~
# It's just a command line in a file ...    

perl -e 'print "Hello World";' 

Background: #!is an interpreter directive.

背景:#!是一个解释器指令

When the command is executed, it is converted to an execution of the interpreter.

当命令被执行时,它被转换为解释器的执行。

回答by eruciform

You might be trying to do this:

您可能正在尝试这样做:

system("/usr/bin/perl -e 'print \"Hello!\\n\";'");

回答by eruciform

Since you said you're looking for the opposite, this may be what you mean:

既然你说你在寻找相反的东西,这可能是你的意思:

# vi stdin.pl
# cat stdin.pl
  #!/usr/bin/perl
  while(<STDIN>)
  {
      if( /^hello/ ){ print "Hello back to ya!\n"; }
  }
# chmod 0777 stdin.pl
# ./stdin.pl
  foo
  hello
  Hello back to ya!
#

回答by Henno Brandsma

Just type on the command line (not in a file):

只需在命令行上输入(不是在文件中):

perl -e 'print "Hello World\n";'

This is only really good for one-liners. Longer scripts need their own file...

这仅对单线者非常有用。较长的脚本需要自己的文件...

回答by Greg Bacon

Say you have the following inputs:

假设您有以下输入:

$ for i in a b c ; do echo "$i is $i" > $i; done

$ cat a b c
a is a
b is b
c is c

Everybody knows capital letters are muchbetter!

每个人都知道大写字母好得多!

perl -i.bak -pe '$_ = uc' a b c

So now

所以现在

$ cat a b c
A IS A
B IS B
C IS C

But we'd really like to can this in a command called upcase, and that's easy to do!

但是我们真的很想在一个名为 的命令中做到这一点upcase,这很容易做到!

#! /usr/bin/perl -pi.bak
$_ = uc

See it at work:

在工作中看到它:

$ for i in a b c ; do echo "$i is $i" > $i; done

$ cat a b c
a is a
b is b
c is c

$ ./upcase a b c

$ !cat
cat a b c
A IS A
B IS B
C IS C

More tips from an answer to a similar question:

来自对类似问题回答的更多提示:

The -eoption introduces Perl code to be executed—which you might think of as a script on the command line—so drop it and stick the code in the body. Leave -pin the shebang (#!) line.

In general, it's safest to stick to at most one "clump" of options in the shebang line. If you need more, you could always throw their equivalents inside a BEGIN {}block.

Don't forget to turn on the execute bit!

chmod +x script-name

-e选项引入了要执行的 Perl 代码——您可以将其视为命令行上的脚本——因此删除它并将代码粘贴在正文中。留-p在shebang ( #!) 行。

一般来说,在shebang 行中坚持最多一个“一组”选项是最安全的。如果你需要更多,你总是可以将它们的等价物扔到一个BEGIN {}块中。

不要忘记打开执行位!

chmod +x script-name

回答by Slak

This should work. Double quotes outside single quotes inside ;)

这应该有效。内单引号外的双引号;)

perl -e "print 'Hello'"