18 05/2013

mac 查看目录文件数

最后更新: Sat May 18 2013 17:37:39 GMT+0800

查看本目录文件数

ls -1 | wc -l

查看 本目录包括子目录 文件数

find . -type f | wc -l

18 05/2013

mac 修改终端标题

最后更新: Sat May 18 2013 17:19:25 GMT+0800

在 Ternimal 终端按 +i 调出 inspector,在这里可以手工修改窗口标题和 tab 标题。

但我经常需要在多个终端跑服务,我希望脚本能够自动改标题。这样我就能知道每个终端在干什么。

vim ./bash_profile 加入下面函数

function tabname {
  printf "\e]1;$1\a"
}

function winname {
  printf "\e]2;$1\a"
}

以后就可以

tabname "test" 改标签标题

winname "test" 改窗口标题

我的 node www 脚本

#!/bin/bash 

cd ./public
source ~/.bash_profile
tabname "WWW:8888"
node ../server.js

bash 不能自动加载 ~/.bash_profile,所以需要 source ~/.bash_profile 引入。

只修改 tab 标题的话,你也可以

title='My first title'
echo -n -e "\033]0;$title\007"

18 05/2013

mac 批处理改名

最后更新: Sat May 18 2013 19:03:13 GMT+0800

比如要把所有 .htm 改为 .html

for i in *.htm;do mv "$i" "${i/.*/.html}";done

注意:引号是为了处理 带空格的文件名。我一般用下面命令先测试:

for in in *.htm;echo ${i/.*/.html};done

${} 语法(Parameter Expansion)

语法:

${parameter/pattern/string} 或者 ${parameter//pattern/string}

  • 第一个匹配第一次(/);
  • 二匹配每次。(//)
  • pattern中 # 表示开始
  • pattern中% 表示结尾
  • pattern中 空字符串不处理
  • @ or * (没看懂)

The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pat- tern against its value is replaced with string. In the first form, only the first match is replaced. The second form causes all matches of pattern to be replaced with string. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or , the substitution operation is applied to each positional parameter in turn, and the expan- sion is the resultant list. If parameter is an array variable subscripted with @ or , the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

更多例子

比如要把

image0001.png image0002.png ...

改为

0001.png 0003.png ...

for f in *.png; do mv "$f" "${f#image}"; done

用 sed

ls | sed -n 's/\(.*\)\(\.htm\)/mv "\1\2" "\1.html"/p'|sh

特别注意:ls 必须是默认的 纯文件名输出,而不是 alias 以后的 alias ls="ls -lh" 之类! 最好先用 echo 先查看

或者包括当前子目录都转

find . -type f | sed -n 's/\(.*\)\(\.htm\)/mv "\1\2" "\1.html"/p'|sh

解释:

这种正则比较难读。太严格,其实主要就是括号 () 必须转义 ()

sed -n 's/AAA/BBB/p'

s (Substitute)表示搜索;把 AAA 换成 BBB;最后的 p 表示 print。疯狂的 sed

延伸

a=aaabcd
david@gsmac ~/hexo/tt7$ echo $a | tr 'a' 'k'

返回 kkkbcd

echo "abcabc" | sed 's/a/x/g'

返回 xbcxbc

18 05/2013

hexo 大限将至

最后更新: Sat May 18 2013 17:41:07 GMT+0800

乱了!又乱了!

迅速到达 hexo 大限!大约 300-400 个文件左右!hexo server 或者 generate 都会报错错误!没办法,暂时只好把 labs 和 pics 挪出来,然后本地另起www。但这样没办法实时跟踪文件变化了。站内所有 labs/ *.html 的链接全部实效,需要再次修复!

python www 不能处理 a/ => a/index.html

另起(public) node ../server.js (但这和 hexo server 一样了。)不一样在于 labs pics 目录的问题!!!!hexo server 是实时跟踪 source 而 我自己的 node server.js 不跟踪,只是 www,generate 另外操作。

修改 graceful-fs

// fs.MIN_MAX_OPEN = 64 // fs.MAX_OPEN = 1024

没用。

iphone5 分辨率

  • iPhone 1 – 3gs: 320 x 460 px
  • iPhone 4 – 4s: 640 x 920 px
  • iPhone 5: 640 x 1096 px

但在 safari 中报告是 :320 width and 548 height,

18 05/2013

jqm 自定义样式

最后更新: Sat May 18 2013 18:01:54 GMT+0800

jqm 默认 a href= 是 ajax 载入链接。载入的只是 role=content 的部分。所以类似

body#home ul.myUl {....}

设置自己的 css 没用!因为 ajax load 过来并且渲染的内容从下面的 role=content 才开始!

一旦刷新页面,很尴尬,载入的是 当前页面的 js css。

用 a rel="external" 或者 a data-ajax="false" 恢复传统链接方式。但这样就没有页面转换效果了。

手机测试,必须清缓存!!

推荐下载 flatui jqm 项目。带源文件,自定义 y z.styl 文件,然后用 grunt 跑出 新的 AAA.min.css。

16 05/2013

html5 本地存储

最后更新: Thu May 16 2013 11:17:45 GMT+0800

Chrome 支持

  • Cookies
  • Local Storage
  • Session Strorage
  • Web SQL
  • IndexedDB

Local Storage

浏览器都支持,简单的 key:value,不能查询,大小 5M。

检查是否支持

window.localStorage

设值

localStorage.setItem('name','guoshuang') 或者 localStorage.['name']='guoshuang')

取值

localStorage.getItem('name') 或者 localStorage['name']

删除

localStorage.removeItem('name')for

遍历

for(var i=0; i< localStorage.length;i++){
var key = localStorage.key(i);  
    console.log(key + ":" + localStorage[key]);  
} 

Web SQL

建库、建表、插值

var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);  
var msg;  
db.transaction(function (tx) { 
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');  
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');  
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');  

执行查询

db.transaction(function (tx) { 
 tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) { 
  var len = results.rows.length, i;  
  msg = "<p>Found rows: " + len + "</p>";  
  document.querySelector('#status').innerHTML +=  msg;  
  for (i = 0; i &lt; len; i++){ 
    msg = "<p><b>" + results.rows.item(i).log + "</b></p>";  
    document.querySelector('#status').innerHTML +=  msg;  
  } 
}, null);  
});  

删除表

at fs.js:207:20

indexedDB

window.indexedDB.open("CandyDB","My candy store database");

Session Strorage Web SQL 就是 sqlite,支持 sql 语句 IndexedDB 平面数据库 key/value 加简单索引

15 05/2013

sublime 保存的同时编译

最后更新: Wed May 15 2013 15:52:08 GMT+0800

有时候需要 save 的时候自动 build(jade,less),有时候又不需要,这个插件刚好:

安装 SublimeOnSaveBuild,然后设置

Preferences - Package-settings - SublimeOnSaveBuild - settings-user

{
    "filename_filter": "\\.(jade|less)$",
    "build_on_save": 1
}

filename_filter 处理哪些后缀名 build_on_save 是否在保存的同时编译