how to capture or log outputs/message during script execution
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.