Jun 4, 2013

Run Android Java console program

$ cat Hello.java
class Hello {
        public static void main(String[] args) {
                System.out.println("Hello World!");
        }
}

$ javac Hello.java

$ dx --version
dx version 1.7

$ dx --dex --output=Hello.dex Hello.class

$ ls
Hello.class  Hello.dex  Hello.java

$ adb push Hello.dex /data/local/tmp/

4.0 User:
$ adb shell dalvikvm -cp /data/local/tmp/Hello.dex Hello
Dalvik VM unable to locate class 'Hello'
java.lang.NoClassDefFoundError: Hello
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Hello
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
        ... 1 more

4.0 Eng:
$ adb shell dalvikvm -cp /data/local/tmp/Hello.dex Hello

Hello World!

May 8, 2013

Linux Shell Basic Test


1. 目录下有a.txt,b.txt,c.txt 把他们改名为a.txt.bak,b.txt.bak,c.txt.bak
   rename 's/$/.bak/' [a-c].txt

2. 在tmp目录下创建a0.txt,a1.txt,….a100.txt
   for x in $(seq 0 100); do touch a${x}.txt;done

3. 递归删除/var/www目录下的所有“.svn”目录
   find /var/www -type d -name '*.svc' -exec rm -rf {} \;

4. 查找并杀掉所有的nginx进程,进程名特征:“/usr/local/sbin/nginx”
   ps aux | grep '/usr/local/sbin/nginx' | awk '{print $2}' | xargs kill

5. /var/log/wtmp文件过大了,需要清空这个文件,如何做?
   # > /var/log/wtmp
   $ sudo bash -c '> /var/log/wtmp'

6. shell中有哪些特殊字符,以及他们的含义和应用场合
   file glob: ?, *, [x-y]
   variable: $
   quote: ', ", (), [], {}

7. 文件权限位的含义,如权限为601是什么意思?
   rxw - 412
   601 - rw-------x

8. bash下,命令行操作有哪些快捷键可以用?在哪里可以查到这些快捷键的说明?
   bash -o vi
   bash +o vi
   vi, emacas

9. 如何定义函数,函数如何接受参数,返回值?
   bash function define:
   [function] func_name()
   {
       function body
   }

   args:
   func_name args

   $1, $2 ... ${n}

   $*
   $@

   a)
   global variable

   b)
   echo xxx

   ret_val=$(func_name args)

   c)
   func_name variable_name

   func_name()
   {
        local __var=$1
        ret_val='test only'
        eval $__var=$ret_val
   }
 

Mar 4, 2012

3年过1天

  转眼在成都三年过一天了,09年3月3号,30多小时火车从北京到成都,sz一直把我送上车。

  3年后的同一天,sz也来到了成都,并打算在成都工作、生活了,一切源于去年十一他的成都几日游。

  有好朋友过来,当然是很开心的事情,周末两天吃饭、找房,拿下一个套三的房子,离现在住的地方很近,现在这个套二基本也确定有人要租了,感觉很好。

  这两天,带他看了些房子,他总会讲到华丽,北京的普通工薪族的生活看来比成都差太多。昨天去老码头吃火锅,我觉得一般,他们俩都感觉很好。

  就胡言乱语到这吧,睡觉,明天早点回来搬家。

Sep 5, 2011

Install driver for Atheros AR9271 on Ubuntu

    thinkpad x10 无线网卡太弱,买了块无线网卡,插入无法识别,打开带的光盘,里面只有windows驱动。

   了解了下,已经有人做好了安装工具 - ath9k_htc-installer,还是deb包,下载安装,然后,终端运行

$ ath9k_htc-installer

出来一个图形界面,直接安装即可,然后,插入 无线网卡 ,仍然无法识别
$ dmesg | tail
[  143.882952] usb 1-1: ath9k_htc: Firmware - htc_9271.fw not found
[  143.882983] ath9k_htc: probe of 1-1:1.0 failed with error -22

$ sudo wget  -P /lib/firmware http://wireless.kernel.org/download/htc_fw/1.3/htc_9271.fw

重新插上,小灯开始闪烁了。

从 ath9k_htc-installer 的运行log看,这个工具应该是修改.config文件,然后重新编译kernel了,所以,和http://wireless.kernel.org/en/users/Drivers/ath9k_htc 讲的一样,只是不用一般用户自己编译kernel。

Jul 7, 2011

test

just test from my android phone!

Jun 29, 2011

lidroid 2.6 replace TouchWiz40Launcher to TouchWiz30Launcher

Samsung I9000, sometimes TWL4 is very slow, so I want to use TWL3.

download lidroid 2.7 from here.
$ unrar -e lidroid-sgs-2.7.0-i9000.rar
$ tar xvf PDA-i9000.tar.md5
$ sudo mount -o loop factoryfs.rfs /mnt
$ adb remout
$ adb push /mnt/app/TouchWiz30Launcher.apk /system/app
$ adb shell reboot

May 25, 2011

set time zone on Ubuntu

Modify time zone on Ubuntu 10.04.

$ sudo tzselect

$ sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

$ sudo ntpdate cn.pool.ntp.org

# disable UTC
$ sudo sed -i 's/^UTC=yes/UTC=no/' /etc/rcS

setup vpn server on Amazon EC2

My environment: Amazon free EC2, Ubuntu 10.04 64bit

You should modify some hard code.

server side

  • $ cat setup-pptpd.sh
    #!/bin/bash
    
    # install pptpd
    apt-get install -y pptpd
    
    # config pptpd
    echo "localip 192.168.2.1" >> /etc/pptpd.conf
    echo "remoteip 192.168.2.2-10" >> /etc/pptpd.conf
    echo "ms-dns 172.16.0.23" >> /etc/ppp/options.pptpd
    echo "ms-dns 8.8.8.8" >> /etc/ppp/options.pptpd
    
    # generate user's password
    passwd=`openssl rand 10 -base64`
    if [ "$1" != "" ]
    then passwd=$1
    fi
    
    # setup authenticated user
    echo "user pptpd $passwd *" >> /etc/ppp/chap-secrets
    
    # for ipv4 forwarding
    sed -i 's/^#net.ipv4.ip_forward = 1/net.ipv4.ip_forward = 1/' \
     /etc/sysctl.conf
    sysctl -p
    
    # use iptabes to get the net forward
    iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -j SNAT \
     --to-source $(ifconfig  | grep 'inet addr:'| \
     grep -v '127.0.0.1' | cut -d: -f2 \
     | awk 'NR==1 { print $1}')
    iptables -A FORWARD -p tcp --syn -s 192.168.2.0/24 \
     -j TCPMSS --set-mss 1356
    service iptables save
    
    # make the iptables and pptpd auto start
    chkconfig iptables on
    chkconfig pptpd on
    
    service iptables start
    service pptpd start
    
    echo "VPN service is installed"
    echo "VPN username is user,VPN password is $passwd"
     

client side

$ cat setup-pptp-client.sh
#!/bin/bash

apt-get install -y pptp-linux

# modify those variable for your environment
vpn_server="1.2.3.4"
user="user"
passwd="secret"

echo "pty \"pptp $vpn_server --nolaunchpppd\"
name $user
remotename pptpd 
require-mppe-128
file /etc/ppp/options.pptp
ipparam pptpd" > /etc/ppp/peers/pptpd

echo "$user pptpd $passwd *" >> /etc/ppp/chap-secrets

echo "setup vpn client done."
echo -e "now, you shoule run:"
echo -e "\t$ pon pptpd # start pptpd"
echo -e "\t$ pon pptpd debug dump logfd 2 nodetach # debugging"
echo -e "\t$ poff pptpd # stop pptpd"
echo -e "\t$ ifconfig # check network config"

May 20, 2011

awk after the number of field or the value of field changed

# from awk manpage
References to non-existent fields (i.e. fields after $NF) produce the null-string.
However, assigning to a  non-exis‐tent field (e.g., $(NF+2) = 5) increases the value of NF,
creates any intervening fields with the null string as their value, and causes the value
of $0 to be recomputed, with the fields being separated by the value of  OFS.

References to negative numbered fields cause a fatal error.

Decrementing NF causes the values of fields past the new value to be lost,
and the value of $0 to be recomputed, with the fields being separated by the value of OFS.

Assigning a value to an existing field causes the whole record to  be  rebuilt  when  $0  is  referenced.
Similarly, assigning a value to $0 causes the record to be resplit, creating new values for the fields.


意思是:
1. 引用不存在的字段,会使增加NF的值,$0要根据OFS的值重新创建,中间的字段设置为空字符串, 然后根据FS的值重新对$0进行分割.
如:
$ echo 'a b c' | awk '{print $0; $6=60; OFS=":"; print $0}'
a b c
a:b:c:::60
可以清楚的看到, 第4,5字段的值都是空字符串.

2. 引用编号小于零的字段,会引起错误.
如:
$ cal | awk '{print $-1}'
awk: (FILENAME=- FNR=1) fatal: attempt to access field -1

3. 减少NF的值,编号大于NF的字段将丢失,$0根据OFS的值重新创建,然后根据FS的值重新分割记录.
如:
$ echo 'a b c d e f' | awk '{print $0; NF=3; print $0}'
a b c d e f
a b c

4. 给存在的字段赋值,使$0根据OFS的值重新创建,然后根据FS的值重新分割记录.
如:
$ echo 'a b c' | awk '{print $0; $1=$1;OFS=":"; FS=":"; print $0; print $1}'
a b c
a:b:c
a

5. 给$0赋值,$0现在是被赋予的新值,$0根据FS的值重新分割记录.
$ echo 'a b c' | awk '{print $0; $0=$0;OFS=":"; FS=":"; print $0; print $1}'
a b c
a b c
a

echo 'a b c' | awk '{print $0; $0="c d e"; print $0; print $1}'
a b c
c d e
c

note:
  a. 4, 5是ChinaUnix.net Shell 编程大赛的第8题.
  b. 可以发现当字段个数或者字段被修改后,都要根据OFS的值进行$0重建, 然后,$0根据FS的值重新分割.
  c. 只修改$0, 使$0用新值根据FS的值重新分割.

May 9, 2011

build Android 2.3, ld cannot find -lz

Build Android 2.3 (gingerbread), report error: /usr/bin/ld: skipping incompatible /usr/lib/libz.so when searching for -lz /usr/bin/ld: skipping incompatible /usr/lib/libz.a when searching for -lz /usr/bin/ld: cannot find -lz collect2: ld returned 1 exit status fix it: $ sudo apt-get install lib32z-dev