Archive for the '●Shell标签' Category

perldoc -q “insert a line” 星期六, 07月 4th, 2009

perldoc -q "insert a line"
Found in /usr/share/perl/5.10/pod/perlfaq5.pod
How do I change, delete, or insert a line in a file, or append to the beginning of a
file?
(contributed by brian d foy)

The basic idea of inserting, changing, or deleting a line from a text file
involves reading and printing the file to the point you want to make the change,
making the change, then reading and printing the rest of the file. Perl doesn’t
provide random access to lines (especially since the record input separator, $/,
is mutable), although modules such as "Tie::File" can fake it.

crontab 啊 星期五, 05月 22nd, 2009

这么多年了,才发现没看过man 5 crontab
可以设置路径的哦。
可以设置用户的哦。
☎ cl
# m h dom mon dow command
SHELL=/bin/bash
PATH=/home/exp/应用/脚本/:/usr/local/bin:/bin:/usr/bin
*/30 * * * * msg 抽烟了,其他时间禁止吸烟。
50 17 * * 1-5 msg 下班了。
0 21 * * * msg 记账。

托盘菜单:脚本集合 星期五, 05月 8th, 2009

☎ dog tm-托盘菜单.pl
use utf8;
use Gtk2 -init;
sub popM {
my ($widget, $button, $time, $menu) = @_;
if ($button == 3) {
my ($x,$y,$push_in)=Gtk2::StatusIcon::position_menu($menu, $widget);
$menu->show_all();
$menu->popup(undef,undef,sub{return($x,$y,0)},undef,0,$time);
}
}
open(CFGFILE,"$ENV{HOME}/.traymenu.rc") or die "不能打开配置文件。";
my $menu = Gtk2::Menu->new();
my $item;
my $defaultcmd;
while(<CFGFILE>){
chomp;
next if /^#/;
my($menuname,$iconfile,$command)=split(/,/);
if(!$defaultcmd){$defaultcmd=$command;}
$item = Gtk2::ImageMenuItem->new_with_label("执行 $menuname");
$item->set_image(Gtk2::Image->new_from_file($iconfile));
$item->signal_connect('activate', sub{`$_[1]`;},$command);
$menu->append($item);
}
close CFGFILE;
my $status_icon = Gtk2::StatusIcon->new_from_stock('gtk-home');
$item = Gtk2::ImageMenuItem->new_from_stock('gtk-quit');
$item->signal_connect('activate', sub{Gtk2->main_quit();}, $status_icon);
$menu->append($item);
$status_icon->set_tooltip("托盘菜单:脚本集合");
$status_icon->signal_connect('activate', sub{`$defaultcmd`;});
$status_icon->signal_connect('popup-menu', \&popM, $menu);
$status_icon->set_visible(1);
Gtk2->main();

改良的sdcv.pl 星期一, 05月 4th, 2009


 1 #!/usr/bin/perl

 2 use Getopt::Long;

 3

 4 # 参数:单行输出选择。屏幕提示输出选择。

 5 GetOptions(1 => \$oneline, n=>\$notify);

 6

 7 my $out,$in;

 8 # 无参数时,使用剪贴板内容。

 9 $in=$ARGV[0]; if(!$in){$in=`xsel -o`;} if(!$in){exit;}

10 open(SDCV,"sdcv -n $in|");

11 my $r;

12 while($l=<SDCV>){

13 if($l!~/^$/){$r=$l;chomp($r);$r=~s/–>//;}

14 else{$out="$r –> ";last;}

15 }

16 while($l=<SDCV>){

17 if($l=~/相关|^$/){

18 close(SDCV);

19 if($notify){`notify-send -i ‘/home/exp/媒体/128软件png/pidgin.png’ ’sdcv翻译’ "$out"`;}

20 else{print $out;}

21 exit;

22 }

23 chomp($l) if($oneline);

24 $out.=" ► $l";

25 }

26

27

匹配居然要全部 星期四, 04月 23rd, 2009

my ($c,$w)=($arg=~/(\S+)\s+(.*)/); # $w没匹配到,就$c也没内容

文件内容添加行号。 星期四, 04月 23rd, 2009

perl -pi -e ‘$_ = sprintf “%04d %s”, $., $_’ record
-p 输出
-i 保存

最简单得到页面中所有链接的pl 星期二, 04月 7th, 2009

应该是最正常的了。而且最简单。
while($html=~m{<a href=(["'])(.*?)\1.*?>(<.*?/>)*(.*?)</a>}gsi){
#print ">>>$&<<<\n$2\t--->$4\n";
print "$2\t--->$4\n";
}

bash->perl 星期四, 04月 2nd, 2009

最近几天,无聊,把老的bash,能转perl的,都转了下。发现不想用bash了。

baidu.pl 星期二, 03月 31st, 2009

☎ dog baidu.pl
use LWP;
use LWP::UserAgent;
my $w=$ARGV[0];
$w=`echo "$w"|iconv -f utf8 -t gbk|uni2ascii -a J`;
chomp($w);
$w=~s/\ /+/g;
$w="http://www.baidu.com/s?wd=$w";
my $ua = LWP::UserAgent->new();
$ua->max_size( 15 * 1024 );
my $reply = $ua->get("$w");
my $html;
if ( $reply->is_success ) {
$html = $reply->content;
my @t=$html=~/href=\"http.*?<\/a>/g;
my $n=5;
for my $s (@t){
if($s=~/www\.baidu\.com/){next;}
if($s=~/tieba\.baidu\.com/){next;}
if($s!~/font>/){next;}
$s=~s/<.*?>//g;
$s=~s/^href=\"//;
$s=~s/\".*>/ - /g;
$s=`echo "$s"|iconv -t utf8 -f gbk`;
chomp($s);
print " ▛ ";
print $s;
$n--;
last if ($n==0);
}}
else {die "无法获取的地址。";}

强大的switch(perl) 星期二, 03月 31st, 2009

#----------------------------------------------
if(join(" ",@Amynick)=~$nick){ # 主人列表,私聊命令
my ($c,$w)=split(/\s/,$arg);
switch ($c){
case "join" {$self->join("${cfg_room}");}
case "nick" {$self->nick($w);}
case (\@ACmd) {
my $cmd;
if($w eq "on"){$cmd="$c=1";}else{$cmd="$c=0";}
eval "\$$cmd";
$self->privmsg("${cfg_room}", "Yes, Sir. $cmd\n");
$cmd="print \"----$c=\$$c----\t\"";
eval "$cmd";
last;
}
else {$self->privmsg("${cfg_room}", "$arg\n");}
}
}
#----------------------------------------------