'script' command

Attempting to use the 'script' command to save the session to a log. However when the command is used as the first line of the executable script, the script stops and won't proceed.

#! /usr/bin/sh
script /home/test.log
cat somefile
exit

The prompt just sits there and will not proceed to the next command with is

cat somefile

This is the message when the script stops.

Script started, file is /home/test.log

Any help would be appreciated

Ok
but what follows after:

#! /usr/bin/sh
script /home/test.log
cat somefile
exit

Is not a session but a script...

script /home/test.log

script get executed and is waiting for session commands... only as in a script, the script will pass to the next line :

cat somefile

only once you have exited script...

In other words it doesnt work the way you imagine as it is to capture interactive session ...

1 Like

VBE then the 'script' command can not be used in a executable script?

Hi,

Essentially, this is correct behaviour. What's happening is that the 'script' command is spawning its own sub-shell underneath your current shell, and that's why you get a shell prompt and execution of the script does not continue. If you type 'exit' at that new prompt, the sub-shell will exit and the execution of the script will continue as normal past that point, albeit without being logged as a typescript.

If what you're actually wanting to accomplish is logging the output of a script, there's a few ways you could go about it, and the 'script' command isn't in fact the appropriate answer here.

If the script is non-interactive - that is, it requires no user input (and your provided test script looks like it doesn't) - then you could perhaps do something like this to log its output:

./script.sh | tee -a log.txt

The 'tee' command ensures that you still see the output of a script on standard output, but it also gets captured to a logfile (log.txt in the current directory, in this case). User inputs and certain other things won't get captured this way, but regular output will. This will make sure it goes to a file, and to the screen at the same time.

1 Like

thanks guys!

No. The script command can be used in a shell script as long as the user invoking the script directly interacts with whatever commands the script command runs OR your shell script redirects the input to the script command to complete whatever the script is running until the commands being run by script have terminated.