LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2012, 10:33 AM   #1
VijayaRaghavanLakshman
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Rep: Reputation: Disabled
how to capture or log outputs/message during script execution


Hi All,

Am struck with one of the requirement which I want to include in my script.

Have a script which call several sub functions, reads user inputs and perform actions.

requirement : Is to capture all the Inputs/actions/messages while executing script. (in short, messages displayed in cmd prompt while executing script)

I tried with create logfile & appending them using tee logfilename but couldn't succeed.

Anyhelp please...

Thanks,
Vijay
 
Old 01-23-2012, 12:12 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi VijayaRaghavanLakshman,

Welcome to LQ!!!

You can use script command to achieve what you are looking for. Once you type script command in the terminal it will create a file name typescript and start logging everything that ran in the terminal. Remember it will create the typescript file under the same directory where you were when you ran the script command.

Syntax is pretty simple:

Code:
script
to exit or end logging type the following:

Code:
exit
and it will display a message saying "Script started, file is typescript"

You an put this command at the beginning of your script or you can run this on the terminal before executing your script. Just do man script to get more options on this command.

Last edited by T3RM1NVT0R; 01-24-2012 at 02:44 AM.
 
Old 01-24-2012, 02:55 AM   #3
VijayaRaghavanLakshman
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks T3RM1NVT0R for the reply.

I did try using script command in both ways ie,

a. adding "script command" at the top of shell script :

outcome :

1. It does create file called "typescript" from where shell script executed but file size is zero.

2. As soon as i run the shell script i get the message & terminates the shell script.(though this script has interactive mode so that can get user input and perform actions based on selected choices)

Code:
vijay@vijay-laptop:~/shell_scripts$ ./Install_steps.sh 
Script started, file is typescript
vijay@vijay-laptop:~/shell_scripts$
b. I tired another way as well, ie, typing script command first & then executing shell script.

Outcome :

1. Shell script run in interactive mode gets the options/choices and inputs from user and performs action but it doesn't store anything in "typescript" file (file size still zero)

Is there anything wrong in the way am using this script command. or is there any other way to achieve to log every actions/inputs performed during shell script execution.

Regards,
Vijay

Last edited by VijayaRaghavanLakshman; 01-24-2012 at 03:00 AM.
 
Old 01-24-2012, 04:26 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Presuming you are using read to get user input and echo for output, how about replacing them with functions, say my_read and my_echo, which do what the builtins do but also write the input and output to a log file?

Last edited by catkin; 01-24-2012 at 04:27 AM. Reason: corrected "standard functions" to builtins
 
Old 01-24-2012, 07:34 AM   #5
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

You have to type exit command as I mentioned in my previous post (edited version) to end the script and wirte to typescript file.

Rember typescript file looks exactly the same how terminal looked at the time of execution of commands / script. So dont get confused.
 
Old 01-24-2012, 09:34 AM   #6
VijayaRaghavanLakshman
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks for the reply

but it still didn't do any good.

T3RM1NVT0R - As you have pointed out i put both script & exit command but as soon as i run the shell script, it terminates the execution.

ie,

Code:
root@vijay-laptop:/home/vijay/shell_scripts# ./Install_steps.sh 
Script started, file is typescript
root@vijay-laptop:/home/vijay/shell_scripts#
it just create empty typescript file

Last edited by VijayaRaghavanLakshman; 01-24-2012 at 09:36 AM.
 
Old 01-24-2012, 09:46 AM   #7
VijayaRaghavanLakshman
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
At last, found out an alternative way to do this. ie, while running shell script we need to use this command :

Code:
root@vijay-laptop:/home/vijay/shell_scripts# script -c ./Install_steps.sh Install.log
Once the shell script execution done, we can see
Code:
Script done, file is Install.log
root@vijay-laptop:/home/vijay/shell_scripts#
Am in the log file, can see all the actions/user inputs/commands type...

If anyone knows better way then do please let us know.

Thanks,
vijay
 
Old 01-25-2012, 08:00 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
I suggested what I believe is a better solution in this post.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Execution of awk script gives error message for invalid character tanggo81 Linux - Kernel 2 06-26-2010 02:35 AM
Help w/ script to read file and parse log message shyork2001 Linux - General 4 04-06-2010 11:48 AM
shell script, which should diff between ps outputs? m4rtin Programming 3 02-18-2010 07:29 AM
Trigger script by message in /var/log/messages? Zoidy Linux - Software 3 01-17-2004 12:03 AM
ODD! Cygwin - cat tr outputs email message from ealier in the day?!?!?!?! chingasman Linux - General 0 02-27-2003 05:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration