将一个目录下文件名包含某字符串的文件复制到其他目录
ls source/ | grep specialchars | xargs -n 1 -i cp source/{} target/
redis命令行批量统计多个set记录的和
/usr/local/redis/bin/redis-cli keys *:20130130:ipset | xargs /usr/local/redis/bin/redis-cli scard
将一个目录下文件名包含某字符串的文件复制到其他目录
ls source/ | grep specialchars | xargs -n 1 -i cp source/{} target/
redis命令行批量统计多个set记录的和
/usr/local/redis/bin/redis-cli keys *:20130130:ipset | xargs /usr/local/redis/bin/redis-cli scard
LNMP的环境,当前PHP版本5.3.8,遇到一个应用需求只支持PHP 5.2.x,又希望保持现有应用还是用PHP 5.3.8。也就是说需要两个版本的PHP同时存在,供nginx根据需要调用不同版本。
Nginx是通过PHP-FastCGI与PHP交互的。而PHP-FastCGI运行后会通过文件、或本地端口两种方式进行监听,在Nginx中配置相应的FastCGI监听端口或文件即实现Nginx请求对PHP的解释。
既然PHP-FastCGI是监听端口和文件的,那就可以让不同版本的PHP-FastCGI同时运行,监听不同的端口或文件,Nginx中根据需求配置调用不同的PHP-FastCGI端口或文件,即可实现不同版本PHP共存了。
下面记录简单的配置流程,基于已经安装了lnmp的debian环境。当前版本的PHP是5.3.8,位于/usr/local/php。
1.下载PHP-5.2.14及相关的FPM、autoconf组件:
mkdir ~/php5.2 cd ~/php5.2 wget -c http://museum.php.net/php5/php-5.2.14.tar.gz wget -c http://php-fpm.org/downloads/php-5.2.14-fpm-0.5.14.diff.gz
2.解压PHP-5.2.14,并打上PHP-FPM的补丁:
tar zxvf php-5.2.14.tar.gz gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
3.如果你已经通过lnmp安装,应该已经安装好了autoconf,如果没有,请自行下载并编译autoconf-2.13,然后设置autoconf环境变量:
export PHP_AUTOCONF=/usr/local/autoconf-2.13/bin/autoconf¬ export PHP_AUTOHEADER=/usr/local/autoconf-2.13/bin/autoheader
3.编译安装PHP-5.2.14在新的路径(/usr/local/php-5.2.14)下,注意–prefix、–with-config-file-path的路径,并且打开fastcgi和fpm选项:
cd php-5.2.14/ ./buildconf --force ./configure --prefix=/usr/local/php-5.2.14 --with-config-file-path=/usr/local/php-5.2.14/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fastcgi --enable-fpm make ZEND_EXTRA_LIBS='-liconv' make install
4.设置/usr/local/php-5.2.14/etc/php-fpm.conf,监听端口:
<value name="listen_address">127.0.0.1:9001</value>或者监听文件:
<value name="listen_address">/path/to/unix/socket</value>其他参数根据服务器环境和需求自行定制。
5.启动php-fpm,以后可以通过php-fpm进行管理:
/usr/local/php-5.2.14/sbin/php-fpm start
php-fpm支持的操作:
- start,启动PHP的FastCGI进程。
- stop,强制终止PHP的FastCGI进程。
- quit,平滑终止PHP的FastCGI进程。
- restart, 重启PHP的FastCGI进程。
- reload, 重新加载PHP的php.ini。
- logrotate, 重新启用log文件。
6.配置好PHP-5.2.14的php.ini,重新加载生效:
vi /usr/local/php-5.2.14/etc/php.ini /usr/local/php-5.2.14/sbin/php-fpm reload
7.修改nginx配置,对需要的服务配置使用PHP-5.2.14:
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include fcgi.conf;
}
当前目录下一个test.html,共有4个子目录t1,t2,x3,z4,目的是做test.html的硬链接到t1-z4四个目录中:
[saymoon@local ~]$ ls test.html t1 t2 x3 z4
我的实现方法:
ll | grep ^d |awk '{print $8}' | xargs -n 1 ln -f test.html
ll:列出当前目录中所有文件目录的详细列表; grep ^d:筛选出所有的目录; awk ‘{print $8}’:每行一条,打印出所有目录名; xargs -n 1:将awk输出的内容作为后面命令的参数传给后面的命令,每次传递一个目录名; ln -f test.html:将test.html硬链接到xargs传过来的目录中;
如果有其他更优的解决方案欢迎在下面留言哈!~
今天需要在 mac os 中自动替换某个文件中的特定字符串,最简单的方法当然是 sed,不过在实际使用中遇到一点小问题,在此记录。
sed 's/origin_text/replacement/g' /root/test.sql
执行上面这条命令会把 test.sql 中的 ‘origin_text’ 全部替换为 ’replacement’ 并将替换后的结果输出到终端,但是没有达到我们要替换源文件内容的目的,google 后发现 linux 中可以使用 -i 这个参数达到我们的目的。但是在 mac os 中尝试增加 -i 参数,会出现错误提示,并没有实现我们的需求
sed: 1: "/root/test.sql": bad flag in substitute command: 'n'
再次 google ,有人建议用这种方法实现:
sed 's/origin_text/replacement/g' /root/test.sql > /root/test.sql
这样执行后 test..sql 内容变为空,还是有问题。最终找到正确的实现方法:
sed -ig 's/origin_text/replacement/g' /root/test.sql
tablename: user 抽取数值:M 随机数种子:N(可选)
select * from user order by rand() limit M;
使用N作为随机种子数时,将rand()换成rand(N).
很早就开始用simplehtmldom这个php类来抓取分析html页面,它支持:
invalidHTML并提供非常简单的方式来操作HTML元素; 在HMTL页面上查找标签所使用的语法与jQuery(一个轻量级,实用的javascript框架)相似;
项目地址:http://simplehtmldom.sourceforge.net/
补充一点使用中遇到的问题及解决方法:
在一段代码中多次获取并分析页面,一定要记得每次使用完都释放内存,否则会导致内存泄露。或者Segmentation fault错误。
具体释放内存的方法要用simplehtmldom的clear方法,然后再unset:
$html = file_get_html(...); // do something... $html->clear(); unset($html);
众所周知的原因,google系的产品一直处于能用和不能用之间,尤其处于公司网络一个出口n多人用的情况下,GFW一会一封让人很难受。下面的方法其实很简单,通过设置host,把gg系的域名强制指向到gg北京的服务器,这样不出国就不会碰到FGW了,但是出于安全性考虑,特殊人士慎用。
203.208.39.22 webcache.googleusercontent.com 203.208.39.22 mail.google.com 203.208.39.22 www.google.com.hk 203.208.39.22 www.google.com 203.208.39.22 docs.google.com
在MAC OS下安装了MAMP Pro管理Apache、PHP、Mysql,简单的配置后通过浏览器访问php一切正常。
当尝试在console下运行php脚本时却提示pdo连接mysql.sock时出错。
PHP Error[2]: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)但是查过了MAMP自带的php5.2和php5.3的php.ini发现里面的几处默认mysql连接文件default_socket设置都没有问题,于是google之。发现原来命令行的php是用了MAC OS自带的php,需要修改MAC OS自带的php.ini。 具体步骤如下:
如果你已经有/private/etc/php.ini就不需要再拷贝一份php.ini.default出来了。
cd /private/etc/ sudo cp php.ini.default php.ini sudo vi php.ini
把php.ini里面所有的default_socket都改成MAMP的mysql.sock的正确位置即可。
pdo_mysql.default_socket=/Applications/MAMP/tmp/mysql/mysql.sock mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock mysqli.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock
// recursive make directory function for ftp
function make_directory($ftp_stream, $dir)
{
// if directory already exists or can be immediately created return true
if (ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true;
// otherwise recursively try to make the directory
if (!make_directory($ftp_stream, dirname($dir))) return false;
// final step to create the directory
return ftp_mkdir($ftp_stream, $dir);
}</p>
<p>function ftp_is_dir($ftp_stream, $dir)
{
// get current directory
$original_directory = ftp_pwd($ftp_stream);
// test if you can change directory to $dir
// suppress errors in case $dir is not a file or not a directory
if ( @ftp_chdir( $ftp_stream, $dir ) ) {
// If it is a directory, then change the directory back to the original directory
ftp_chdir( $ftp_stream, $original_directory );
return true;
} else {
return false;
}
}
发现wordpress默认的中文标签链接里的中文默认全部被urlencode了,本来想看看有没有插件解决这个问题,找了一下貌似没有现成的,我用的nginx网上现成的方法也不好使,自己动手hack,以后有空弄个插件实现。
修改文件:wp-includes/category-template.php :912行.
$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
改成:
$term_links[] = '<a href="' . urldecode($link) . '" rel="tag">' . $term->name . '</a>';