在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)
答案 0 :(得分:1)
您应该在“slurp模式”中使用 try {
// Create the socket
ServerSocket server ( 15000 );
std::cout << "Server running on 0.0.0.0:15000" << std::endl;
std::string res;
while(true) {
ServerSocket new_sock;
server.accept ( new_sock );
try {
while(true) {
std::string data;
new_sock >> data;
res = process_query(data, kv_map, zset_map);
new_sock << res;
}
}
catch(SocketException&) {}
}
}
catch ( SocketException& e ){
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
return 0;
和cat
,而不是使用open
将文件内容转换为Perl变量:
<>
答案 1 :(得分:0)
我不确定这是想要实现的目标,但这是我的答案:
- (void)fontTest
{
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++) {
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (@"%@: %@", fontFamily, fontNames);
}
}
如果您需要这些行,my $var = `/bin/cat $0`; # the Perl program itself ;-)
print $var;
可以拆分为$var
。
$/
答案 2 :(得分:0)
以下是一些方法:
#!/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);
}
}