LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-11-2006, 08:29 AM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,357

Rep: Reputation: 50
Script to create a log file


Hi folks,

I need each command line typed on Console/Xterm window to be automatically copied to a file after hitting [ENTER] and the last "ten/x" lines of each corresponding output on the screen to be recorded on the file simultaneously. There are >300 comand lines to be executed. I'm building LFS-LinuxFromScratch. I only need the last "x" lines of the output on the screen to be recorded because on compilation maybe there are >10 pages of output. It serves as a log file.

I don't expect adding >file/>>file to the end of each command lines typed, if possible.

Please advise whether it is possible to run a shell script so that my requirement can be done. If YES, please advise how to prepare this shell script.

Is there another alternative to do it instead of running a shell script.

TIA

B.R.
satimis
 
Old 01-11-2006, 09:19 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

I'd probably just use the 'script' command to catch everything input and output to the terminal, then use a combination of grep -n, head and tail to parse the resulting log later. Doing this also means you have a full log, which might be more useful than just the last few lines of each command output.

Dave
 
Old 01-11-2006, 09:49 AM   #3
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,357

Original Poster
Rep: Reputation: 50
Hi Dave,

Tks for your advice.

I haven't figured out how to make use of "script" and "grep -n" commands to do the job copying all typescript printed on the terminal and the corresponding output to a file. Could you please shed me some light.

e.g. on 3 command lines
- command line-1
- command line-2
- command line-3

File name = XYZ

TIA

B.R.
satimis
 
Old 01-11-2006, 11:21 AM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
OK

If you run 'script' in a console, everything that is typed into, or output to the console gets recorded in a file called 'typescript' in your current directory. The 'man script' should describe it better than I can.

Once you've run your commands, do Ctrl-D to stop the 'script' command.

You can then look for the output of a particular command by grep'ing for your prompt and the name of the command, e.g. my command prompt looks like:
Code:
[0 dave@cronus ~]$
so if I was looking for the output of a command called 'multiburn', I would do:
Code:
grep -n 'dave@cronus' typescript | grep multiburn
the line number in the typescript file that command appears in would be shown as the number at the start of the line. You can look at the output of that command by jumping to that line number with
Code:
less +<lineNumber>g typescript
Dave
 
Old 01-12-2006, 01:06 AM   #5
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,357

Original Poster
Rep: Reputation: 50
Hi Dave,

If I understand your advice correctly I shall perform following steps.

1)
To let typescript and screen output go to the file "xyz"

1a)
on console run;
# script /path/to/xyz

1b)
keyin the command lines

(remark:
I have to save "xyz" on the hard disc because I'm running LiveCD to build LFS. Otherwise once reboot all data saved will lose)

1c)
Ctrl-D to stop the 'script' command.



2)
To read info on "xyz"

2a)
To read the output of "command line-1"
# grep "command line-1" /path/to/xyz

2b)
To read line-210
# less +210g /path/to/xyz

If i'm wrong please correct me, tks.



Now I don't expect I can finish all commands in one day. On following day whether I have to run;

# script /path/to/xyz_2

Can I direct typescript of new command lines and screen output go to the same file "xyz" without overwriting previous content, i.e continue on the same file in following day.

TIA

B.R.
satimis

Last edited by satimis; 01-12-2006 at 01:08 AM.
 
Old 01-12-2006, 07:17 AM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi again.

That looks just right.

You can add another session's output to xyz by using 'script -a /path/to/xyz' instead of 'script /path/to/xyz'

Dave
 
Old 01-12-2006, 07:45 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
blimey, script, never knew that one.
excellent work.
 
Old 01-12-2006, 08:39 AM   #8
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,357

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by ilikejam
You can add another session's output to xyz by using 'script -a /path/to/xyz' instead of 'script /path/to/xyz'
Hi Dave,

Noted with tks

BR
satimis
 
Old 01-13-2006, 06:41 AM   #9
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Cool. Enjoy.

Dave
 
Old 01-13-2006, 09:31 AM   #10
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,357

Original Poster
Rep: Reputation: 50
Hi Dave,

I ran following test creating a file "lfs_notes_061113".
But it was an empty file. I think I made a misktake but could not discover the cause.

Test:
====

Started a LiveCD

root@4[/]# mount /dev/hda8 /mnt/hda8
root@4[/]# script /mnt/hda8/lfs_notes_061113
Code:
Script started, file is /mnt/hda8/lfs_notes_061113
sh-3.00# export LFS=/mnt/lfs
sh-3.00# mount -pv /dev/hda3 $LFS
sh-3.00# mkdir -v $LFS/home
Code:
mkdir: created directory ‘/mnt/lfs/home’
sh-3.00# mount -v /dev/hda5 $LFS/home
Code:
mount: you didn't specify a filesystem type for /dev/hda5
       I will try type ext3
/dev/hda5 on /mnt/lfs/home type ext3 (rw)
sh-3.00# echo $LFS
Code:
/mnt/lfs
sh-3.00# mkdir -v $LFS/tools
Code:
mkdir: created directory ‘/mnt/lfs/tools’
sh-3.00# ln -sv $LFS/tools /
Code:
create symbolic link ‘//tools’ to ‘/mnt/lfs/tools’
Here I started another console and mount /dev/hda8

root@6[/]# ls /mnt/hda8
Code:
lost+found
lfs_notes_061113
root@6[/]# nedit /mnt/hda8/lfs_notes_061113
It was an empty file

root@6[/]# du -sh /mnt/hda8/lfs_notes_061113
Code:
0       /mnt/hda8/lfs_notes_061113
Can you see which step I went wrong?

TIA

B.R.
satimis
 
Old 01-13-2006, 12:02 PM   #11
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

Yep. You have to do Ctrl-D (or 'exit') to finish the script session and write the file.

Dave
 
Old 01-14-2006, 09:42 AM   #12
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,357

Original Poster
Rep: Reputation: 50
Hi Dave,

That means before executing "Ctrl+D" the file created is empty. If I need to read the history I must execute "Ctrl+D". To continue, run;
# script -a /path/to/file

and so on.


B.R.
satimis
 
Old 01-14-2006, 12:24 PM   #13
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
If you run script with the '-f' option, it'll write to the file after each operation. That might be what you're after.

The 'script' man page is quite helpful ;-)

Dave
 
Old 01-16-2006, 06:18 AM   #14
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,357

Original Poster
Rep: Reputation: 50
Hi Dave,

Your advice noted with tks.

On "man script"
Code:
...
......
     -c COMMAND
             Run the COMMAND rather than an interactive shell.  This makes it easy for a script to capture
             the output of a program that behaves differently when its stdout is not a tty.

     -f      Flush output after each write. This is nice for telecooperation: One person does `mkfifo foo;
             script -f foo' and another can supervise real-time what is being done using `cat foo'.

     -q      Be quiet.

     -t      Output timeing data to standard error. This data contains two fields, separated by a space.
             The first field indicates how much time elapsed since the previous output. The second field
             indicates how many characters were output this time. This information can be used to replay
             typescripts with realistic typing and output delays.
......
What will be the use of options - c, q and t ?

What shall I replace "COMMAND" on the command line to be run?

TIA

B.R.
satimis
 
Old 01-16-2006, 06:30 AM   #15
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
-c would be used if you just want to run a single command (replace COMMAND with that command), or if the command you are running does something weird when it output isn't a terminal. Can't think of any examples of programs that do this, though. You could use this option to gather the output from commands if they're being run non-interactively, like from a shell script.

-q means it doesn't print the 'Script started, file is typescript' message at the start, or the message at the end.

-t prints (to std error, not std out) the time between keystrokes and outputs.

Dave
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create log file from script fotoguy Programming 12 03-27-2012 06:39 AM
Create text file with a script zael Programming 3 06-02-2004 03:27 AM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 06:57 AM
perl script to create a backwards file?! WorldBuilder Programming 16 10-30-2003 10:05 PM
PHP Script to retrieve queries from log file saravanan1979 Programming 1 03-17-2002 08:13 AM


All times are GMT -5. The time now is 11:58 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration