输出shell脚本结果像终端屏幕一样文件



我是外壳脚本的初学者,这是我的示例脚本:

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

我通过此命令运行它: ./runScript.ksh > outPutFile.txt

并获取输出文件:

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

我只想问是否可以在文件上输出此输出?像在终端上执行命令时所示的内容?:

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"

非常感谢您。我将非常感谢任何帮助。

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.

相关内容

  • 没有找到相关文章

最新更新