写logfile (ex41.sh)

#!/bin/bash

# Generates a log file in current directory
# from the tail end of /var/log/messages.

# Note: /var/log/messages must be world readable
# if this script invoked by an ordinary user.
#         #root chmod 644 /var/log/messages

LINES=5

( date; uname -a ) >>logfile
# Time and machine name
echo ---------------------------------------------------------- >>logfile
tail -n $LINES /var/log/messages | xargs | fmt -s >>logfile
echo >>logfile
echo >>logfile

exit 0

#  Note:
#  ----
#  As Frank Wang points out,
#+ unmatched quotes (either single or double quotes) in the source file
#+ may give xargs indigestion.
#
#  He suggests the following substitution for line 15:
#  tail -n $LINES /var/log/messages | tr -d "\"'" | xargs | fmt -s >>logfile



#  Exercise:
#  --------
#  Modify this script to track changes in /var/log/messages at intervals
#+ of 20 minutes.
#  Hint: Use the "watch" command. 
[root@localhost shell]# ./ex41.sh
[root@localhost shell]# cat logfile
2012年 10月 30日 星期二 20:34:40 CST
Linux localhost.localdomain 2.6.25-14.fc9.i686 #1 SMP Thu May 1 06:28:41 EDT 2008 i686 i686 i386 GNU/Linux
----------------------------------------------------------
Oct 30 10:22:18 localhost tpvmlpd[2223]: aborting Oct 30 10:24:10
localhost kernel: VMCIUtil: Updating context id from 0xc65b48c5 to
0xc65b48c5 on event 0. Oct 30 11:04:40 localhost kernel: VMCIUtil:
Updating context id from 0xc65b48c5 to 0xc65b48c5 on event 0. Oct 30
12:33:29 localhost kernel: VMCIUtil: Updating context id from 0xc65b48c5
to 0xc65b48c5 on event 0. Oct 30 16:13:34 localhost kernel: VMCIUtil:
Updating context id from 0xc65b48c5 to 0xc65b48c5 on


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值