bash'中的Perl脚本;埃雷多克
是否可以在bash脚本中编写一个perl脚本作为herdoc 这不起作用(仅示例)bash'中的Perl脚本;埃雷多克,perl,bash,Perl,Bash,是否可以在bash脚本中编写一个perl脚本作为herdoc 这不起作用(仅示例) #/bin/bash perl这里的问题是,脚本正在stdin上传递给perl,因此试图从脚本处理stdin是行不通的 1.字符串文字 如果您使用perl-e将脚本传递给perl,那么就不会有stdin问题,您可以在脚本中使用您喜欢的任何字符。不过,这样做有点迂回。Heredocs在stdin上产生输入,我们需要字符串。怎么办?哦,我知道!这需要$(cat你知道我从来没有想到过这一点 答案是“是的!”它确实有效。
#/bin/bash
perl这里的问题是,脚本正在stdin上传递给perl,因此试图从脚本处理stdin是行不通的
1.字符串文字
如果您使用perl-e
将脚本传递给perl,那么就不会有stdin问题,您可以在脚本中使用您喜欢的任何字符。不过,这样做有点迂回。Heredocs在stdin上产生输入,我们需要字符串。怎么办?哦,我知道!这需要$(cat你知道我从来没有想到过这一点
答案是“是的!”它确实有效。正如其他人所提到的,
不能使用,但这很有效:
$ perl <<'EOF'
print "This is a test\n";
for $i ( (1..3) ) {
print "The count is $i\n";
}
print "End of my program\n";
EOF
This is a test
The count is 1
The count is 2
The count is 3
End of my program
$perl只是@John Kugelman答案的一小部分。您可以删除无用的cat
并使用:
read -r -d '' perlscript <<'EOF'
while(<>) {
chomp;
print "xxx: $_\n";
}
EOF
perl -e "$perlscript"
read-r-d''perlscript下面是在bash中使用PERL-herdoc脚本并充分利用它的另一种方法
#!/bin/sh
#If you are not passing bash var's and single quote the HEREDOC tag
perl -le "$(cat <<'MYPL'
# Best to build your out vars rather than writing directly
# to the pipe until the end.
my $STDERRdata="", $STDOUTdata="";
while ($i=<STDIN>){ chomp $i;
$STDOUTdata .= "To stdout\n";
$STDERRdata .= "Write from within the heredoc\n";
MYPL
print $STDOUTdata; #Doing the pipe write at the end
warn $STDERRdata; #will save you a lot of frustration.
)" [optional args] <myInputFile 1>prints.txt 2>warns.txt
!/bin/sh
#如果您没有传递bash var和单引号herdoc标记
perl-le“$(cat)在可执行文件的名称之后不需要一些命令行开关吗?例如perl-we
?@TLP否,OP只是将脚本作为标准输入传递。第二个问题:$\uuUcode>由shell插入,可能是一个空字符串。将其转义…print“xxx:\$\n“;
每个文件只能使用一次,但是perl的-x
标志可以使用不太难看的结果。@john kugelman是否可以通过herdoc
?@sdkks在perl脚本中使用Bash的变量,您可以使用导出的Bash变量$ENV{variable},例如,如果m/$ENV{HOME},则使用set | perl-lne'print”/“@JohnKugelman我在我的帖子中提到,你不能使用STDIN。问题是“有可能在某种程度上用bash脚本编写一个perl脚本作为herdoc”。而不是你是否可以从STDIN读取。答案是肯定的,它确实有效。
perl -e "$(cat <<'EOF'
while(<>) {
chomp;
print "xxx: $_\n";
}
EOF
)"
perl <(cat <<'EOF'
while(<>) {
chomp;
print "xxx: $_\n";
}
EOF
)
$ perl <<'EOF'
print "This is a test\n";
for $i ( (1..3) ) {
print "The count is $i\n";
}
print "End of my program\n";
EOF
This is a test
The count is 1
The count is 2
The count is 3
End of my program
read -r -d '' perlscript <<'EOF'
while(<>) {
chomp;
print "xxx: $_\n";
}
EOF
perl -e "$perlscript"
#!/bin/sh
#If you are not passing bash var's and single quote the HEREDOC tag
perl -le "$(cat <<'MYPL'
# Best to build your out vars rather than writing directly
# to the pipe until the end.
my $STDERRdata="", $STDOUTdata="";
while ($i=<STDIN>){ chomp $i;
$STDOUTdata .= "To stdout\n";
$STDERRdata .= "Write from within the heredoc\n";
MYPL
print $STDOUTdata; #Doing the pipe write at the end
warn $STDERRdata; #will save you a lot of frustration.
)" [optional args] <myInputFile 1>prints.txt 2>warns.txt
#!/bin/sh
set WRITEWHAT="bash vars"
#If you want to include your bash var's
#Escape the $'s that are not bash vars, and double quote the HEREDOC tag
perl -le "$(cat <<"MYPL"
my $STDERRdata="", $STDOUTdata="";
while (\$i=<STDIN>){ chomp \$i;
\$STDOUTdata .= "To stdout\n";
\$STDERRdata .= "Write $WRITEWHAT from within the heredoc\n";
MYPL
print \$STDOUTdata; #Doing the pipe write at the end
warn \$STDERRdata; #will save you a lot of frustration.
)" [optional args] <myInputFile 1>prints.txt 2>warns.txt