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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
01-11-2006, 08:29 AM
|
#1
|
Senior Member
Registered: Apr 2003
Posts: 3,695
Rep:
|
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
|
|
|
01-11-2006, 09:19 AM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
01-11-2006, 09:49 AM
|
#3
|
Senior Member
Registered: Apr 2003
Posts: 3,695
Original Poster
Rep:
|
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
|
|
|
01-11-2006, 11:21 AM
|
#4
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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: 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
|
|
|
01-12-2006, 01:06 AM
|
#5
|
Senior Member
Registered: Apr 2003
Posts: 3,695
Original Poster
Rep:
|
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.
|
|
|
01-12-2006, 07:17 AM
|
#6
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
01-12-2006, 07:45 AM
|
#7
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
blimey, script, never knew that one.
excellent work.
|
|
|
01-12-2006, 08:39 AM
|
#8
|
Senior Member
Registered: Apr 2003
Posts: 3,695
Original Poster
Rep:
|
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
|
|
|
01-13-2006, 06:41 AM
|
#9
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
Cool. Enjoy.
Dave
|
|
|
01-13-2006, 09:31 AM
|
#10
|
Senior Member
Registered: Apr 2003
Posts: 3,695
Original Poster
Rep:
|
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 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
|
|
|
01-13-2006, 12:02 PM
|
#11
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
Hi.
Yep. You have to do Ctrl-D (or 'exit') to finish the script session and write the file.
Dave
|
|
|
01-14-2006, 09:42 AM
|
#12
|
Senior Member
Registered: Apr 2003
Posts: 3,695
Original Poster
Rep:
|
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
|
|
|
01-14-2006, 12:24 PM
|
#13
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
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
|
|
|
01-16-2006, 06:18 AM
|
#14
|
Senior Member
Registered: Apr 2003
Posts: 3,695
Original Poster
Rep:
|
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
|
|
|
01-16-2006, 06:30 AM
|
#15
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
-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
|
|
|
All times are GMT -5. The time now is 04:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|