Category Archives: Programming

Linux系统编程知识在运维排错中的应用

1.    谁偷了我的磁盘空间? 发现大小为10G的/home分区磁盘空间使用了9.8G,但是通过du得到的大小却是5G. 另外的4.8G空间哪里去了呢? df -Th /home Filesystem    Type    Size  Used Avail Use% Mounted on /dev/sda3      ext3     10G   9.8G  0.2G  98% /home du -hs /home 5.0G    /home/ du和df都不太可能出错,文件系统也没有损坏的迹象,那问题出在哪儿呢?查看unlink系统调用的手册页:

Posted in Programming, System Administration | Tagged , , | Leave a comment

Bash quote trap: why rsync exclude pattern not work

A friend wanted to sync all his files to a remote host, exluding the source files(those with suffix .c, .cpp, .h, .hpp). First I wrote this script #!/bin/bash ####sync.sh (version 1): sync files to remote host### SYNC_DST=’xxx@example.com::sample_project’ SYNC_OPT=’ –exclude=”*.[ch]” –exclude=”*.[ch]pp” … Continue reading

Posted in Bash, Programming, System Administration | Tagged , , , | 2 Comments

Using libcurl: follow redirect and get header only

Let’s look at a sample code first(I use the WWW::Curl perl module here, the idea is the same for other language). #!/usr/bin/perl use warnings; use strict; use WWW::Curl::Easy; my $url = ‘http://search.cpan.org/CPAN/authors/id/L/LO/LORN/LWP-Curl-0.09.tar.gz'; my $resp_body; #Get file length via HTTP HEAD … Continue reading

Posted in Perl, Programming | Tagged , , | Leave a comment

Create new KVM guest from template

Most of us don’t like to install guest OS repeatedly, instead, we often install one guest OS, then do some setup and customization. After that, we make a backup of the disk image(We use it as a template). If we … Continue reading

Posted in Programming, Python, System Administration, Virtualization | Tagged | 3 Comments

A convenient Bash function to backup configuration file

For sysadmins, it’s a good practice to backup configuration files before changing them. We normally do it via cp. eg: cp named.conf named.conf.bak cp named.conf named.conf.orig I personally like to use the date time as the file name suffix, so … Continue reading

Posted in Programming, System Administration | Tagged , | Leave a comment

正则表达式之环顾断言(续)

3. 肯定后顾断言(positive look-behind assertion): (?<=pattern) 从当前匹配位置往后(左)看,将当前匹配位置左(后)边的字符串与子模式进行比较,若匹配则成功。 例如,要匹配Perl Hacker(而不是Java Hacker 或者Python Hacker). 即要求Hacker之前必须是单词Perl, 可以这样: /(?< =\bPerl)\s+Hacker/ 测试字符串: He is a Perl Hacker --------位置------- 0123456789012345678 ----------1-------- 可以成功匹配。我们来分析下。 首先,确定当前匹配位置。断言子模式右边的模式成功匹配的位置, 就是当前位置。后面的模式是 /\s+Hacker/ 可知当前匹配位置为12.去掉其后面的字符,将剩下的字符串”He is a Perl”与断言子模式 \bPerl进行比较,结果匹配成功,于是整个模式的成功匹配。 测试字符串: He is just another Java Hacker … Continue reading

Posted in Programming | Tagged , | Leave a comment

正则表达式之环顾断言

在正则表达式中,环顾断言(Look-Around Assertions)属于扩展模式,因为它是零宽度断言(zero-length assertions),所以它的结果只是匹配成功或者失败,而不会记录匹配到的东西。从结果上,环顾断言分为肯定断言(positive assertion)和否定断言(negative assertion)。肯定断言成功的前提是其子模式成功匹配,而否定断言成功的前提是其子模式不匹配。从方向上看,环顾断言又分为前瞻断言(从当前匹配位置向前/右看,look-ahead)和后顾断言(从当前匹配位置往后/左,look-behind)。这里前和后容易搞混,可以这样想,正则表达式是从左到右进行匹配的,从起点(左)出发,往前即是往右走,往后即是往左走。 综合起来,环顾断言共有以下四种情况: 1. 肯定前瞻断言(positive look-ahead assertion): (?=subpattern) 从当前匹配位置往前看,若子模式匹配,则成功。这里有一个来自《精通Perl》第二章,”高级正则表达式”的例子:匹配同时包含单词Wilma和Fred的行(不管顺序如何)。 $_ = “Here come Wilma and Fred!”; print “Matches: $_” if /(?=.*Wilma).*Fred/; 分析这个正则表达式的时候,我们首先要确定当前匹配位置。忽略断言(?=.*Wilma),于是得到模式 .*Fred 成功匹配它之后,当前匹配位置为0,也就是字符串的开头。然后再看断言子模式 .*Wilma 如果这个字符串中包含Wilma,那么匹配成功。注意,由于两个子模式的起点都是字符串的开头,所以无论是 Here come Wilma and Fred 还是 Here come Fred and Wilma,也就是说,不管Fred和Wilma的位置如何,我们的模式都能正确匹配。 … Continue reading

Posted in Programming | Tagged , | Leave a comment

让Yoast Breadcrumbs支持中文

修改wp-content/plugins/breadcrumbs/yoast-breadcrumbs.php: — wp-content/plugins/breadcrumbs/yoast-breadcrumbs.php (revision 22) +++ wp-content/plugins/breadcrumbs/yoast-breadcrumbs.php (revision 23) @@ -49,7 +49,7 @@ foreach (array(‘home’, ‘blog’, ‘sep’, ‘singleparent’, ‘prefix’, ‘archiveprefix’, ‘searchprefix’, ‘breadcrumbprefix’, ‘breadcrumbsuffix’) as $option_name) { if (isset($_POST[$option_name])) { – $opt[$option_name] = htmlentities(html_entity_decode($_POST[$option_name])); + $opt[$option_name] = htmlentities(html_entity_decode($_POST[$option_name], ENT_COMPAT, “UTF-8″), ENT_COMPAT, … Continue reading

Posted in Programming, WEB Application | Tagged , | 1 Comment

Shell programming trap: batch rename with find

When we want to rename all files under the current directory and its sub-directories, say renaming files with .shell suffix to those with .sh suffix. we may write this command: find . -name “*.shell” -exec mv {} $(echo {} | … Continue reading

Posted in Programming, System Administration | Tagged | Leave a comment

Display RSS feed in Joomla!

Better not reinvent the wheel Last week, my boss asked me to add a module to show his latest WordPress blog post on our Joomla! site. Then I began to dig google and the Joomla! Extensions Directory for such kind … Continue reading

Posted in Programming, WEB Application | Tagged , | Leave a comment