一个rails2.x 的项目,需要迁移到rails3.x , ruby 1.9.2的编码问题,需要在rb文件头添加指定编码, 常见指定方式如下:
#coding:utf-8 #encoding:utf-8 # -*- coding: utf-8 -*- # -*- encoding: utf-8 -*-
那么多rb文件总不能一个一个加吧,写个shell解决之!!!
上次写了安装所有gems,如果系统已经安装了 就跳过安装的 shell脚本,其中最主要的使用了 字符串的截取,发现bash里截取字符串非常之麻烦,请教了 一个 搞嵌入式的 家伙(同学) @liuqun,给了我 几个指点,发现原来也有很多办法,只是不是那么顺手.
上次的那个 shell 脚本:
#!/bin/bash # 安装所有的gems,如果已经安装了就不安装 cd /usr/local/system/ruby/lib/ruby/gems/1.8/cache for i in `ls`;do gem=`echo $i | awk -F'-' '{print $1}'` version=`echo $i | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//'` is_gem_exist=`gem list $gem -v=$version` if [ -z "$is_gem_exist" ]; then # 注意[] 内部两边留空格 `gem install $i` else echo "$i have installed" fi done
1:
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//' 0.4.1
sed 's/^-//' 把开头的 - 变为空
sed 's/.gem//' 是把.gem 变为空
2,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem"; STR=${STR##*-}; echo ${STR%\.*} 2.3.5
${STR%\.*} 从右向左截取第一个 . 后的字符串
3,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem"; echo ${STR:13:5} 2.3.5
4,
wxianfeng@ubuntu:~$ echo actionmailer-2.3.5.gem | sed 's/[^0-9]*\(.*\).gem/\1/' 2.3.5
5,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem" ; echo ${STR:13:5} 2.3.5
6,
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | awk '{print substr($0,13,5)}' 0.4.1
13 是偏移量
5 是 长度
7,
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | cut -d- -f 3 | cut -d . -f 1-3 0.4.1
-f 3 是第三断
-f 1-3 第一到第三断
see: http://tech.foolpig.com/2008/07/09/linux-shell-char/
http://linux.sheup.com/linux/linux5426.htm
你知道你目前的项目代码有多少行吗?有多少个文件吗?有多少个文件夹吗?。。。^o(∩∩)o…哈哈,这个很简单,一句shell就搞定了,效率还非常之高:
1,统计你项目的.rb 结尾的代码行数,去掉空格
find . -type f -name "*.rb" | xargs cat | grep -v ^$ | wc -l
当然你还可以添加去掉注释
2,统计项目文件所有行数,包括空行
find . -type f | xargs cat | wc -l
3,统计项目有多少个文件
find . -type f | wc -l
4,统计项目有多少个文件夹
find . -type d | wc -l
5,找出项目下.rb 结尾的文件行数最大的一个(由小到大排序)
find . -type f -name "*.rb" | xargs wc -l | sort -n
6,自由发挥
总之find 命令结合 xargs 很强大的说
这些都是基本的用法,例如上面想得到tar.gz 等压缩包的内容就难了,那么可以借助cloc(perl库)得到更详细的信息,各种文件类型的行数,注释,空行等等信息:
ubuntu 安装:
sudo apt-get install cloc
使用:
wxianfeng@ubuntu:/usr/local/system/projects/redmine_1_0_3$ cloc . 6455 text files. 3119 unique files. 1035 files ignored. http://cloc.sourceforge.net v 1.09 T=13.0 s (204.8 files/s, 25293.4 lines/s) ------------------------------------------------------------------------------- Language files blank comment code scale 3rd gen. equiv ------------------------------------------------------------------------------- Ruby 2076 33371 44800 178357 x 4.20 = 749099.40 YAML 197 1299 691 44512 x 0.90 = 40060.80 Javascript 105 1963 2111 12461 x 1.48 = 18442.28 Ruby HTML 241 716 29 4862 x 4.00 = 19448.00 CSS 22 366 102 1889 x 1.00 = 1889.00 HTML 15 120 3 627 x 1.90 = 1191.30 Perl 2 80 98 233 x 4.00 = 932.00 SQL 3 17 2 96 x 2.29 = 219.84 XML 1 0 0 9 x 1.90 = 17.10 ------------------------------------------------------------------------------- SUM: 2662 37932 47836 243046 x 3.42 = 831299.72 -------------------------------------------------------------------------------
另外 rails 中已经封装了一个rake 任务,用来统计的,瞧瞧:
wxianfeng@ubuntu:/usr/local/system/projects/blog.wxianfeng.com$ rake stats (in /usr/local/system/projects/blog.wxianfeng.com) +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 2422 | 1978 | 36 | 215 | 5 | 7 | | Helpers | 858 | 712 | 0 | 103 | 0 | 4 | | Models | 3360 | 2661 | 55 | 447 | 8 | 3 | | Libraries | 1203 | 884 | 30 | 143 | 4 | 4 | | APIs | 436 | 353 | 17 | 23 | 1 | 13 | | Functional tests | 16 | 12 | 2 | 0 | 0 | 0 | | Unit tests | 16 | 12 | 3 | 0 | 0 | 0 | | Model specs | 2094 | 1680 | 1 | 24 | 24 | 68 | | View specs | 249 | 200 | 0 | 12 | 0 | 14 | | Controller specs | 3507 | 2734 | 2 | 37 | 18 | 71 | | Helper specs | 53 | 39 | 0 | 0 | 0 | 0 | | Library specs | 15 | 11 | 0 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 14229 | 11276 | 146 | 1004 | 6 | 9 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 6588 Test LOC: 4688 Code to Test Ratio: 1:0.7
see:
http://bbs.chinaunix.net/viewthread.php?tid=1665204
http://garfileo.is-programmer.com/2010/6/11/lines-counting-using-cloc.18828.html
写了个shell脚本 监控网站 是否能ping通(前提是你iptables设置允许ping),还有测试 访问网站首页 返回的状态 是否 是 200 ,如果出现异常会发邮件 和 短信,ping域名不通了 , 说明你的网关到ISP那的网络不通 ,ping IP 地址不通 , 说明你的网关到你的服务器的网络有问题 , ping 域名不通 , ping IP地址通, 那就应该是DNS解析的问题了。
shell脚本:
#!/bin/sh if [ $# -ne 1 ] # -ne 不等于 then echo "Usage: $0 <URL>"; # $0 得到当前文件名 exit; fi if ping -c 2 $1 # $1 得到第一个参数 then echo "server is running" else echo "$1 is down" | mail -s "$1 is down" wang.fl1429@gmail.com curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20is%20down" fi response=$(curl -s -I -L $1 | grep HTTP); status=${response#* }; # Strip off characters up to the first space status=${status:0:3}; # Just use the 3 digit status code if [ "$status" != "200" ] then echo "Error fetching $1. Status code $status" | mail -s "$1 response code is not 200" wang.fl1429@gmail.com; curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20response%20status%20code%20$status" fi
或者
#!/bin/sh if [ $# -ne 1 ] # -ne 不等于 then echo "Usage: $0 <URL>"; # $0 得到当前文件名 exit; fi ping -c 2 $1 &>/dev/null # $1 得到第一个参数 , -c 2 是发送两次数据包 , &>/dev/null 输出内容null if [ $? = 0 ] # $?输出命令退出代码:0为命令正常执行,1-255为有出错 then echo "server is running" else echo "$1 is down" | mail -s "$1 is down" wang.fl1429@gmail.com curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20is%20down" fi response=$(curl -s -I -L $1 | grep HTTP); status=${response#* }; # Strip off characters up to the first space status=${status:0:3}; # Just use the 3 digit status code if [ "$status" != "200" ] then echo "Error fetching $1. Status code $status" | mail -s "$1 response code is not 200" wang.fl1429@gmail.com; curl "http://sms.api.bz/fetion.php?username=15001100000&password=12345&sendto=15001100000&message=$1%20response%20status%20code%20$status" fi
让shell脚本添加 可执行
chmod +x monitor_domain.sh
添加 crontab 任务
>crontab -e # m h d mon dow command */10 * * * * /usr/local/system/shell/monitor_domain.sh "blog.wxianfeng.com" # 每10分钟运行一次
发短信 api 用的是别人的 , 以后自己可以搭一个 API , 发短信除了这个办法 , 还有一个办法 , 申请一个 139的email , 139 的email可以设置邮件 短信提醒 , 同时收到邮件 和 短信,不错 ,
我的centos 5.5 在执行 mail 命令时提示找不到 , 需要安装mailx 库解决:
yum install mailx
see:
http://www.coderholic.com/server-status-checker-shell-script-2/
http://blog.s135.com/fetion_api/