QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1040|回复: 2

显示时间和进程的小脚本

[复制链接]
发表于 2005-7-17 20:45:59 | 显示全部楼层 |阅读模式
我现在做一个化工的流程模拟,需要一个程序界面,我选择了ncurse来做。今天是学习的开始,首先搞了一下终端的控制序列。最后参考一个C程序写了这个小脚本,算是学习作业。大家看看。 另外我把终端的转义序列打包放在软件下载里面了,希望要的兄弟自己去下载。
[code:1]#!/bin/bash
# textsysinfo.sh: show the current time and the
#                 number of process running at this moment

# if press ctrl+c, deal the possible problem
trap "echo -n -e ^[[m^[8 ; clear ; exit 130" 2

# global variable
current_time=
num_of_running_process=
default_row=1
default_col=72

function get_current_time() {
  current_time=$(date +%T)
}

function get_num_of_running_process() {
  num_of_running_process=$(ls -l /proc | awk '{ print $9 }' | \
        egrep '^[0-9]+$' | wc -l)
}


# make the clock

row=${1:-${default_row}}
col=${2:-${default_col}}
newrow=$((row+1))       # determine where the clock displayed

while true
do
  get_current_time
  get_num_of_running_process
  echo -n -e "^[7^[[7m"
        # save the cursor and reverse vedio on
  echo -n -e "^[[${row};${col}H${current_time}^[[K"
        # move the cursor, print the current time and
        # erase from cursor to the end of the line
  echo -n -e "^[[${newrow};${col}H${num_of_running_process} ...^[[K"
        # move the cursor, print the number of the running process and
        # erase from cursor to the end of line
  echo -n -e "^[[m^[8"
        # all attributes off and restore the cursor
  sleep 1
done
[/code:1]
 楼主| 发表于 2005-7-17 22:04:59 | 显示全部楼层
补充一下,脚本里面的ESC转义在vim里面是这样作的:
首先按CTRL-V,再按ESC。
其实几乎所有的控制字符都是这样写入文本文件的。又比如要让echo输出ctrl-o,就是先用前面说的ESC转义,然后按ctrl-o就可以了。这个的用处就是:如果你用cat打开一个二进制文件。然后你的终端就全部是怪怪的符号了。这是因为文件中的ctrl-N字符使你的终端出错了。这个时候,用echo输出ctrl-o就恢复正常了。
当然,还有一种办法实现转义,就是用ESC的八进制表示:\033[
这些都是最近学到的。如果有什么错误,还请大家指出。。。  
回复

使用道具 举报

发表于 2005-7-20 13:05:20 | 显示全部楼层
已阅
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-10-3 06:38 , Processed in 0.039813 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表