Author Topic: Capturing Interactive Shell Script Output  (Read 9300 times)

daniel3000

  • Hero Member
  • *****
  • Posts: 1003
    • View Profile
    • http://
Capturing Interactive Shell Script Output
« 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
SL-C3200 with weeXpc, based on pdaXrom 1.1.0beta3
HP 200LX with MS-DOS 5.0

Da_Blitz

  • Hero Member
  • *****
  • Posts: 1579
    • View Profile
    • http://www.pocketnix.org
Capturing Interactive Shell Script Output
« Reply #1 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)
Personal Blog
Code
Twitter

Gemini Order: #95 (roughly)
Current Device: Samsung Chromebook Gen 3
Current Arm Devices Count: ~30
Looking to acquire: Cavium Thunder X2 Hardware

grog

  • Hero Member
  • *****
  • Posts: 692
    • View Profile
    • http://
Capturing Interactive Shell Script Output
« Reply #2 on: April 10, 2007, 10:55:42 am »
The script utility saves all command line output. If it's not available for your ROM, it's part of the util-linux kernel package if you want to compile it. HTH
GROG!

daniel3000

  • Hero Member
  • *****
  • Posts: 1003
    • View Profile
    • http://
Capturing Interactive Shell Script Output
« Reply #3 on: April 10, 2007, 12:59:47 pm »
Quote
The script utility saves all command line output. If it's not available for your ROM, it's part of the util-linux kernel package if you want to compile it. HTH
[div align=\"right\"][{POST_SNAPBACK}][/a][/div]

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
SL-C3200 with weeXpc, based on pdaXrom 1.1.0beta3
HP 200LX with MS-DOS 5.0

grog

  • Hero Member
  • *****
  • Posts: 692
    • View Profile
    • http://
Capturing Interactive Shell Script Output
« Reply #4 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
GROG!

daniel3000

  • Hero Member
  • *****
  • Posts: 1003
    • View Profile
    • http://
Capturing Interactive Shell Script Output
« Reply #5 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
SL-C3200 with weeXpc, based on pdaXrom 1.1.0beta3
HP 200LX with MS-DOS 5.0

ShiroiKuma

  • Hero Member
  • *****
  • Posts: 900
    • View Profile
Capturing Interactive Shell Script Output
« Reply #6 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...
[span style=\'font-size:8pt;line-height:100%\']Das ganze tschechische Volk ist eine Simulantenbande.[/font][/span]
Militäroberarzt Bautze