简体   繁体   English

将Shell脚本结果输出到类似于终端屏幕的文件

[英]Output shell script result to file like terminal screen

I am a beginner in shell scripting and this is my sample script: 我是shell脚本的初学者,这是我的示例脚本:

#!/bin/ksh
echo "start script"
ls
echo "end script"

I run it by this command: ./runScript.ksh > outPutFile.txt 我通过以下命令运行它: ./runScript.ksh > outPutFile.txt

and got the output inside file: 并在文件中得到了输出:

start script
file1.txt
file2.txt
file3.txt
end script

I just want to ask if it is possible to have this output on the file? 我只想问一下是否有可能在文件中有此输出? Like what is shown if the commands are executed on the terminal?: 就像在终端上执行命令时显示的内容一样:

user@serverName:/parentPath/childPath/>echo "start script"
user@serverName:/parentPath/childPath/>ls
file1.txt
file2.txt
file3.txt
user@serverName:/parentPath/childPath/>echo "end script"

Thank you so much in advance. 提前非常感谢您。 I will really appreciate any help. 我将非常感谢您的帮助。

Use the 'script' command. Any command issued after the 'scipt' command will be captured in the specified file (or default file typescript).

**Example:**

$ script my_output.txt
Script started, output file is my_output.txt
$ uname -r
16.1.0
$ date
Wed Mar  8 12:55:29 IST 2017
$ exit

Script done, output file is my_output.txt
$ cat my_output.txt
Script started on Wed Mar  8 12:55:19 2017
$ uname -r
16.1.0
$ date
Wed Mar  8 12:55:29 IST 2017
$ exit

Script done on Wed Mar  8 12:55:32 2017
$

man script

SCRIPT(1)                 BSD General Commands Manual                SCRIPT(1)


NAME
     script -- make typescript of terminal session

SYNOPSIS
     script [-adkpqr] [-F pipe] [-t time] [file [command ...]]

DESCRIPTION
     The script utility makes a typescript of everything printed on your terminal.  It is useful for students who need a hardcopy record of an interactive session
     as proof of an assignment, as the typescript file can be printed out later with lpr(1).

     If the argument file is given, script saves all dialogue in file.  If no file name is given, the typescript is saved in the file typescript.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM