说说Linux下的mv命令

当我们使用mv命令移动文件overwrite的时候,会不会有提示呢?

mv 有两个 option

-f # do not prompt before overwriting
-i # prompt before overwrite

此外我们还需要使用

$ alias mv

来查看mv命令有没有设置别名


Liunx

这篇文章: 有用 无用
创建 2017-07-11 23:01:31 / 更新 2017-07-11 23:05:13

说说Linux下的diff命令

diff 的语法

diff [options] from-file(修改前的文件) to-file(修改后的文件)

diff 的 options

-u  # Use the unified output format.
-r  # When compare directories, recursively compare any subdirectories found.
-d  # Change the algorithm to perhaps find a smaller set of changes.  This makes diff slower (sometimes much slower).
-q  # Report only whether the files differ, not the details of the differences.

Liunx

这篇文章: 有用 无用
创建 2017-07-06 22:16:50 / 更新 2017-07-06 22:16:50

如何打印Perl程序的进程号

$ perl -e 'print $$, "\n"' # just use $$

参考:

http://perldoc.perl.org/perlvar.html


Perl

这篇文章: 有用 无用
创建 2017-06-12 02:07:20 / 更新 2017-06-12 02:10:45

Mojo::UserAgent下载大文件的时候,如何打印下载进度

利用 Mojo::Message

#!/usr/bin/env perl
use feature qw(say);
use warnings;

use Mojo::UserAgent;

my $url = "https://cpan.metacpan.org/authors/id/S/SR/SRI/Mojolicious-7.30.tar.gz";
my $ua  = Mojo::UserAgent->new();
my $tx  = $ua->build_tx(GET => $url);
$tx->res->on(progress => sub {
    my $msg = shift;
    return unless my $len = $msg->headers->content_length;
    my $size = $msg->content->progress;
    say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%';
});
$ua->start($tx);
say($tx->res->code, ' ', $tx->res->message);

利用 Mojo::Content

#!/usr/bin/env perl

use 5.12.0;
use warnings;

use Mojo::UserAgent;

my $url = "https://cpan.metacpan.org/authors/id/S/SR/SRI/Mojolicious-7.30.tar.gz";
my $ua  = Mojo::UserAgent->new();
my $tx  = $ua->build_tx(GET => $url);

$tx->res->content->on(read => sub {
    my $content = shift;
    return unless my $len = $content->headers->content_length;
    my $size = $content->progress;
    say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%';
});
$ua->start($tx);

say($tx->res->code, ' ', $tx->res->message);

参考:

  1. http://stackoverflow.com/questions/10086592/mojouseragent-get-with-userdefined-callback
  2. https://metacpan.org/pod/Mojo::UserAgent#build_tx
  3. https://metacpan.org/pod/Mojo::Message#progress
  4. https://metacpan.org/pod/Mojo::Content#read

Perl

这篇文章: 有用 无用
创建 2017-04-16 05:25:46 / 更新 2017-04-16 05:25:46

在Perl中捕捉和处理 Signals

通过 %SIG Hash

use strict;
use warnings;

$SIG{INT} = sub {die "Caught a sigint $!"};

sleep(100);

如果运行这个Perl程序,然后再按control-c,就会发送 SIGINT 给Perl程序,然后 $SIG{INT} 对应的方法会捕捉到 SIGINT 信号

通过 sigtrap 指令

sigtrap 是用来更方便的维护 %SIG 的指令

sigtrap 把信号分为3组:

  • normal-signals(HUP, PIPE, INT and TERM)

  • error-signals(ABRT, BUS, EMT, FPE, ILL, QUIT, SEGV, SYS and TRAP)

  • old-interface-signals(ABRT, BUS, EMT, FPE, ILL, PIPE, QUIT, SEGV, SYS, TERM and TRAP)

例如:

use strict;
use warnings;
use sigtrap qw/die normal-signals/;

sleep(100);


use strict;
use warnings;
use sigtrap qw/handler signal_handler normal-signals/;

sub signal_handler {
    die "Caught a signal $!";
}

sleep(100);

关于 signal

The default signal for kill is TERM

control+c will send interrupt signal(SIGINT) to program

参考:

  1. http://perltricks.com/article/37/2013/8/18/Catch-and-Handle-Signals-in-Perl/
  2. https://superuser.com/questions/107543/bash-man-page-kill-pid-vs-kill-9-pid
  3. https://metacpan.org/pod/sigtrap

Perl

这篇文章: 有用 无用
创建 2017-04-16 03:41:41 / 更新 2017-04-16 05:26:18