获取本机内网IP

/sbin/ifconfig | grep "inet addr" -m 1 | cut -d ":" -f 2 | cut -d " " -f 1

输出unix时间戳

date +%s

从路径中获取文件名

basename /usr/bin/sort
    Output "sort". 
basename include/stdio.h .h
    Output "stdio".

遍历指定路径文件

file_list=`ls dir`
for file in $file_list
do
	echo $file
done

gdb attach指定进程名的进程

#! /bin/sh

if [ $# -lt 1 ]; then
    echo "please give a name"
    exit 1
fi

ENTRY=`ps x | grep $1 -m 1`
PID=`echo $ENTRY | cut -d " " -f 1`
while true; do
    echo -e "The following process will be attached by gdb\n\n  ${ENTRY}\n"
    read -p "Continue? (y or n) " confirm
    case $confirm in
        [y] )  gdb attach $PID; break;;
        [n] )  exit 1;;
        *) echo "Please answer y or n.";;
    esac
done

批量查找替换文本字符串

将当前目录下,所有的.h文件中的wrong替换为right

find . -type f -name "*.h" | xargs perl -pi -e 's|wrong|right|g'

借助expect实现自动scp

#!/bin/sh

help()
{
    echo "Usage: $0 local dest"
}

if [ $# != 2 ]; then
    help
    exit 1
fi

echo "[!] uploading, please wait..."

user=root
passwd=abc123
ip=1.2.3.4
local=$1
dest=$2

expect -c "
    set timeout 100000;
    spawn scp -r ${local} ${user}@${ip}:${dest}
    expect {
        \"*assword\" {send \"${passwd}\r\";}
        \"yes/no\" {send \"yes\r\"; exp_contine;}
    }
expect eof"

echo "[ok] uploaded."