ProgrammingThis 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.
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.
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.
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.
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
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
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.
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
...
......
-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?
-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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.