【问题标题】:turn off terminal echo in mac?关闭mac中的终端回声?
【发布时间】:2016-07-21 16:21:40
【问题描述】:

我正在 Mac 上编写一个简单的 perl 脚本,它将一堆单词放在单独的行中并将它们打印出来:

#!/usr/bin/perl -w
use strict;
select STDERR; $| = 1;
select STDOUT; $| = 1; # auto flushing
my %count;
print "Please type in a series of words in seperate lines and ends with Ctrl+D.\n";
chomp(my @series = <STDIN>);
# print "\n";
foreach my $name (@series){`
    print "$name\n";    
}

这是输入:

a
a
b
(ctrl+D)

我注意到在我完成输入并按 ctrl+D 后,我在终端中看到了这个:

aD
a
b

我认为的原因是因为终端也回显了没有“\n”的^D,当我的程序打印出输入时,它会踩到^,留下我看到的aD(你可以通过取消注释来检查这个第 8 行中的 print "\n")所以我想我要问的是我应该怎么做才能防止终端回显 ^D?

【问题讨论】:

    标签: macos terminal


    【解决方案1】:

    一种简单的方法是使用stty 暂时关闭该功能。在命令行上会是

    stty -echoctl
    

    关闭,并且

    stty echoctl
    

    打开。如果您从您的脚本中对这些进行了system 调用,它将执行所要求的操作。这是一个例子:

    #!/usr/bin/env perl
    use strict;
    select STDERR; $| = 1;
    select STDOUT; $| = 1; # auto flushing
    system("stty -echoctl");
    my %count;
    print "Please type in a series of words in separate lines and ends with Ctrl+D.\n";
    chomp(my @series = <STDIN> );
    print "\n";
    foreach my $name (@series){
    print "$name\n";
    }
    system("stty echoctl");
    1;
    

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2010-09-30
      • 2017-01-20
      • 2015-06-23
      • 2014-03-16
      • 1970-01-01
      相关资源
      最近更新 更多