'script' command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 'script' command
# 1  
Old 02-12-2017
'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.

Code:
#! /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
Code:
cat somefile

This is the message when the script stops.
Code:
Script started, file is /home/test.log

Any help would be appreciated
# 2  
Old 02-12-2017
Quote:
Attempting to use the 'script' command to save the session to a log
Ok
but what follows after:
Code:
#! /usr/bin/sh
script /home/test.log
cat somefile
exit

Is not a session but a script...
Code:
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 :
Code:
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 ...
This User Gave Thanks to vbe For This Post:
# 3  
Old 02-12-2017
VBE then the 'script' command can not be used in a executable script?
# 4  
Old 02-12-2017
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:

Code:
./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.
This User Gave Thanks to drysdalk For This Post:
# 5  
Old 02-12-2017
thanks guys!
# 6  
Old 02-12-2017
Quote:
Originally Posted by jimmyf
VBE then the 'script' command can not be used in a executable script?
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

2. UNIX for Dummies Questions & Answers

Shell script not working but command works in command prompt

Hi everyone I have a problem with my script If I try directly this command /usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz It works But if I simply add this command in a script and... (8 Replies)
Discussion started by: picemma
8 Replies

3. UNIX for Dummies Questions & Answers

Set Command to output a log of every command executed in the script

Hi Guys, I like to output every command executed in the script to a file. I have tried set -x which does the same. But it is not giving the logs of the child script which is being called from my script. Is there any parameters in the Set command or someother way where i can see the log... (2 Replies)
Discussion started by: mac4rfree
2 Replies

4. Shell Programming and Scripting

In Shell Script Does Second Command Wait For First Command To Complete

Hi All, I have a question related to Shell scripting. In my shell script, I have following two commands in sequence: sed 's/^/grep "^120" /g' $ORIGCHARGEDAMTLIST|sed "s;$;| cut -f$FIELD_NO1 -d '|' | awk '{ sum+=\$1} END {printf (\"%0.2f\\\n\", sum/100)}' >$TEMPFILE mv $TEMPFILE $ORIGFILE... (3 Replies)
Discussion started by: angshuman
3 Replies

5. Shell Programming and Scripting

Script to check one command and if it fails moves to other command

Input is list of Server's, script is basically to remove old_rootvg, So it should check first command "alt_rootvg_op -X old_rootvg" if it passes move to next server and starts check and if it fails moves to other command "exportvg old_rootvg" for only that particular server. I came up with below,... (6 Replies)
Discussion started by: aix_admin_007
6 Replies

6. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

7. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

8. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

9. Shell Programming and Scripting

can anyone help with shell script command about searching word with grep command?

i want to search in the current directory all the files that contain one word for example "hello" i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies
Login or Register to Ask a Question