【问题标题】:Prevent Perl program from print newline after writing to sh terminal using system使用系统写入 sh 终端后防止 Perl 程序打印换行符
【发布时间】:2018-05-24 16:31:03
【问题描述】:

我正在尝试从文件中提取数据到 Perl 程序并使用“foreach”循环在 Linux sh 终端中执行它,但是每次 Perl 将值返回到 sh 终端时,它都会在打印下一个导致我的脚本的字符串之前打印一个新行失败。我该如何预防?

open(FILE, "input") or die("Unable to open file");

# read file into an array
@data = <FILE>;

# close file 
close(FILE);

foreach my $x(@data) {
    system "/granite_api.pl -type update_backdoor -project tgplp -test_id $x -turninid 4206";
}

预期输出:

/granite_api.pl -type update_backdoor -project tgplp -test_id example -turninid 4206

实际输出:

/granite_api.pl -type update_backdoor -project tgplp -test_id example 
 -turninid 4206

【问题讨论】:

    标签: linux bash shell perl


    【解决方案1】:

    @data = <FILE>;
    

    @data 包含输入文件中的所有行。每行以 LF 结尾。您需要从每个$x 中删除它,例如使用chomp(删除$/ 中设置的尾随字符)

    foreach my $x ( @data ) {
        chomp $x;
        system "/granite_api.pl -type update_backdoor -project tgplp -test_id $x -turninid 4206";
    }
    

    chomp in perldoc

    【讨论】:

    • chomp 也适用于列表:chomp(@data); 将 chomp @data 中的所有元素。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2013-03-01
    • 2018-07-24
    相关资源
    最近更新 更多