Chinaunix首页 | 论坛 | 博客
  • 博客访问: 741720
  • 博文数量: 130
  • 博客积分: 2951
  • 博客等级: 少校
  • 技术积分: 1875
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 18:32
文章分类

全部博文(130)

文章存档

2013年(1)

2012年(129)

分类: Python/Ruby

2012-10-30 17:44:51

先看一个shell,功能是查看有哪些用户log on或者out了

点击(此处)折叠或打开

  1. #!/bin/sh
  2. #
  3. # watch.sh - a simple version of the watch utility, written in sh
  4. #
  5.     who | sort > prev        # 第一次查看,结果放到prev文件中
  6.     while true            
  7.     do
  8.         sleep 30        # 等一会儿
  9.         who | sort > current    # 再次查看,结果放到current文件中
  10.         echo "Logged out:"    
  11.         comm -23 prev current    # -23表示不显示第二个文件独有的,也不显示两个文件共有的,所以这里只显                                 #示第一个文件prev中有的用户,也就是说明这些用户当前已经不在线了,即lo                                 #g out了
  12.         echo "Logged in:"   
  13.         comm -13 prev current    # -13表示不显示第一个文件独有的,也不显示两个文件共有的,所以这里只                                   #显示第二个文件current中有的用户,也就是说明这些用户是新来的,即log                                  #on了

  14.         mv current prev          # 把当前文件current命名为prev,下次循环时,这个prev就表示之前存在的                                  # 用户了。
  15.     done
现在要增加一些需求,要求从参数可以传入一个用户列表文件,针对这些用户监控log on和out

  1. #!/bin/sh
  2. #
  3. # note: the list of users has to have one name per line
  4. #
  5. #
  6.     FILE=$1                                        #$1表示第一个参数,$0表示命令本身
  7.     if test -z "$FILE"                             #-z表示文件名长度如果为0,则test判断结果为真
  8.     then
  9.         if test ! -f $HOME/.watchees               #如果没传入文件,则判断$HOME下面是否有规则文件.watchees
  10.         then
  11.             echo "usage: watch.sh userlist"        #如果不存在,则报错
  12.             exit 1
  13.         fi
  14.     fi
  15.     NAMES=`wc -w < $FILE`                          #获取文件中字符串个数
  16.     LINES=`wc -l < $FILE`                          #获取文件长度
  17.     if test $NAMES -ne $LINES ; then               #如果长度不等于字符串个数, 因为事先要求文件中每个用户名                                                    #就占一行,所以用户名总数就等于行数
  18.         echo "file $FILE must have one name per line" #报错
  19.         exit 2
  20.     fi

  21.     # file has to be sorted, because we plan to use join
  22.     sort -o $FILE $FILE                            #对用户名排序,并输出到本文件。

  23.     # main loop now
  24.     who | sort | join - $FILE >prev                # 这里主要是有个join操作,"-"表示从标准输入读入,也就是从                                                   #sort后的结果输入,然后和$FILE中的内容进行join, 把两个文                                                   #件相同的部分合成一行
  25.     while true            #
  26.     do
  27.         sleep 10                                   # wait a while
  28.         who | sort | join - $FILE > current        # get current user list
  29.         echo "Logged out:"                         # print header
  30.         comm -23 prev current                      # and results
  31.         echo "Logged in:"                          # header
  32.         comm -13 prev current                      # and results
  33.         mv current prev                            # make now past
  34.     done
看一个join的例子
[lizhuohua@lizhuohua-phy Program]$ cat lizhuohua
11
22
33
44
[lizhuohua@lizhuohua-phy Program]$ cat lizhuohua1
11
22
44
55
[lizhuohua@lizhuohua-phy Program]$ join lizhuohua lizhuohua1
11
22
44
所以上面脚本中join的作用就是只把文件中有的用户名从who的结果中过滤出来,而不是监控所有用户的登录状况。

现在再增加一些需求,仅仅当有新用户登录或注销,才打印结果,而不是每个循环都打印结果

  1. #!/bin/sh
  2. #
  3. # note: the list of users has to have one name per line
  4. #
  5.     FILE=$1
  6.     if test -z "$FILE"
  7.     then
  8.         if test ! -f $HOME/.watchees
  9.         then
  10.             echo "usage: watch.sh userlist"
  11.             exit 1
  12.         fi
  13.     fi
  14.     NAMES=`wc -w < $FILE`
  15.     LINES=`wc -l < $FILE`
  16.     if test $NAMES -ne $LINES ; then
  17.         echo "file $FILE must have one name per line"
  18.         exit 2
  19.     fi

  20.     # file has to be sorted, because we plan to use join
  21.     sort -o $FILE $FILE

  22.     # create a scratch file
  23.     SCRATCH=`mktemp /tmp/watch.XXXXXX`                          #create一个tmp文件
  24.     trap "rm -f $SCRATCH; exit 0" 0 2 3 15                      #设置一个信号捕捉器,当有0(正常退出), 2(                                                                #SIGINT), 3(SIGQUIT),15(SIGTERM)信                                                                   #号到来时候,执行命令,删除临时文件。

  25.     # main loop now
  26.     who | sort | join - $FILE >prev   
  27.     while true          
  28.     do
  29.         sleep 10              
  30.         who | sort | join - $FILE > current  
  31.         # compute results into scratch fle
  32.         comm -23 prev current> $SCRATCH                         #把比较结果写入临时文件

  33.         # and only print the file if non-empty
  34.         if test -s $SCRATCH ; then                              #判断临时文件的大小是否为0,如果大于0,说明                                                                 #有新用户登录,再输出
  35.             echo "Logged out:"      
  36.             cat $SCRATCH           
  37.         fi

  38.         # same logic for other one
  39.         comm -13 prev current > $SCRATCH  
  40.         if test -s $SCRATCH ; then                              #判断临时文件的大小是否为0,如果大于0,说明                                                                 #有用户注销,再输出
  41.             echo "Logged in:"       
  42.             cat $SCRATCH          
  43.         fi
  44.         mv current prev              
  45.     done

现在再增加一些需求。who命令除了列出用户名,还列出来登录时间,终端名称等。这些信息都是不需要的,去掉这些信息

  1. #!/bin/sh
  2. #
  3.     FILE=$1
  4.     if test -z "$FILE"
  5.     then
  6.         if test ! -f $HOME/.watchees
  7.         then
  8.             echo "usage: watch.sh userlist"
  9.             exit 1
  10.         fi
  11.     fi
  12.     NAMES=`wc -w < $FILE`
  13.     LINES=`wc -l < $FILE`
  14.     if test $NAMES -ne $LINES ; then
  15.         echo "file $FILE must have one name per line"
  16.         exit 2
  17.     fi

  18.     # file has to be sorted, because we plan to use join
  19.     sort -o $FILE $FILE

  20.     # create a scratch file
  21.     SCRATCH=`mktemp /tmp/watch.XXXXXX`
  22.     trap "rm -f $SCRATCH; exit 0" 0 2 3 15

  23.     # main loop now
  24.     # first get initial list
  25.     who | cut -d" " -f1 | sort -u | join - $FILE >prev    #cut命令 -d指出分割符是空格 -f指出需要的字段是第一                                                          #个,比如who的结果是“(unknown) tty2 2012-10-29 23:                                                          #30 (:1)”, cut处理后就是"(unkonwn)"
  26.     while true          
  27.     do
  28.         sleep 10                
  29.         who | cut -d" " -f1 | sort -u | join - $FILE > current    
  30.         # compute results into scratch fle
  31.         comm -23 prev current> $SCRATCH

  32.         # and only print the file if non-empty
  33.         if test -s $SCRATCH ; then
  34.             echo "Logged out:"        
  35.             cat $SCRATCH            
  36.         fi

  37.         # same logic for other one
  38.         comm -13 prev current > $SCRATCH    
  39.         if test -s $SCRATCH ; then
  40.             echo "Logged in:"        
  41.             cat $SCRATCH            
  42.         fi
  43.         mv current prev                
  44.     done

再增加一个需求, 不再使用prev和current文件,而都采用临时文件,并且程序退出时候删除临时文件。

  1. #!/bin/sh
  2. #
  3. # note: the list of users has to have one name per line
  4. #
  5.     FILE=$1
  6.     if test -z "$FILE"
  7.     then
  8.         if test ! -f $HOME/.watchees
  9.         then
  10.             echo "usage: watch.sh userlist"
  11.             exit 1
  12.         fi
  13.     fi
  14.     NAMES=`wc -w < $FILE`
  15.     LINES=`wc -l < $FILE`
  16.     if test $NAMES -ne $LINES ; then
  17.         echo "file $FILE must have one name per line"
  18.         exit 2
  19.     fi

  20.     # file has to be sorted, because we plan to use join
  21.     sort -o $FILE $FILE

  22.     # create a scratch file
  23.     SCRATCH=`mktemp /tmp/watch.XXXXXX`
  24.     CURR=`mktemp /tmp/watch.XXXXXX`                      #用临时文件代替current文件
  25.     PREV=`mktemp /tmp/watch.XXXXXX`                      #用临时文件代替prev文件
  26.     trap "rm -f $SCRATCH $CURR $PREV; exit 0" 0 2 3 15   #程序退出时候删除所有临时文件。

  27.     # main loop now
  28.     # first get initial list
  29.     who | cut -d" " -f1 | sort -u | join - $FILE >$PREV
  30.     while true            # true is a program: exit(0);
  31.     do
  32.         sleep 10                # wait a while
  33.         who | cut -d" " -f1 | sort -u | join - $FILE > $CURR    
  34.         # compute results into scratch fle
  35.         comm -23 $PREV $CURR> $SCRATCH

  36.         # and only print the file if non-empty
  37.         if test -s $SCRATCH ; then
  38.             echo "Logged out:"       
  39.             cat $SCRATCH           
  40.         fi

  41.         # same logic for other one
  42.         comm -13 $PREV $CURR > $SCRATCH   
  43.         if test -s $SCRATCH ; then
  44.             echo "Logged in:"       
  45.             cat $SCRATCH           
  46.         fi
  47.         mv $CURR $PREV              
  48.     done




阅读(1729) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

最大行业软件2012-11-03 14:50:22

ISOGRAPH_AVSIM_V10.0

ADINA系列产品:
ADINA.V8.8.1

CROCODILE.TECHNOLOGY.V609  系统控制仿真软件

Esteco.modeFRONTiER.v4.3.0 20101110 优化设计

ETA系列:
ETA.CAD.Translator.v1.200704
ETA.VPG.v3.3 汽车整车仿真软件
ETA_DYNAFORM_V5.6(多语言版,包括中文)
(DYNAFORM 是ETA开发的用于板料成形模拟的软件包。针对板料冲压的工艺特点,开发了方便高效的前后处理器,极大地缩短

了模型准备的周期。求解器采用LS-DYNA,基于增量法有限元理论,分析结果准确可靠。DYNAFORM可以模拟预压边、拉延、翻

边、弯曲、多工步成形等工艺过程,能够预测板料起皱、拉裂、回