将bash脚本代码更改为perl
在bash中:将bash脚本代码更改为perl,bash,perl,Bash,Perl,在bash中: #!/bin/bash var=$(cat ps.txt) for i in $var ; do echo $i done 而ps.txt是: 356735 535687 547568537 7345673 3653468 2376958764 12345678 12345 现在我想用perl实现这一点,或者我想知道如何将命令的输出保存在perl中的变量中,比如var=$(cat ps.txt),而不是使用cat将文件内容保存到perl变量中,您应该在“slurp模式”中使用
#!/bin/bash
var=$(cat ps.txt)
for i in $var ; do
echo $i
done
而ps.txt是:
356735
535687
547568537
7345673
3653468
2376958764
12345678
12345
现在我想用perl实现这一点,或者我想知道如何将命令的输出保存在perl中的变量中,比如
var=$(cat ps.txt)
,而不是使用cat
将文件内容保存到perl变量中,您应该在“slurp模式”中使用open
和
:
打开我的$fh,“我不确定这是为了实现什么,但这是我的答案:
my $var = `/bin/cat $0`; # the Perl program itself ;-)
print $var;
如果需要这些行,可以在$/
上拆分$var
#! /usr/bin/perl -w
my $var = `/bin/cat $0`;
print $var;
my $n = 1;
for my $line ( split( $/, $var ) ){
print "$n: $line\n";
$n++;
}
以下是一些方法:
#!/usr/bin/perl
$ifile = "ps.txt";
# capture command output
# NOTE: this puts each line in a separate array element -- the newline is _not_
# stripped
@bycat = (`cat $ifile`);
# this strips the newline from all array elements:
chomp(@bycat);
# so would this:
# NOTE: for this type of foreach, if you modify $buf, it also modifies the
# corresponding array element
foreach $buf (@bycat) {
chomp($buf);
}
# read in all elements line-by-line
open($fin,"<$ifile") or die("unable to open '$ifile' -- $!\n");
while ($buf = <$fin>) {
chomp($buf);
push(@byread,$buf);
}
close($fin);
# print the arrays
# NOTE: we are passing the arrays "by-reference"
show("bycat",\@bycat);
show("byread",\@byread);
# show -- dump the array
sub show
# sym -- name of array
# ptr -- reference to array
{
my($sym,$ptr) = @_;
my($buf);
foreach $buf (@$ptr) {
printf("%s: %s\n",$sym,$buf);
}
}
!/usr/bin/perl
$ifile=“ps.txt”;
#捕获命令输出
#注意:这将把每一行放在一个单独的数组元素中——换行符不是_
#剥去
@bycat=(`cat$ifile`);
#这将从所有数组元素中删除换行符:
chomp(@bycat);
#这也会:
#注意:对于这种类型的foreach,如果修改$buf,它也会修改
#对应数组元素
foreach$buf(@bycat){
chomp($buf);
}
#逐行读入所有元素
在bash
@chepner中,open($fin),,而readi;do echo“$i”;done
是一种更正确的方法,我想在Perl中这样做open
非常简单。
返回文件的内容,一条记录一条记录(默认情况下是一行一行地记录)。若要将整个文件作为单个记录返回,需要取消设置记录分隔符变量-$/
,这是通过local$/;
实现的。原始bash
代码逐行迭代内容(尽管不正确),因此单独使用$/
并使用my@file_contents=;
可能更正确。$0
是脚本的名称,而不是参数零(例如,从foo bar
,$0
是“foo”而不是“bar”)。在perl中,命令行参数位于[预先提供的]中数组@ARGV
。因此,将$0
替换为$ARGV[0]
@CraigEstey我正在使用Perl程序本身进行测试:-),但我应该说清楚。是的。我没有“摸索”那个,我已经做了20多年的perl了。该评论“修复”了一些问题。
#!/usr/bin/perl
$ifile = "ps.txt";
# capture command output
# NOTE: this puts each line in a separate array element -- the newline is _not_
# stripped
@bycat = (`cat $ifile`);
# this strips the newline from all array elements:
chomp(@bycat);
# so would this:
# NOTE: for this type of foreach, if you modify $buf, it also modifies the
# corresponding array element
foreach $buf (@bycat) {
chomp($buf);
}
# read in all elements line-by-line
open($fin,"<$ifile") or die("unable to open '$ifile' -- $!\n");
while ($buf = <$fin>) {
chomp($buf);
push(@byread,$buf);
}
close($fin);
# print the arrays
# NOTE: we are passing the arrays "by-reference"
show("bycat",\@bycat);
show("byread",\@byread);
# show -- dump the array
sub show
# sym -- name of array
# ptr -- reference to array
{
my($sym,$ptr) = @_;
my($buf);
foreach $buf (@$ptr) {
printf("%s: %s\n",$sym,$buf);
}
}