简体   繁体   English

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

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

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 命令行的新手。)

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.当命令被执行时,它被转换为解释器的执行。

perl is not a valid command inside a Perl script. perl不是 Perl 脚本中的有效命令。 If you had named that file as a .sh script, and used #!/bin/bash on 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?)如果您将该文件命名为 .sh 脚本,并在 shebang 行上使用#!/bin/bash ,它会起作用,但是编写一个 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 @ARGV array.既然您提到要与命令行交互,我将在这里提到您可以通过@ARGV数组获取 Perl 中的命令行选项。 (See perldoc perlvar .) (请参阅perldoc perlvar 。)

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...较长的脚本需要自己的文件...

This should work.这应该有效。 Double quotes outside single quotes inside ;)内单引号外的双引号;)

perl -e "print 'Hello'"

您可能正在尝试这样做:

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

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!
#

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 much better!每个人都知道大写字母好得多!

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的命令中upcaseupcase ,这很容易做到!

#! /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 -e option 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. -e选项引入了要执行的 Perl 代码——您可以将其视为命令行上的脚本——因此将其删除并将代码粘贴在正文中。 Leave -p in the shebang ( #! ) line.-p留在 shebang ( #! ) 行中。

In general, it's safest to stick to at most one "clump" of options in the shebang line.一般来说,在shebang 行中坚持最多一个“一组”选项是最安全的。 If you need more, you could always throw their equivalents inside a BEGIN {} block.如果您需要更多,您可以随时将它们的等效项放入BEGIN {}块中。

Don't forget to turn on the execute bit!不要忘记打开执行位!

 chmod +x script-name

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

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