linux shell 脚本攻略学习4

1.cat命令详解

cat 是concatnate(拼接)的简写.

语法:

cat file1 file2 file3 ....

作用:将文件内容拼接在一起进行输出

具体应用:

1).压缩空白行

加上-s参数压缩连续的空白行

amosli@amosli-pc:~/learn$ cat mutil_blank.txt 
this

is 






blank!
amosli@amosli-pc:~/learn$ cat -s mutil_blank.txt #压缩连续的空白行
this

is 

blank!

移除所有的空白行,

tr -s "\n"
amosli@amosli-pc:~/learn$ cat mutil_blank.txt | tr -s "\n"
this
is 
blank!

2).将制表符显示为^|

-T 参数

amosli@amosli-pc:~/learn$ cat file.py 
def function():
    var = 5;
        next = 6;
    third = 7;

amosli@amosli-pc:~/learn$ cat -T file.py # 加上参数-T,即可将制表符标示出来!
def function():
^Ivar = 5;
^I^Inext = 6;
^Ithird = 7;

3).显示行号

-n参数

amosli@amosli-pc:~/learn$ cat -n file.py #加上参数-n即可显示行号
     1    def function():
     2        var = 5;
     3            next = 6;
     4        third = 7;
     5    

 

2.录制与回放终端会话

amosli@amosli-pc:~/learn$ script -t 2> timing.log -a output.session#开始录制
Script started, file is output.session
amosli@amosli-pc:~/learn$ hello1
No command 'hello1' found, did you mean:
 Command 'hello' from package 'hello-debhelper' (main)
 Command 'hello' from package 'hello' (main)
hello1: command not found
amosli@amosli-pc:~/learn$ who
amosli   tty7         2013-12-19 22:58
amosli   pts/1        2013-12-19 22:59 (:0.0)
amosli   pts/2        2013-12-19 23:16 (:0.0)
amosli@amosli-pc:~/learn$ time

real    0m0.000s
user    0m0.000s
sys    0m0.000s
amosli@amosli-pc:~/learn$ date
2013年 12月 20日 星期五 01:06:41 CST
amosli@amosli-pc:~/learn$ exit
exit #结束录制
Script done, file is output.session

播放录制:

amosli@amosli-pc:~/learn$ scriptreplay timing.log output.session #按播放命令序列输出
amosli@amosli-pc:~/learn$ hello1
No command 'hello1' found, did you mean:
 Command 'hello' from package 'hello-debhelper' (main)
 Command 'hello' from package 'hello' (main)
hello1: command not found
amosli@amosli-pc:~/learn$ who
amosli   tty7         2013-12-19 22:58
amosli   pts/1        2013-12-19 22:59 (:0.0)
amosli   pts/2        2013-12-19 23:16 (:0.0)
amosli@amosli-pc:~/learn$ time

real    0m0.000s
user    0m0.000s
sys    0m0.000s
amosli@amosli-pc:~/learn$ date
2013年 12月 20日 星期五 01:06:41 CST
amosli@amosli-pc:~/learn$ exit
amosli@amosli-pc:~/learn$ 

 

进行多个用户之间进行广播视频会话:打开两个终端

(1)在终端1中输入以下命令:

mkfifo scriptfifo

(2)在终端2中输入以下命令:

cat scriptfifo

(3)返回终端1,输入以下命令:

script -f scriptfifo
commands...

这样就实现了广播,在线课堂类的作用,非常神奇!!

我自己实验的输出:

终端1:
amosli@amosli-pc:~$ mkfifo scriptfifo
amosli@amosli-pc:~$ script -f scriptfifo 
Script started, file is scriptfifo
amosli@amosli-pc:~$ who
amosli   tty7         2013-12-19 22:58
amosli   pts/1        2013-12-19 22:59 (:0.0)
amosli   pts/2        2013-12-20 01:14 (:0.0)
amosli   pts/4        2013-12-20 01:16 (:0.0)
amosli   pts/5        2013-12-20 01:20 (:0.0)
amosli   pts/6        2013-12-20 01:20 (:0.0)
amosli@amosli-pc:~$ 

终端2:
amosli@amosli-pc:~$ cat scriptfifo
Script started on 2013年12月20日 星期五 01时21分19秒
amosli@amosli-pc:~$ who
amosli   tty7         2013-12-19 22:58
amosli   pts/1        2013-12-19 22:59 (:0.0)
amosli   pts/2        2013-12-20 01:14 (:0.0)
amosli   pts/4        2013-12-20 01:16 (:0.0)
amosli   pts/5        2013-12-20 01:20 (:0.0)
amosli   pts/6        2013-12-20 01:20 (:0.0)
amosli@amosli-pc:~$ 

 

 

你可能感兴趣的:(Linux shell)