【问题标题】:Output shell script result to file like terminal screen将 shell 脚本结果输出到终端屏幕等文件
【发布时间】:2017-03-08 06:56:18
【问题描述】:

我是 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"

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

【问题讨论】:

  • 试试ksh -x ./runScript.ksh > outPutFile.txt
  • 嗨,@codeforester,输出是一样的 :(
  • 跟踪部分进入标准错误。所以,ksh -x ./runScript.ksh &> outPutFile.txt。如果您想要整个会话,script 可能是您的选择。
  • ksh -x ./runScript.ksh &> outPutFile.txt 生成一个空白文件。我想将命令放在 shell 脚本中,因为稍后我将在其中添加更多命令。我认为script 命令仅适用于在终端上键入命令?

标签: linux bash shell unix ksh


【解决方案1】:
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.

【讨论】:

  • 嗨,@Sharad,我可以把它放在 shell 脚本中吗?因为稍后,我会在 shell 脚本中添加许多命令。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-12-15
  • 2018-10-11
  • 2011-08-23
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多