温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux中如何使用script命令

发布时间:2021-10-23 15:43:51 来源:亿速云 阅读:471 作者:小新 栏目:系统运维

这篇文章将为大家详细讲解有关Linux中如何使用script命令,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

如何使用 script 命令?

默认情况下,直接输入 script 这个命令即可,它会在当前目录自动创建一个 typescript  文件,之后你在此终端的所有操作都会被记录在这个文件里。

记录文件是一个文本文件,可以使用任意的文本工具打开查看。

如果要退出记录,可以在终端里按快捷键 ctrl + D 或直接输入 exit 。在退出 script 前,你会发现,记录文件大小为 0  Kb,当退出后,文件大小会变大。

[alvin@VM_0_16_centos test]$ script Script started, file is typescript [alvin@VM_0_16_centos test]$ echo hello hello [alvin@VM_0_16_centos test]$ ls test1.py  test2  test2.cpp  test2.py  test3  test3.c  test.py  typescript  WeixinBot  wxpy  wxRobot [alvin@VM_0_16_centos test]$ exit exit Script done, file is typescript

如果我们想要自己起个文件名,或者将文件放在其它位置,那么我们可以直接在 script 后面跟上文件名即可。

[alvin@VM_0_16_centos test]$ script ~/alvin-script Script started, file is /home/alvin/alvin-script [alvin@VM_0_16_centos test]$ ll total 64 -rw-rw-r--  1 alvin alvin    21 Nov 10 09:40 test1.py -rwxrwxr-x  1 alvin alvin 14074 Dec 31 07:35 test2 -rw-rw-r--  1 alvin alvin   403 Dec 31 07:35 test2.cpp -rw-rw-r--  1 alvin alvin  2093 Nov 10 10:50 test2.py -rwxrwxr-x  1 alvin alvin  8553 Jan  7 20:03 test3 -rw-rw-r--  1 alvin alvin    78 Jan  7 20:03 test3.c -rw-rw-r--  1 alvin alvin    94 Nov  9 23:25 test.py -rw-rw-r--  1 alvin alvin   489 Jan 11 12:07 typescript drwxrwxr-x  6 alvin alvin  4096 Nov 10 11:19 WeixinBot drwxrwxr-x  6 alvin alvin  4096 Nov 10 11:30 wxpy drwxrwxr-x 11 alvin alvin  4096 Nov 10 11:34 wxRobot [alvin@VM_0_16_centos test]$ echo hello hello [alvin@VM_0_16_centos test]$ exit exit Script done, file is /home/alvin/alvin-script

学会这两个基本操作,可以应付很多场景下需要记录终端的场景。

如何使用 script 与同事协作?

现在有一项工作,需要与同事一起协作,我完成一半,他完成另一半。

首先,我来做我的工作,用 script 记录一下我的工作过程:

[alvin@VM_0_16_centos test]$ script cooperate-job Script started, file is cooperate-job [alvin@VM_0_16_centos test]$ echo this is alvin_s job this is alvin_s job [alvin@VM_0_16_centos test]$ ls cooperate-job  test1.py  test2  test2.cpp  test2.py  test3  test3.c  test.py  typescript  WeixinBot  wxpy  wxRobot [alvin@VM_0_16_centos test]$ exit exit Script done, file is cooperate-job

工作完成之后,将记录文件发给同事,他可以使用文本工具打开,就可以知道你的进度了,然后接着你的进度干活。

如果他要接着在你的记录文件里记录他的操作的话,可以加一个 -a 选项,即 append 的缩写。

[alvin@VM_0_16_centos test]$ script -a cooperate-job Script started, file is cooperate-job [alvin@VM_0_16_centos test]$ echo this is harry_s job this is harry_s job [alvin@VM_0_16_centos test]$ pwd /home/alvin/test [alvin@VM_0_16_centos test]$ exit exit Script done, file is cooperate-job

请他人远程协助时,如何记录他的操作过程?

让他人登陆到自己的电脑,如果是熟人还好,是陌生人的话心里多少会有些不踏实。为了放心一下,我们还是偷偷记录一下他的所作所为吧。

我们可以将 script 命令添加到 Shell 配置文件中,用户一旦登录进来,script 命令就自动启动,并记录操作者的所有操作过程。

实现这个目的,我们可以修改 .bash_profile 文件。

vim ~/.bash_profile

在最后一行,我们将 script 命令添加进去:

/usr/bin/script -qa your_path #补齐自己的路径

然后保存,使用 source 或 . 命令使它生效。下次其它人登录到系统时,script 就会自动运行,并将记录文件保存在你所指定的位置。

在这里,-q 选项代表静默记录,对方将不知道你在后台记录。如果不使用这个选项,则他会收到这个提示:

Last login: Fri Jan 11 15:13:37 2019 from 119.33.28.6 Script started, file is /home/alvin/test/script-file  #提示 [alvin@VM_0_16_centos ~]$

关于“Linux中如何使用script命令”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI