如何使用Perl创建shell脚本?

时间:2022-06-27 07:12:59

I have a Perl script called replaceUp:

我有一个名为replaceUp的Perl脚本:

 #!/usr/bin/perl                                          

 search=$1
 replace=$2

 find . -type f -exec perl -p -i -e "s/$search/$replace/g" {} \;

The script does not get loaded. This suggests me that my script is wrong.

该脚本未加载。这表明我的脚本错了。

How can you make a shell script using Perl?

如何使用Perl创建shell脚本?

1 个解决方案

#1


The first line should be #!/bin/sh, not #!/usr/local/bin/perl. You are mistaken that that is a Perl script; it is a shell script that calls Perl.

第一行应该是#!/ bin / sh,而不是#!/ usr / local / bin / perl。你错了,这是一个Perl脚本;它是一个调用Perl的shell脚本。

It's also not going to actually work because $search and $replace are not going to get interpolated inside single quotes. Try single quotes inside double quotes.

它也不会真正起作用,因为$ search和$ replace不会在单引号内插入。在双引号内尝试单引号。

Or better yet, try my mass search/replace Perl script. I keep a pure-Perl script for this around because, dangerous as mass search/replace is, you don't need multiple levels of shell metacharacter interpretation taking it from dangerous to absolutely lethal.

或者更好的是,尝试我的大规模搜索/替换Perl脚本。我为此保留了一个纯Perl脚本,因为大众搜索/替换是危险的,你不需要多级别的shell元字符解释从危险到绝对致命。

#1


The first line should be #!/bin/sh, not #!/usr/local/bin/perl. You are mistaken that that is a Perl script; it is a shell script that calls Perl.

第一行应该是#!/ bin / sh,而不是#!/ usr / local / bin / perl。你错了,这是一个Perl脚本;它是一个调用Perl的shell脚本。

It's also not going to actually work because $search and $replace are not going to get interpolated inside single quotes. Try single quotes inside double quotes.

它也不会真正起作用,因为$ search和$ replace不会在单引号内插入。在双引号内尝试单引号。

Or better yet, try my mass search/replace Perl script. I keep a pure-Perl script for this around because, dangerous as mass search/replace is, you don't need multiple levels of shell metacharacter interpretation taking it from dangerous to absolutely lethal.

或者更好的是,尝试我的大规模搜索/替换Perl脚本。我为此保留了一个纯Perl脚本,因为大众搜索/替换是危险的,你不需要多级别的shell元字符解释从危险到绝对致命。