ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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:
[0 dave@cronus ~]$
so if I was looking for the output of a command called 'multiburn', I would do:
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
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.