简体   繁体   中英

Output shell script result to file like terminal screen

I am a beginner in shell scripting and this is my sample script:

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

I run it by this command: ./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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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