OESF Portables Forum

Everything Else => Desktop Operating Systems Issues => Zaurus General Forums => Archived Forums => Linux Issues => Topic started by: daniel3000 on April 10, 2007, 03:47:11 am

Title: Capturing Interactive Shell Script Output
Post by: daniel3000 on April 10, 2007, 03:47:11 am
Hi guys,

does anyone know how to capture the output (stdout AND stderr) of an interactive shell script to a file?

Of course I could write a "| tee ..." behind every single command, but I'd like to avoid that hassle.
Using "| tee" on the entire script eats the interactivity ("read -p" output isn't shown on the screen anymore).

Thank you!
daniel
Title: Capturing Interactive Shell Script Output
Post by: Da_Blitz on April 10, 2007, 04:31:03 am
i belive there is a pam module, however it might not do what you want

i know there is a program out there but its not perfect, you could hack up some messy code using tee as you said by using pipes and file descripter redirection however perhaps bash has an option to allow you to dump IO

well lets see what dirty code i can come up with

Code: [Select]
#!/bin/sh

# use these for real time logging and processing (ie external program to add timestamps)
#mkfifo stdin
#mkfifo stdout

tee stdin | $* 2>&1 | tee stdout

just watch out for stuff that uses the ncurses libary, this will not work with them. same goes for anything using escape chars, or if it does use it make sure you only playback the script on the same term (otherwise escape chars might be diffrent)
Title: Capturing Interactive Shell Script Output
Post by: grog on April 10, 2007, 10:55:42 am
The script (http://www.scism.sbu.ac.uk/law/UnixStuff/script.html) utility saves all command line output. If it's not available for your ROM, it's part of the util-linux (http://www.kernel.org/pub/linux/utils/util-linux/) kernel package if you want to compile it. HTH
Title: Capturing Interactive Shell Script Output
Post by: daniel3000 on April 10, 2007, 12:59:47 pm
Quote
The script (http://www.scism.sbu.ac.uk/law/UnixStuff/script.html) utility saves all command line output. If it's not available for your ROM, it's part of the util-linux (http://www.kernel.org/pub/linux/utils/util-linux/) kernel package if you want to compile it. HTH
[div align=\"right\"][{POST_SNAPBACK}][/a][/div] (http://index.php?act=findpost&pid=158412\")

hmm- the util-linux package on [a href=\"http://www.tyrannozaurus.com/feed/beta3/feed/]http://www.tyrannozaurus.com/feed/beta3/feed/[/url] does not contain "script".

Now trying to compile the sources 2.12r.
Thanks for the hint! It just looks like what I need!

daniel
Title: Capturing Interactive Shell Script Output
Post by: grog on April 10, 2007, 04:01:39 pm
Quote
Now trying to compile the sources 2.12r.
Thanks for the hint! It just looks like what I need![div align=\"right\"][a href=\"index.php?act=findpost&pid=158420\"][{POST_SNAPBACK}][/a][/div]
Glad I could help. Don't forget to share
Title: Capturing Interactive Shell Script Output
Post by: daniel3000 on April 11, 2007, 03:30:59 am
Quote
Quote
Now trying to compile the sources 2.12r.
Thanks for the hint! It just looks like what I need![div align=\"right\"][a href=\"index.php?act=findpost&pid=158420\"][{POST_SNAPBACK}][/a][/div]
Glad I could help. Don't forget to share
[div align=\"right\"][a href=\"index.php?act=findpost&pid=158438\"][{POST_SNAPBACK}][/a][/div]

Of course I won't!  

So here is the solution:
Attached is the script utility (stripped binary only, compiled for pdaXrom 1.1.0beta3), just unzip and copy it to /usr/bin).

I have also developed a nice trick to make usage of "script" totally transparent to the user, so the user still only has to call the shell script as usual.
Normally, in order to create a log from a shell script execution, you need to call the script with "script" this way:

Code: [Select]
script -c "shellscript.sh" logfilename
This can be automated within the actual shell script to be executed and logged.
Just put these lines at the beginning of the shell script:

Code: [Select]
TODAY=`date +%Y-%m-%d_%Hh%M`

if [ ! "$1" = "run" ]; then
  script -c "$0 run" ${TODAY}_LOGFILE
  echo Created logfile ${TODAY}_LOGFILE
  exit 0
fi

So when calling the shell script as usual, it actually calls "script" which, with the -c parameter, calls the shell script recursively once more to execute its actual contents.
The "run" parameter is used to determine if the user calls the script (go into the "if") or if script has called it (run the actual script contents).

In the above example, "script" will create a logfile in the currect directory with the name (for example)

2007-04-11_09:25_LOGFILE

Interactivity of the shell script, here using mainly echo and read, is fully preserved.

Thank you for your help!

daniel
Title: Capturing Interactive Shell Script Output
Post by: ShiroiKuma on April 11, 2007, 04:53:00 am
My preferred solution to this problem is to use Emacs.

When you run the shell in Emacs, either bash or eshell, you can then simply save the buffer to a file, you can browse the shell window like a text file etc.

It's superb...