shell脚本处理技巧

来源:互联网 发布:淘宝导航条去掉 编辑:程序博客网 时间:2024/05/21 00:54

1、超时自动退出的方法

timeout()
{
waitfor=20
command=$*
$command &
commandpid=$!
( sleep $waitfor ; kill -9 $commandpid > /dev/null 2>&1 ) &
watchdog=$!
sleeppid=$PPID
wait $commandpid > /dev/null 2>&1
kill $sleeppid > /dev/null 2>&1
}

使用时,timeout functionA,即能使functionA运行20秒后强制退出。

2、获取随机数格式如:b8b192ae-22e6-7a46-2423-401f58f55740
mcookie|awk '{print substr ( $0,1,8 )"-"substr ( $0,9,4 )"-"substr ( $0,13,4 )"-"substr ( $0,17,4 )"-"substr ( $0,21,12 )}'

3、按时间顺序列出/home/user目录,并筛选出最新的20开头的目录名
ls -t /home/user | awk '$NF~/^20/{print $0}' | sed '2,$d'

 

未完待更新...